<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shashank-Holla</title>
    <description>The latest articles on DEV Community by Shashank-Holla (@shashankholla_10).</description>
    <link>https://dev.to/shashankholla_10</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F529203%2F0b64ca99-8cfb-43e2-b22b-95c58cfbe563.png</url>
      <title>DEV Community: Shashank-Holla</title>
      <link>https://dev.to/shashankholla_10</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shashankholla_10"/>
    <language>en</language>
    <item>
      <title>Attention in Seq2Seq models</title>
      <dc:creator>Shashank-Holla</dc:creator>
      <pubDate>Thu, 04 Feb 2021 19:39:24 +0000</pubDate>
      <link>https://dev.to/shashankholla_10/attention-in-seq2seq-models-11ik</link>
      <guid>https://dev.to/shashankholla_10/attention-in-seq2seq-models-11ik</guid>
      <description>&lt;p&gt;In this post, we will discuss about transformer model, an attention based model which has significant boost in model training speed. In this sequence processing model, there is no recurrent layers or convolution layers being used. Instead, it is made of attention and fully connected layers.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Convolutional Sequence to Sequence learning - A Closer Look</title>
      <dc:creator>Shashank-Holla</dc:creator>
      <pubDate>Thu, 28 Jan 2021 19:17:34 +0000</pubDate>
      <link>https://dev.to/shashankholla_10/convolutional-sequence-to-sequence-learning-a-closer-look-bdn</link>
      <guid>https://dev.to/shashankholla_10/convolutional-sequence-to-sequence-learning-a-closer-look-bdn</guid>
      <description>&lt;p&gt;In this post we take a closer look into Convolution based Sequence to Sequence machine translation. &lt;/p&gt;

&lt;p&gt;In simple words, Machine translation is the translation of texts from one language to another. Sequence to sequence (Seq2Seq), an encoder-decoder based architecture, is used to convert sequences from the source language to sequences in target language. Widely known approach for sequence to sequence translation is via recurrent neural network. Compared to RNN networks, convolution based network are less common but have certain advantages. In this post, we will look at the inner working of the encoder-decoder modules. We are considering Pytorch based implementation of German to English language translation as an example in this post.&lt;/p&gt;

&lt;p&gt;Source code for this available &lt;a href="https://github.com/Shashank-Holla/TSAI-END-Program/tree/main/11-%20HandsOn%205"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Compared to recurrent networks, convolutional models create fixed size context representations and this can be made larger by stacking convolutional layers on top of each other. This allows CNN control over the maximum length of of dependencies to be modeled. Similar to the idea of edges and gradients to textures to patterns to parts of objects being captured in multiple convolution blocks in image  domain, Multi-layered CNN network creates hierarchical representation over the input sentence where nearby input elements interact at lower layers and distant elements interact at higher layers. Computations in convolutional network is applied over all elements in parallel in training and exploits the GPU hardware.&lt;/p&gt;

&lt;p&gt;Seq2seq model employs Encoder-Decoder architecture for translation. Encoder's role is to encode the input sentence which is in the source language into a context vector. Encoder produces two context vectors per token. So if the input German sentence has 4 tokens, it would produce 8 context vectors. The two context vectors produced by the encoder are 'conved vector' and 'combined vector'. Conved vector is produced by passing each token through a few fully connected layers and the convolutional block. Combined vector is the sum of the convolved vector and the embedding of the token. &lt;br&gt;
Decoder's role is to use the context vector to produce the output sentence in the target language. Decoder model, unlike recurrent models, predicts all the tokens in parallel in the target sentence. We'll look into the encoder and decoder's working seperately.&lt;/p&gt;

&lt;h2&gt;
  
  
  Encoding process
&lt;/h2&gt;

&lt;p&gt;Below is the architecture diagram of the encoder block. We'll look into each segment of this block in detail.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zncTPnRD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/98ufl810tjhvz15xd0x7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zncTPnRD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/98ufl810tjhvz15xd0x7.png" alt="Encoder"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Embedding vector
&lt;/h4&gt;

