<?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 Amay Gada on Medium]]></title>
        <description><![CDATA[Stories by Amay Gada on Medium]]></description>
        <link>https://medium.com/@amaygada?source=rss-8f09eca41d44------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GqF-3tE7Moz4ZMAxpUFsxw.png</url>
            <title>Stories by Amay Gada on Medium</title>
            <link>https://medium.com/@amaygada?source=rss-8f09eca41d44------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 10:04:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amaygada/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[Sequence to Sequence models (with implementation — make a translator) — Part 2]]></title>
            <link>https://medium.com/analytics-vidhya/sequence-to-sequence-models-with-implementation-make-a-translator-part-2-49dbccd3d7d6?source=rss-8f09eca41d44------2</link>
            <guid isPermaLink="false">https://medium.com/p/49dbccd3d7d6</guid>
            <category><![CDATA[implementation]]></category>
            <category><![CDATA[attention]]></category>
            <category><![CDATA[se2seq-with-attention]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Amay Gada]]></dc:creator>
            <pubDate>Tue, 08 Feb 2022 08:15:14 GMT</pubDate>
            <atom:updated>2023-06-07T04:58:36.157Z</atom:updated>
            <content:encoded><![CDATA[<h3>Adding “attention” to Machine Translation (full python code and explanation)</h3><p>In this article, we extend the seq2seq model we built in <a href="https://amaygada.medium.com/sequence-to-sequence-models-with-implementation-part-1-73cd012520eb"><strong>part 1</strong></a><strong> </strong>and add ‘attention’ to it. I have previously covered the need of sequence to sequence models, modelling it using RNNs in <a href="https://amaygada.medium.com/sequence-to-sequence-models-with-implementation-part-1-73cd012520eb"><strong>part 1</strong></a><strong>.</strong></p><p>Note that this article has snippets of code. <strong>For the full code base</strong> <a href="https://colab.research.google.com/drive/1Fy5nYokwJR3TDek-yS8QC75WDpV_fDxa"><strong>click here</strong></a><strong>.</strong></p><p><em>I insist going through the part 1 in order to ease into part 2</em></p><h3>Why attention?</h3><p>We want the encoder to remember the important words of the input sentence which affect how the decoder decodes the encoding. For this we use <strong>Attention mechanism.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*RgF1q7ZpgvrS_JAj.jpg" /></figure><h4>Building an Intuition</h4><p>Let’s consider the following translation :</p><pre>ENGLISH<br>I have a blue cat who ate a can of dog food two days ago .</pre><pre>SPANISH<br>Tengo un gato azul que se comió una lata de comida para perros hace dos días.</pre><p>For the model to predict each spanish word in the decoder, it needs to know which english words are most affecting the next word that the decoder will predict.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/746/1*vgM3tKQd_u9CmUySW1SZ7g.png" /></figure><pre>Since we need to predict &#39;gato&#39; (cat in spanish), the english word &#39;cat&#39; has the highest weight and hence importance during prediction.</pre><pre>This otherwise would have been lost due to <strong>vanishing gradients</strong> since the word &#39;cat&#39; is at the beginning of the input sentence.</pre><h4>Getting into the details</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*epj4ErwHZcxYA1nXa2kbCg.png" /></figure><p>Consider the above model</p><pre><strong>α1 α2</strong> ... <strong>α7</strong>  -&gt;  the weights for attention<br><strong>h1 h2 ... h7  </strong>-&gt; output of each RNN layer in the encoder<br><strong>S0</strong> -&gt; Hidden state of the last RNN layer </pre><pre>[h1, h2, ... h7] =&gt; H<br>[S0]  -&gt; S</pre><h4>Computing the attention weights (alphas)</h4><p>We introduce to trainable parameters: WQ (query weight) and WK (key weight).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/145/1*d2qTGQgNBgiVdT66-gVbig.gif" /><figcaption>computing Key (can be seen as a dense layer)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/153/1*NNZppiSltMDKDLK-9l2lqQ.gif" /><figcaption>computing query (can be seen as a dense layer)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/174/1*oEOprQ1LE5XMbOynM8M3kg.gif" /><figcaption>computing the attention weights</figcaption></figure><p>Discussing Shapes</p><pre>H -&gt; (64,16,1024)<br>WK -&gt; (1024,1024)<br>K -&gt; (64, 16, 1024)</pre><pre>S -&gt; (64,1,1024)<br>WQ -&gt; (1024,1024)<br>Q -&gt; (64,1,1024)</pre><pre>alpha -&gt; (64,16,1)</pre><h4>Computing the context vector</h4><p>The context vector is multiplied with the RNN outputs (H). It can be intuitively seen as multiplying each words output with some ‘importance’ weight.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/211/1*hbeBg-oWY7t9qwwvAUEcjg.gif" /><figcaption>computing the context vector</figcaption></figure><h4>Moving on to the Decoder</h4><p>The word embedding for the current word in the decoder (E) is concatenated with the Context Vector (C). This joint vector is used as the hidden state in the RNN.</p><p>Note that before each new word is predicted by the decoder, a new context vector is computed.</p><h4>Coding up the Dot Product Attention</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2dc7de3065ed642429e52336d4d694b0/href">https://medium.com/media/2dc7de3065ed642429e52336d4d694b0/href</a></iframe><h3>Building the decoder with Attention</h3><h4>Code</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a673b1166829f2399e18b0b646d081d/href">https://medium.com/media/4a673b1166829f2399e18b0b646d081d/href</a></iframe><h3>Training</h3><h4>Code</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5478306d928ccfe3cf9aeb16c5431aa2/href">https://medium.com/media/5478306d928ccfe3cf9aeb16c5431aa2/href</a></iframe><h3>Results</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/445/1*VtLLVyz5Cwr0JPJZYWPGaA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*6bmwaZKlRicWxOLVuHhYdg.png" /></figure><h3>Translate</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5a7f5746d97d693bc0a3c009cabee91/href">https://medium.com/media/b5a7f5746d97d693bc0a3c009cabee91/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/421/1*QKwikJnJ9saseOaD9GDXPA.png" /></figure><h3>Do we really need RNNs?</h3><h4>What does attention offer?</h4><ol><li>Takes into account all the input words (Bidirectional, sequential RNNs)</li><li>Computes a context vector that tries to solve vanishing gradients (GRUs/LSTMs)</li><li>Allows parallel computation of weights during forward and backward propogation as only dense layers are involved.</li></ol><pre>We see that attention can do a lot of things RNNs can do.<br>Hence it is worth to see an experimental setting where we remove RNNs entirely and use only attention.</pre><pre>It is in fact proven that Attention performs faster and much better than RNNs. </pre><pre>This research work was done in a paper called Attention is all you need.</pre><pre>We&#39;ll naively implement pure attention in <strong>Part 3 (COMING SOON!)</strong></pre><h3>For the entire Code Base visit</h3><p><a href="https://colab.research.google.com/drive/1Fy5nYokwJR3TDek-yS8QC75WDpV_fDxa">Google Colaboratory</a></p><h3>References</h3><ol><li><a href="https://www.youtube.com/watch?v=pLpzU-xGi2E&amp;t=3028s">https://www.youtube.com/watch?v=pLpzU-xGi2E&amp;t=3028s</a></li><li><a href="https://github.com/YanXuHappygela/NLP-study/blob/master/seq2seq_with_attention.ipynb">https://github.com/YanXuHappygela/NLP-study/blob/master/seq2seq_with_attention.ipynb</a></li><li><a href="https://www.youtube.com/watch?v=B3uws4cLcFw&amp;list=PLgtf4d9zHHO8p_zDKstvqvtkv80jhHxoE">https://www.youtube.com/watch?v=B3uws4cLcFw&amp;list=PLgtf4d9zHHO8p_zDKstvqvtkv80jhHxoE</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=49dbccd3d7d6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/sequence-to-sequence-models-with-implementation-make-a-translator-part-2-49dbccd3d7d6">Sequence to Sequence models (with implementation — make a translator) — Part 2</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sequence to Sequence models (with implementation) — Part 1]]></title>
            <link>https://medium.com/analytics-vidhya/sequence-to-sequence-models-with-implementation-part-1-73cd012520eb?source=rss-8f09eca41d44------2</link>
            <guid isPermaLink="false">https://medium.com/p/73cd012520eb</guid>
            <category><![CDATA[seq2seq]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[translator-app]]></category>
            <category><![CDATA[rnn]]></category>
            <dc:creator><![CDATA[Amay Gada]]></dc:creator>
            <pubDate>Sat, 05 Feb 2022 16:25:55 GMT</pubDate>
            <atom:updated>2023-06-07T04:56:28.872Z</atom:updated>
            <content:encoded><![CDATA[<h3>Machine translation in python (with code and explanation)</h3><p>Note that this article has snippets of code. <strong>For the full code base</strong> <a href="https://colab.research.google.com/drive/1Fy5nYokwJR3TDek-yS8QC75WDpV_fDxa"><strong>click here</strong></a><strong>.</strong></p><h3>Need of sequence of sequence models</h3><p>Sequence to Sequence models are encoder-decoder networks, whose architecture can be leveraged for complex machine learning tasks like Machine Translation, Text <a href="https://www.vocabulary.com/dictionary/summarization#:~:text=summarization%20Add%20to%20list%20Share,court%20is%20called%20a%20summation.">Summarization</a> and chat bots.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/525/1*RiRBLweuqK7_t8wROBluXQ.jpeg" /><figcaption>Seq2Seq models in a nutshell</figcaption></figure><h3>Preprocessing</h3><h4>Guidelines</h4><ol><li>Remove special characters (@, %, #, …)</li><li>Add space between punctuation and words. (Good morning! — &gt; Good morning !)</li><li>Add &lt;start&gt; and &lt;end&gt; tokens to each sentence</li><li>Create word index and reverse word index (using the keras tokenizer)</li><li>Pad each sentence to maximum length</li></ol><h4>Sample Input</h4><blockquote>esta es mi vida.</blockquote><h4>Sample output</h4><blockquote>&lt;start&gt; esta es mi vida . &lt;end&gt;</blockquote><h4>Code</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37e5b66bba24557338959e45a2290936/href">https://medium.com/media/37e5b66bba24557338959e45a2290936/href</a></iframe><h3>Modelling the network</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*w9HuOm0pp-KKJxIYukSuPg.jpeg" /></figure><h4>Discussing forward propogation</h4><ol><li>Convert all words in source sentence to one hot vectors depending on the vocab size</li><li>Put the one hot vectors through a trainable 256 dimension embedding layer (<a href="https://medium.com/analytics-vidhya/a-mathematical-dive-into-word-embeddings-5c06f2fd9bbe"><strong>Click here</strong></a><strong> </strong>to know more about embedding layers and word vectors)</li><li>The embedded vectors are passed through RNN layers (1024 units).</li><li>An encoding of the source sentence is generated.</li><li>Encoding is passed to the decoder and it acts as the hidden state for the decoder RNN.</li><li>The decoder is trained by passing one word at a time.</li><li>The model is expected to predict the next word, which is passed to the RNN layer. (The actual word is passed during training, while the predicted word is passed during prediction)</li><li>The model stops once it encounters the &lt;end&gt; token or the max length of the target sentence as determined by the dataset is reached.</li></ol><h4>Discussing matrix shapes</h4><p>Interestingly, not a lot of people discuss matrix shapes while discussing modelling and I feel that it is a very important part of understanding networks in ML.</p><p>The letters in curved brackets () indicate the layer. Using these letter denotations, we shall look into matrix shapes.</p><p>We’ll also consider batch size (64) to avoid confusion when going through the code</p><p>let max length of source sentence = 16 (after padding)</p><p>Number of units in RNN = 1024</p><pre>{we will consider all 16 together because we have the entire source sentence}<br>A -&gt; (64, 16, Vocab_size)<br>B -&gt; (64, 16, 256)<br>C -&gt; (64,1,1024)<br>Hi -&gt; (64, 1, 1024)</pre><pre>{we will consider each word one by one in the decoder as we use the previous word to form the next word. Hence we will not use the max length here.}<br>D -&gt; (64, 1, Vocab_size)<br>E -&gt; (64, 1, 256)<br>F -&gt; (64,1,1024)<br>G -&gt; (64, 1, Vocab_size)  {apply softmax here to get output word}</pre><h4>Sample input</h4><blockquote>&lt;start&gt; esta es mi vida . &lt;end&gt;</blockquote><h4>Sample output</h4><blockquote>&lt;start&gt; this is my life . &lt;end&gt;</blockquote><h4>Coding the Encoder Network</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9b41f19ea887eba88f8904a22c34194/href">https://medium.com/media/c9b41f19ea887eba88f8904a22c34194/href</a></iframe><h4>Coding the Decoder Network</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e5e90c79d5962b20f33ff4777509784/href">https://medium.com/media/0e5e90c79d5962b20f33ff4777509784/href</a></iframe><h3>Loss Function</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f60443f4e4fdb58c115898ade0893fe2/href">https://medium.com/media/f60443f4e4fdb58c115898ade0893fe2/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/206/1*lis_IOBk0EBGFnk3whr4aw.gif" /><figcaption>Loss function</figcaption></figure><h3>Training</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/35bf4549d54ba888c5b2ece91eab2d7e/href">https://medium.com/media/35bf4549d54ba888c5b2ece91eab2d7e/href</a></iframe><h3>Results</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*8rhlslqvANoK-0of3BnV0w.png" /><figcaption>result section</figcaption></figure><h3>Translate</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab8326a9911de3f54451f1f8f543ec3e/href">https://medium.com/media/ab8326a9911de3f54451f1f8f543ec3e/href</a></iframe><pre>Input:                     &lt;start&gt; ¿ todavia estan en casa ? &lt;end&gt; <br>Predicted translation:     are you at home ? &lt;end&gt;</pre><h3>Problem with the current Encoder Decoder Network</h3><p>By the time the encoder encodes the last word in the input sentence, a lot of the information related to the previous words is lost due to <strong><em>vanishing gradients</em></strong></p><p>This usually affects longer input sentences.</p><h4>Solution</h4><p>We want the encoder to remember the important words of the input sentence which affect how the decoder decodes the encoding. For this we use <strong>Attention mechanism.</strong></p><p>Detailed description on attention in <a href="https://amaygada.medium.com/sequence-to-sequence-models-with-implementation-make-a-translator-part-2-49dbccd3d7d6"><strong>Part 2</strong></a></p><h3>For the entire code base visit:</h3><p><a href="https://colab.research.google.com/drive/1Fy5nYokwJR3TDek-yS8QC75WDpV_fDxa">Google Colaboratory</a></p><h3>References</h3><ol><li><a href="https://www.youtube.com/watch?v=pLpzU-xGi2E&amp;t=3028s">https://www.youtube.com/watch?v=pLpzU-xGi2E&amp;t=3028s</a></li><li><a href="https://github.com/YanXuHappygela/NLP-study/blob/master/seq2seq_with_attention.ipynb">https://github.com/YanXuHappygela/NLP-study/blob/master/seq2seq_with_attention.ipynb</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=73cd012520eb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/sequence-to-sequence-models-with-implementation-part-1-73cd012520eb">Sequence to Sequence models (with implementation) — Part 1</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Mathematical dive into word embeddings]]></title>
            <link>https://medium.com/analytics-vidhya/a-mathematical-dive-into-word-embeddings-5c06f2fd9bbe?source=rss-8f09eca41d44------2</link>
            <guid isPermaLink="false">https://medium.com/p/5c06f2fd9bbe</guid>
            <category><![CDATA[word-embeddings]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[Amay Gada]]></dc:creator>
            <pubDate>Sat, 10 Jul 2021 09:39:07 GMT</pubDate>
            <atom:updated>2021-07-30T11:38:34.422Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>A Mathematical dive into word embeddings (skip grams)</strong></h3><h3><strong>Why word embeddings?</strong></h3><p>Let us consider that our NLP model learns the phrase:</p><blockquote>That is a beautiful painting</blockquote><p>It would be tough for it to predict the word <em>‘painting’</em> in the phrase - That is a gorgeous ________. <br>This is because there is no direct relationship between the words <em>beautiful</em> and <em>gorgeous. </em>So the next question is, who do we have to blame for it?</p><h4><strong>One Hot Vectors</strong></h4><p>Earlier, each word was described by a one-hot vector of the entire corpus of words (called the vocabulary). Hence, a million word corpus would cause each word to be a million dimensions long.</p><h4><strong>Word Embeddings</strong></h4><p>As you may have figured, the main objective is to build an encoding for words that encapsulate certain relationships between them. These encodings are called as word embeddings. In case you are looking for an intuition for the same, we aim to create embeddings, such that the words having similar contexts cluster together (cosine similarity can be used to find how close two word embeddings are).<br>Moreover, this helps in reducing the dimensionality of the input vectors as well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*DEbs5tWie25uSNVqxxt8yg.gif" /></figure><h3><strong>How to generate word embeddings?</strong></h3><p>Hopefully, now you have a general idea of what we are trying to do.</p><p>Now comes the time to ponder upon how we may actually go about generating these encodings!</p><p>Let’s begin with <strong><em>skip grams</em></strong>.</p><h4><strong>Skip Grams</strong></h4><p>The skip-gram neural network model is actually surprisingly simple in its most basic form. It involves training a neural network with one hidden layer. However, equally important and interesting as the architecture, is how we use it to generate word embeddings.</p><p>Initially we try to solve a completely different problem. From a large corpus of English text, we keep on choosing phrases whose lengths depend on a previously chosen window size. From this window, we choose a central word and call it the target. Rest of the words in that phrase are called the context. We build a model such that given a target word, we predict its context.</p><blockquote>I want two <strong><em>slices</em></strong> of mozzarella pizza<br>window size = 7<br>Wi -&gt; slices<br>Wo -&gt; I, want, two, of , mozzarella, pizza</blockquote><blockquote>P(Wo|Wi) -&gt; probablity of getting a context word Wo given the input word Wi</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/245/1*DZxkGxGTEUXGr2iZ6lpYPQ.png" /><figcaption>objective of the neural network — (eq 1)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/440/1*Za6uasyG__qovOJyExzmAQ.png" /><figcaption>skip gram model architecture — (fig 1)</figcaption></figure><pre>Shape of W1 = (300, 10000)<br>Shape of W2 = (10000, 300)</pre><pre>Hidden = W1.X (300,1)<br>(Note that no activation function is applied here and neither is a bias term involved)</pre><p>Looking at the architecture, it is trivial to see how it maximises the objective of getting context words corresponding to target words. The question is, how does it help us generate word embeddings?</p><p>The answer lies in the hidden layer weight matrix W1.</p><p>While learning different contexts for a target word, we are in a way developing a model that encapsulates the relationships between the target and the context. Hence, we could come to a conclusion that the hidden layer is in fact the word embedding for a given input word. However, we need the word embeddings for all the words in the corpus. Hence, we turn to the weight matrix W1.</p><p>W1 is of shape (300, 10000). A matrix multiplication with a one-hot input vector of shape (10000, 1) would render a vector with shape (300,1). Given that the input is a one-hot encoded vector, only the node with value 1 would play a role in the matrix multiplication. The other nodes being 0, would contribute nothing.</p><p>Therefore, we could say that a word corresponding to the 10th element in the one-hot encoded vector, would have the 10th column of the W1 matrix as its word embedding. (think about this a little)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/165/1*pqR0VDcnphV9xmzYC_nffQ.png" /><figcaption>(fig 2)</figcaption></figure><h4>The softmax layer</h4><p>The details about the output softmax layer were kept vague deliberately. Now is the time where we unveil the mystery behind that. To understand this it is necessary to understand that we are actually learning two word embeddings; One when the word is the target and one when the word is the context. The former is given by the weight matrix W1 and the latter is given by the weight matrix W2.</p><pre>e1 = W1 . I      [I is the input vector]<br>e2 = (W2)T . O   [O is the one hot vector of the softmax argmax]</pre><p>For each input vector, the hidden layer learns 300 weights (Vwi) corresponding to the weight matrix W1. <strong><em>Vwi</em></strong> is then multiplied by the weight matrix W2 in order to give a softmax output. Responsible for each node in the softmax output, we can say that there exist 300 (Uwj) parameters from the W2 matrix (for each output word j). According to what we saw earlier, <br>Vwi = e1 and Uwj = e2. Using this we can finally say that the dot product:<br><strong><em>(Vwi)T . Uwj </em></strong>plays a role in calculating the decimal probablity at the output node j.</p><p><strong>Note: </strong>The above calculation is for one input vector Vwi</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*nblz2MoSK5kUrohnDShMlw.png" /><figcaption>Diagram for visualising Vwi and Uwj(fig 3)</figcaption></figure><p>Using this we can say that the softmax function for each input looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/216/1*dwE-9HUWCMtjYUUqp6GWiQ.png" /><figcaption>(eq 2)</figcaption></figure><h4><strong>The Cost Function</strong></h4><p>To define the cost function we must go back to when we actually defined our objective. Our goal is to maximize the probablity of getting context words given a target word as described in equation 1. Let’s talk a little more about it.</p><p>Firstly, let’s answer the question: <em>Why a multiplication operation?</em> <br>As in all machine learning problems, we assume stuff. Here we assume independence. i.e all context target pairs are independent of each other (just like a coin flip!)</p><p>This gives us something like this:</p><blockquote>P(w1,w2,w3 | Wi) = P(w1|Wi) . P(w2|Wi) . P(w3|Wi)</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*PaWHZFbv9f3JIkuZEwO3HQ.png" /></figure><p>Moreover do you see something missing?</p><p>Equation 1 only considers one window of size 2c+1. Whereas, we need to consider all the windows in the entire english text corpus. Hence the equation 1 must be extended to all the T windows that we encounter.</p><p>Finally, for the sake of easier differentials and preventing underflows, we take the log of the objective function. This leaves us with:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/373/1*qY5-bD3tmzzX6QTVrMnr7A.png" /><figcaption>We divide by T so that the cost function is not affected by the size of the corpus — (eq 3)</figcaption></figure><p>The probablity P can be calculated using the equation 2</p><h4><strong>Computational Issues</strong></h4><p>The skip gram model works well for smaller vocabulary sizes. However, this is not always the case. Vocabulary sizes can easily scale up to millions of words and this poses a serious threat to the skip gram model.</p><p>To understand this better, we must go back to equation 2 and have a look at the summation in the denominator. A vocabulary of around 10 million would easily make the computation infeasible. Moreover, the summation of 10 million has to be done for every context-target pair existing in the corpus. This causes the skip gram model to be very slow.</p><p>So the next plausible question is: <em>How do we fix it?</em></p><p>There are a few ways in which we can reduce this complexity.<br>1. Sub sampling<br>2. Noise contrastive estimation<br>3. Negative sampling<br>4. Hierarchial softmax</p><p>The in depth explanation for the above solutions is out of scope of this blog post. Hopefully I will write about them in the near future and link them here.</p><h3><strong>References</strong></h3><ol><li><a href="https://www.youtube.com/watch?v=G5ah0QqkUgE&amp;list=PLxlkzujLkmQ8erDs5VSteKTmVg-y9Z1dR&amp;index=8">https://www.youtube.com/watch?v=Co-YYk0731I&amp;list=PLxlkzujLkmQ8erDs5VSteKTmVg-y9Z1dR&amp;index=7</a></li><li>Tomas Mikolov et al., Efficient Estimation of Word Representations in Vector Space 2013</li><li>Andrew NG (week 2 of course 5 of the deep learning.ai series)</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5c06f2fd9bbe" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/a-mathematical-dive-into-word-embeddings-5c06f2fd9bbe">A Mathematical dive into word embeddings</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>