<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Phoebe Wong on Medium]]></title>
        <description><![CDATA[Stories by Phoebe Wong on Medium]]></description>
        <link>https://medium.com/@phoebew?source=rss-1f5750b4b955------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*FTlDN8fots-_kT5wDZ4isQ.jpeg</url>
            <title>Stories by Phoebe Wong on Medium</title>
            <link>https://medium.com/@phoebew?source=rss-1f5750b4b955------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 11:57:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@phoebew/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building An Image Recommendation System For News Articles using Word and Sentence Embeddings]]></title>
            <link>https://medium.com/data-science/building-an-image-recommendation-system-for-news-articles-using-word-and-sentence-embeddings-a5acb485a445?source=rss-1f5750b4b955------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5acb485a445</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[word-embeddings]]></category>
            <category><![CDATA[towards-data-science]]></category>
            <category><![CDATA[sentence-embedding]]></category>
            <category><![CDATA[nlp]]></category>
            <dc:creator><![CDATA[Phoebe Wong]]></dc:creator>
            <pubDate>Tue, 17 Dec 2019 14:27:49 GMT</pubDate>
            <atom:updated>2019-12-17T17:40:34.579Z</atom:updated>
            <content:encoded><![CDATA[<figure><a href="https://miro.medium.com/max/1200/1*9B7S9mipwLURUX9wdZN7Pw.jpeg"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cEaeMuTvINqIgyYQMSJWUA.jpeg" /></a><figcaption>Source: <a href="https://miro.medium.com/max/1200/1*9B7S9mipwLURUX9wdZN7Pw.jpeg">https://miro.medium.com/max/1200/1*9B7S9mipwLURUX9wdZN7Pw.jpeg</a></figcaption></figure><p><em>Authors: Karina Huang, Dianne Lee, Abhimanyu Vasishth and Phoebe Wong (alphabetized by last name)</em></p><p><em>The views expressed in this post are the views of the authors and not those of Associated Press.</em></p><h3><strong>Introduction</strong></h3><p>This blog post showcases a part of our Capstone project at <a href="https://www.capstone.iacs.seas.harvard.edu/">Harvard AC297r</a> class in collaboration with the <a href="https://www.ap.org/en-us/">Associated Press</a> (AP). We built a Text-to-Image recommendation system for images for any given news article headline. The goal of this project is as follows:</p><blockquote>Given an article headline, recommend a set of best matching images using their captions.</blockquote><p>Since Machine Learning methods cannot optimize text directly, we need a way to convert text to a numerical representation. For this task, we use word embeddings, which are means by which a word can be represented as a vector of numbers.</p><p>In the sections below, we discuss our approaches using two different kinds of embeddings 1) average GloVe word embeddings, and 2) Universal Sentence Encoder (USE) to encode article headlines and image captions, followed by the recommendation process and examples.</p><h3><strong>Data</strong></h3><p>Since the data we used for our project is proprietary to our industry partner, we used a publicly available dataset that contains news articles and their associated images for the purposes of this blog post, in order to make our methods reproducible. We downloaded the <a href="https://drive.google.com/file/d/1rswGdNNfl4HoP9trslP0RUrcmSbg1_RD/view">data</a> provided by <a href="http://openaccess.thecvf.com/content_CVPR_2019/papers/Biten_Good_News_Everyone_Context_Driven_Entity-Aware_Captioning_for_News_Images_CVPR_2019_paper.pdf">Biten, A. F., Gomez, L., Rusinol, M., &amp; Karatzas, D. (2019)</a>. We then used the following code chunk to extract the article headlines and image captions. The images are provided by the original authors <a href="https://drive.google.com/file/d/1RF-XlPTNHwwh_XcXE2a1D6Am3fBPvQNy/view">here</a> as well.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f4c0a63af064b4d6f920537b1d80317/href">https://medium.com/media/1f4c0a63af064b4d6f920537b1d80317/href</a></iframe><p>After cleaning up the data by excluding articles with fewer than 5 words in their headlines and images with captions fewer than 5 words, the final dataset has 276596 news articles, with 431841 associated images.</p><p>We used the <a href="https://github.com/amueller/word_cloud">WordCloud</a> Python library to create word clouds of the words in the article headlines (left) and image captions (right).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*I_wQX5JR9rzwpF0hypMqQQ.png" /></figure><p>Here is some visualization of the summary statistics of the dataset.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X6WfgYkAmfmphSJ7U1Umkw.png" /></figure><h3><strong>GloVe Word Embeddings</strong></h3><p>From the <a href="https://nlp.stanford.edu/projects/glove/">Stanford NLP Website</a> about GloVe Embeddings:</p><blockquote>“GloVe is an unsupervised learning algorithm for obtaining vector representations for words. Training is performed on aggregated global word-word co-occurrence statistics from a corpus, and the resulting representations showcase interesting linear substructures of the word vector space.”</blockquote><p>One naive way to do word embeddings is to create a long vector of 0s for all possible words in the English language. Then, given a word, fill in a 1 in the index of the vector corresponding to that word. An example of this is as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bg8f_vNozuQI1mbzOLLFqw.png" /></figure><p>Using this example, the embedding for the word “water” would be [0,1,0,0,…,0] and the embedding for the word “table” would be [0,0,0,1,…,0]. But we can immediately see that this is not a good idea for multiple reasons”:</p><ol><li>The vector is so long! There are hundreds of thousands of words in the English language (including proper nouns) and storing each word as such a long vector would take up a huge amount of memory and make any ML algorithm very slow.</li><li>Semantic information is not preserved. This means that the index corresponding to the word does not really convey any meaning. “Balloon” and “water” are one position apart, but these are very different words.</li><li>On a similar note, the distance between two words does not give you an idea of how semantically similar these two words are.</li></ol><p>For these reasons, GloVe embeddings were developed. These were trained on a huge corpus of words in an unsupervised way, using co-occurrence of words i.e. how many times different pairs of words occur together. This gives you a sense of how related these two words are. The resulting set of embeddings have a few neat properties, which are shown in the images below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/487/1*KVGdEpJqHNIzCt5jnU5cTQ.png" /></figure><p>From the creators of the GloVe embeddings:</p><blockquote>“The underlying concept that distinguishes man from woman, i.e. sex or gender, may be equivalently specified by various other word pairs, such as king and queen or brother and sister. To state this observation mathematically, we might expect that the vector differences man — woman, king — queen, and brother — sister might all be roughly equal. This is similar for differences of vectors strong — stronger, clear-clearer etc.”</blockquote><p>We download the pre-trained Wikipedia 2014 + Gigaword 5 GloVe word embeddings which have over 400000 words in their vocabulary. The pre-trained embeddings can be downloaded from the <a href="https://nlp.stanford.edu/projects/glove/">Stanford NLP Website</a>. It is called <strong>glove.6B.zip.</strong> After unzipping In this folder, you will find several files. These contain embeddings in 50 dimensions, 100d, 200d, &amp; 300d and is an 822 MB download. To load the downloaded GloVe embeddings, follow the code below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3a854af31d84be90136505c89135b9c/href">https://medium.com/media/d3a854af31d84be90136505c89135b9c/href</a></iframe><h3><strong>Average GloVe Word Embeddings to Create Sentence Embeddings</strong></h3><p>The question now is: given that we know how to embed a single word as a vector of numbers, how do we extend this to a news headline, which is a sentence of multiple words?</p><p>An intuitive expectation for a sentence embedding is that it should capture hidden features from all words within the sentence. Therefore, we can generate embedding for a given sentence by concatenating or averaging word embeddings of its words. Concatenation, however, might not be desirable depending on the dimensionality of word embeddings. The dimension of the sentence embedding will increase multiplicatively by the number of words. In addition, concatenation would require all input sentences to have the same number of words, potentially leads to loss of important information by word reduction.</p><p>We generated sentence embeddings for article headlines and image captions by averaging pre-trained GloVe word embeddings. We do this after some pre-processing, which involves:</p><ol><li>Converting all words in a sentence to lower case</li><li>Removing unnecessary punctuation from a sentence</li><li>Removing stop words from a sentence. These include words such as ‘the’, ‘not’, ‘my’ etc. A list of stopwords has been downloaded from the WordCloud library, when we made the word clouds earlier.</li></ol><p>For implementation, see the code below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eebdf49489d10d259ca9f93fc5322445/href">https://medium.com/media/eebdf49489d10d259ca9f93fc5322445/href</a></iframe><p>To avoid repeated pairwise comparison between input article headlines and image captions, we retrieved and saved all the image caption embeddings in a matrix. The code below creates the image caption embedding matrix with GloVe embeddings:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ce0a0ec298fc7f109fbd848dd46714c/href">https://medium.com/media/1ce0a0ec298fc7f109fbd848dd46714c/href</a></iframe><h3><strong>Universal Sentence Encoder</strong></h3><p>Using averaged GloVe embeddings is certainly a decent way to get a sentence embedding, however, you may have noticed that this is a context-free approach. That is, we do not consider the order of words in a sentence.</p><blockquote>The average GloVe embedding for a sentence “a tiger eats a man” is the same as that of the sentence “a man eats a tiger”!</blockquote><p>Therefore, we need a way of using the context of a word in a sentence in order to create a more holistic set of sentence embeddings.</p><p>Introduced by Google Research in 2017 (read <a href="https://arxiv.org/abs/1803.11175">the full paper</a> here), the Universal Sentence Encoder (USE) was trained as a general-purpose, language-understanding model. The flowchart below demonstrates the training process of USE trained with Deep Average Network (DAN), which is a series of feed-forward neural networks. Given an input sentence, the DAN is trained to encode the average embedding of all words in the sentence into a 1 x N vector, which is effectively the last softmax layer of the DAN. The network weights are updated by evaluating the sentence embedding vector in different language task (e.g. sentiment analysis).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1pPrfsVcY6vpGYFRGaCMBA.png" /></figure><p>Effectively, the network can learn to capture both syntactic and semantic information solely from the average word embeddings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/1*RHyqtgqHhCIigPLnjrPyjg.png" /><figcaption>Figure source (<a href="https://arxiv.org/pdf/1803.11175.pdf">the paper on USE embeddings by Cer et al.</a>)</figcaption></figure><p>We embedded article headlines and image captions with the pre-trained USE. The model is open-sourced on tensorflow hub. For an input text of any length, USE outputs a sentence embedding of the dimension 1 x 512. The code below shows how to access the model. There is zero preprocessing required on the user end!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c03b73d55b827e8d17f2eec572eee260/href">https://medium.com/media/c03b73d55b827e8d17f2eec572eee260/href</a></iframe><p>Again, we can retrieve and save all image caption embeddings:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12de3f63ba0e7e0bfe3df7bfc38c118b/href">https://medium.com/media/12de3f63ba0e7e0bfe3df7bfc38c118b/href</a></iframe><p>Note that there are <a href="https://tfhub.dev/google/collections/universal-sentence-encoder/1">different USE models</a>, and different versions of USE models available on tensorflow hub. The models are updated with tensorflow version updates, and optimized for different language tasks. The version we employed is <a href="https://tfhub.dev/google/universal-sentence-encoder/4">version 4</a>. Alternatively, you can also explore a variation of USE trained with Transformer encoder (<a href="https://tfhub.dev/google/universal-sentence-encoder-large/5">version 5</a>). The Transformer encoder reportedly performs better on transfer learning tasks than the DAN encoder, but costs remarkable memory and computing resources.</p><h3><strong>Recommendation</strong></h3><p>In order to recommend images, we calculate the cosine similarities between the image caption embeddings and the article headline embedding. Images were then recommended based on the computed cosine similarities ranked in descending order. The higher the cosine similarity, the more semantically similar an image caption to the article headline. From the <a href="https://en.wikipedia.org/wiki/Cosine_similarity">Wikipedia page</a> on cosine similarity:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/0*ZLMVJorG53ikomGG" /></figure><p>We normalized the embedding vectors to the unit norm so that the comparison would simply be a dot product. The code below shows the recommendation process using GloVe and USE embeddings:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac3dec1659ee1e290ef1f43adddf9bd4/href">https://medium.com/media/ac3dec1659ee1e290ef1f43adddf9bd4/href</a></iframe><h3><strong>Results</strong></h3><p>Here are the recommended images by GloVe and USE embedding models. Since this dataset consists of actual news articles, we have provided the URL of the respective news article.</p><p>Article 1 headline: <a href="https://www.nytimes.com/2016/02/21/travel/walla-walla-wineries-restaurants.html">Reinvention in Walla Walla’s Wine Country</a></p><p>GloVe Predictions:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*wmbT96bPWdVs8zvR" /></figure><p>USE Predictions:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*gNm6d15druTYEuD3" /></figure><p>As you can see, both models recommend images that are related to wine or wine vineyard, which are semantically similar to “Wine” in the news article headline.</p><h3>Conclusion</h3><p>In this blog post, we demonstrated how to encode a sentence using GloVe and USE embeddings. Using the sentence embeddings, we can then compute the semantic similarity between sentences and use that to recommend images for news articles. The complete implementation is on our jupyter notebook <a href="https://github.com/phoebewong/medium_blogpost/blob/master/news_image_recommendation/ImageRecommendationNewsArticles.ipynb">here</a>.</p><h3>Acknowledgments</h3><p>We would like to acknowledge our mentors Pavlos Protopapas and Isaac Slavitt at Harvard for their guidance throughout the semester. We would also like to thank our collaborators Veronika Zielinska and David Fox at Associated Press for their support.</p><h3>References</h3><p>Biten, A. F., Gomez, L., Rusinol, M., &amp; Karatzas, D. (2019). Good News, Everyone! Context driven entity-aware captioning for news images. In <em>Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition</em> (pp. 12466–12475).</p><p>Cer, D., Yang, Y., Kong, S. Y., Hua, N., Limtiaco, N., John, R. S., … &amp; Sung, Y. H. (2018). Universal sentence encoder. <em>arXiv preprint arXiv:1803.11175</em>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5acb485a445" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/building-an-image-recommendation-system-for-news-articles-using-word-and-sentence-embeddings-a5acb485a445">Building An Image Recommendation System For News Articles using Word and Sentence Embeddings</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>