&lt;p&gt;During data pre-processing, the input sentence in the source language was tokenized and indexed. Now, in the encoding layer, these tokens are passed through Embedding layer to create word embeddings. Unlike recurrent networks which processed each token sequentially, the CNN based model requires all the tokens to be processed simultaneously. Therefore, the model does not possess information about the position of the tokens within the sequence. To rectify this, information about the position of each token is passed along with the token embedding. This is done by passing the position of the token through the embedding layer to create positional embedding. This is then elementwise summed to create the embedding vector.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Fully connected layer-1
&lt;/h3&gt;

&lt;p&gt;Embedding vector is now passed through a fully connected layer. This adds capacity to the model as wells as transforms the dimension of the embedding vector with the required hidden dimension size.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Convolutional block
&lt;/h3&gt;

&lt;h4&gt;
  
  
  a) Convolution
&lt;/h4&gt;

&lt;p&gt;During convolution, kernel takes in n words from a sentence equal to kernel size and convolves over them to produce a feature map. Here, the length of the sentence after convolution reduces by (kernel size - 1). To maintain the length of the input sentence after convolution to be the same as before covolution, the input sentence is padded with padding element on each end of the sentence. The padding amount on each side is equal to (kernel size -1)/2. This is passed as argument in convolution function.&lt;/p&gt;

&lt;h4&gt;
  
  
  b) GLU activation
&lt;/h4&gt;

&lt;p&gt;The convolution output is then passed through a activation function called Gated Linear Units (GLU). GLU function splits the input evenly into two tensors, calculates sigmoid for the second tensor and multiplies elementwise by the first tensor. As per the authors, gating mechanism allows selection of words or features that are important for predicting the next word. GLU splits and reduces the hidden dimension by a factor of 2. Therefore the hidden dimension size is doubled during convolution so as to maintain the hidden dimension size through GLU activation. &lt;/p&gt;

&lt;h4&gt;
  
  
  c) Residual Addition
&lt;/h4&gt;

&lt;p&gt;Similar to residual path in resnet, output from GLU activation is elementwise summed with the same vector before it was passed through convolution layer.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Fully connected layer-2
&lt;/h3&gt;

&lt;p&gt;Vector from the convolutional block is now fed into fully connected layer. This again adds capacity to the model and transforms the vector back from hidden dimension to embedding dimension.  This vector is called conved vector.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Residual layer
&lt;/h3&gt;

&lt;p&gt;Conved vector is elementwise summed with embedding vector via residual connection which bypassed the convolution block. This new vector is called combined vector.&lt;/p&gt;

&lt;p&gt;Conved vector and combined vector is generated for each token in the input sentence.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Decoder process
&lt;/h2&gt;

&lt;p&gt;The decoder takes in the actual target word and tries to predict it. Shared below, are the segments that make up the decoder block.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vgIsI0gy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lz0l80qprvetzk8o1ph6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vgIsI0gy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lz0l80qprvetzk8o1ph6.png" alt="Decoder"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Embedding vector
&lt;/h3&gt;

&lt;p&gt;As in encoder, embedding is calculated for the target tokens and the positions and elementwise summed.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Fully connected layer-1
&lt;/h3&gt;

&lt;p&gt;Embedding vector is passed through fully connected layer which converts the input from embedded dimension to hidden dimension.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Convolution block
&lt;/h3&gt;

&lt;h4&gt;
  
  
  a) Convolution
&lt;/h4&gt;

&lt;p&gt;Unlike in encoder where padding was applied equally on both ends of the sentence, padding in decoder is applied only at the beginning of the sentence. This is to ensure the kernel only looks at the previous and current word for processing and prevent it from looking at the next word (token that needs to be predicted). This helps in preventing the model from copying the next word and not learning to translate. Apart from this change, the convolution is same as of encoder's.&lt;/p&gt;

&lt;h4&gt;
  
  
  b) GLU activation and attention
&lt;/h4&gt;

&lt;p&gt;GLU activation is similar to the one applied in encoder. But, after the GLU activation, attention is calculated by using encoder output as well as the embedding of the current word. The convolution output's dimension is transformed by a fully connected layer and then summed up with its embeddings through a residual connection. Then attention is calculated on this combination by checking how much it matches with the encoder's convolution output. This is done by first calculating the energy by taking a dot product of the convolution output with encoder's convolution output. Softmax function is then applied on the product to calculate the attention. Dot product of the attention with  encoder's convolution output provides more information about the specific token of the encoded sequence which is very useful in making prediction.&lt;/p&gt;

&lt;h2&gt;
  
  
  References:
&lt;/h2&gt;

&lt;p&gt;1) Seq2Seq Pytorch tutorial - &lt;a href="https://github.com/bentrevett/pytorch-seq2seq"&gt;https://github.com/bentrevett/pytorch-seq2seq&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Convolutional Sequence to Sequence Learning - &lt;a href="https://arxiv.org/pdf/1705.03122.pdf"&gt;https://arxiv.org/pdf/1705.03122.pdf&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>Call for your attention! Do you remember LSTM?</title>
      <dc:creator>Shashank-Holla</dc:creator>
      <pubDate>Thu, 03 Dec 2020 18:40:29 +0000</pubDate>
      <link>https://dev.to/shashankholla_10/remember-lstm-3mla</link>
      <guid>https://dev.to/shashankholla_10/remember-lstm-3mla</guid>
      <description>&lt;p&gt;&lt;em&gt;This is my attempt to jot down idea behind Recurrent Neural network variants such as LSTM and GRU and the thought process behind attention mechanism.&lt;/em&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Idea behind RNN
&lt;/h3&gt;

&lt;p&gt;Deep Learning has provided many solutions to interesting problems in computer vision, speech and audio domains. One such weapon from Deep Learning's arsenal to tackle sequence data such as text, audio is Recurrent neural networks. Recurrent Neural network allows to extract gist behind texts of data (sentiment analysis), annotate sequences (image captioning) or even generate new sequences (language translations). Recurrent network posesses ability to persist information that allows us to operate on sequence of input to produce sequence of output vectors.&lt;/p&gt;

&lt;h3&gt;
  
  
  Need for LSTMS, GRUs
&lt;/h3&gt;

&lt;p&gt;The persistence of information in RNN is made possible by loops in neural network. When RNN makes its prediction, it considers the current input as well as the learnings of the past inputs. To decipher this, RNN can be viewed as unrolled version of the same network, with learning from each time step passed to the next. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TjMZjkBY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l3e6sw1ekg8ioa6bx7ah.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TjMZjkBY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l3e6sw1ekg8ioa6bx7ah.jpg" alt="RNN"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;RNN network performs really well for problems with short contexts. But, as contexts gets longer and as the time steps increases, the network fails to carry forward the learnings from the initial stage. Hence, neural network suffers from Short term memory caused by vanishing gradients. Remember that the network learns by calculating gradients and adjusting internal weights. But, when the calculated gradients become quite small, hence vanishing gradients, the network fails to learn anything meaningful. As an example, consider a long sentence that says - &lt;/p&gt;

&lt;p&gt;&lt;em&gt;The General's method is to have his troops ready by dawn.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Our recurrent network might find it hard to decide the context of the word General and if its used as an adjective/noun in our sentence. LSTM and GRU tries to solve these problems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Talking about LSTM
&lt;/h3&gt;

&lt;p&gt;The core idea behind LSTM (Long Short Term memory) is the cell state, a pathway that transfers relative information all the way down the sequence chain. Its working is somewhat similar to the skip connection that is used in &lt;strong&gt;Resnet&lt;/strong&gt; models. Its very easy for information to travel along unchanged through the sequence. LSTM provides means to add or remove information to the cell state by structures called gates. LSTM has three such gates- input gate, forget gate and output gate to learn over time what information is important. Cell state and hidden state from previous timestep and input are processed by these gates.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5sGvgxu1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7r4btprmc6q1mtzg0c3n.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5sGvgxu1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7r4btprmc6q1mtzg0c3n.jpg" alt="LSTM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first contributor in LSTM is the forget gate. It's role is to decide the part of the cell state's information that needs to be thrown away or kept. Sigmoid function is used in these gate and it acts like a selector/controller to selectively remove few features from the embedding vector. In our example, forget gate looks at the previous hidden state h(t-1) and input x(t) and would decide to forget the adjective sense of the word 'general'.&lt;/p&gt;

&lt;p&gt;The second contributor is the input gate which decides the part of the input's information that needs to be added to the cell state. Here, a sigmoid function selects the part of the input that is to be updated. Next, a tanh function creates new candidate vector from the inputs. By tanh characteristics, the new vectors are squished within the range -1 to 1 which regulates the network and prevents possible gradient explosions. By elementwise multiplication, the sigmoid layer decides which of the features of the new candidate vector is important. In our 'General' example, the model might decide the word is being used as a noun and add this part of speech to the cell state.&lt;br&gt;
With forget gate deciding the part of previous cell state to be forgotten and input gate deciding the part of the input to be added, the new cell state is calculated.&lt;/p&gt;

&lt;p&gt;The last contributor is the output gate which now decides the next hidden state. The output gate provides the filtered version of the new cell state. It again uses the sigmoid function to decide the important features of the cell state and tanh function to squash the new cell state between the desired range. In the general example, this would amount to the model deciding adjective as the right part of speech to assist in further timesteps.&lt;/p&gt;

&lt;h3&gt;
  
  
  Talk about GRU
&lt;/h3&gt;

&lt;p&gt;Gated Recurrent Network (GRU) is similar to LSTM but with few modifications. GRU's doesn't have cell state and uses only the hidden state to transfer information. It uses two gates- Update gate and Reset gate rather than the 3 gates that were used in LSTM.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dRBgH7DF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wbckmis7bdotupu4r6wu.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dRBgH7DF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wbckmis7bdotupu4r6wu.jpg" alt="GRU"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first gate is Update gate which acts similar to the forget gate and input gate of LSTM. It helps the model determine how much of past information needs to be passed along.&lt;/p&gt;

&lt;p&gt;The second gate is the Reset gate which decides how much of the past information to forget.&lt;/p&gt;

&lt;h3&gt;
  
  
  Differences and which is better?
&lt;/h3&gt;

&lt;p&gt;GRU doesn't possess internal memory (hidden state) compared to LSTM. GRU also has 3 internal neural networks compared to LSTM which has 4 such networks. Hence, GRU has fewer tensor operations and are little speedier to train than LSTM. However, neither of the model outweighs the other. In cases of training on less data or on need for speed, GRU can be considered. While in cases of longer sequences and to maintain long distance contexts LSTM might be preferred.&lt;/p&gt;

&lt;h2&gt;
  
  
  Attention
&lt;/h2&gt;

&lt;p&gt;For problems such as machine translation which requires many to many mapping, encoder-decoder based RNN models are used. In traditional RNN encoder-decoder architecture, the encoder provides a single context vector to the decoder. The problem with this approach is that it stresses and requires the encoder to provide a utopic vector that provides all the information that the decoder requires to decipher, which is a complex task for both. &lt;/p&gt;

&lt;p&gt;Attention in RNN is a mechanism that provides insight to the decoder to focus on certain part of the input sequence to predict certain part of the output. This part of the post focuses on the idea behind attention and how it tries to solve the above problem.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QWmnRpg7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/spn8u4yf9unnwfmslgin.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QWmnRpg7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/spn8u4yf9unnwfmslgin.jpg" alt="attentionmechanism"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the attention mechanism model, all the hidden states from the encoder are offered for decoder's consideration. In our example, hidden vectors are [h1, h2, h3, h4] while si-1 is the state of the decoder. For the current timestep calculation, the previous decoder state is considered. Attention mechanism begins with attention weights calculation. si-1 (previous decoder state) is concatenated with the hidden state vectors and fed to a shallow fully connected layer. Later, softmax function is applied to the output of fc which ensures the outputs sums up to 1. Now the hidden state vectors [h1, h2, h3, h4] are scaled by the attention weights to produce the context vectors. Due to softmax nature of the output, Context vectors captures the degree of relevance of the hidden vector. If the score is close to 1, then the decoder is heavily influenced by that particular hidden state. With this approach, encoder is relieved of the burden to encode all information into single hidden vector and provides the decoder with greater context to predict.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
