<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Deep Learning on &lt;raj.sh&#39;log&gt;</title>
    <link>https://shalemrajkumar.github.io/tags/deep-learning/</link>
    <description>Recent content in Deep Learning on &lt;raj.sh&#39;log&gt;</description>
    <generator>Hugo -- 0.148.2</generator>
    <language>en</language>
    <atom:link href="https://shalemrajkumar.github.io/tags/deep-learning/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>RNN Documentation</title>
      <link>https://shalemrajkumar.github.io/mydocs/rnn/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://shalemrajkumar.github.io/mydocs/rnn/</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;[!WARNING]
This is a work in progress and may contain inaccuracies or incomplete information.&lt;/p&gt;&lt;/blockquote&gt;
&lt;h2 id=&#34;introduction&#34;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;RNNs are first working initiative to process sequential data by maintaining a hidden state that captures information about previous elements (some early varient of context) in the sequence. They are widely used in various applications such as language modeling, speech recognition, and time series prediction.&lt;/p&gt;
&lt;p&gt;Different types of sequential data include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Text data (e.g., sentences, documents)&lt;/li&gt;
&lt;li&gt;Time series data (e.g., stock prices, weather data)&lt;/li&gt;
&lt;li&gt;Audio data (e.g., speech signals, music)&lt;/li&gt;
&lt;li&gt;Video data (e.g., frames in a video sequence)&lt;/li&gt;
&lt;li&gt;Biological sequences (e.g., DNA, protein sequences)&lt;/li&gt;
&lt;li&gt;sequence of actions (e.g., user behavior, robot movements)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Different ways to implement RNNs include:&lt;/p&gt;</description>
      <content:encoded><![CDATA[<blockquote>
<p>[!WARNING]
This is a work in progress and may contain inaccuracies or incomplete information.</p></blockquote>
<h2 id="introduction">Introduction</h2>
<p>RNNs are first working initiative to process sequential data by maintaining a hidden state that captures information about previous elements (some early varient of context) in the sequence. They are widely used in various applications such as language modeling, speech recognition, and time series prediction.</p>
<p>Different types of sequential data include:</p>
<ul>
<li>Text data (e.g., sentences, documents)</li>
<li>Time series data (e.g., stock prices, weather data)</li>
<li>Audio data (e.g., speech signals, music)</li>
<li>Video data (e.g., frames in a video sequence)</li>
<li>Biological sequences (e.g., DNA, protein sequences)</li>
<li>sequence of actions (e.g., user behavior, robot movements)</li>
</ul>
<p>Different ways to implement RNNs include:</p>
<ul>
<li>point neurons based RNNs (optimized with backpropagation through time)</li>
<li>spiking neurons based RNNs (optimized with surrogate gradient descent)</li>
</ul>
<p>Different architectures of RNNs include:</p>
<ul>
<li><a href="#vanilla-rnn">Vanilla RNNs</a></li>
<li><a href="#long-short-term-memory">Long Short-Term Memory (LSTM) networks</a></li>
<li>Gated Recurrent Units (GRU)</li>
<li>Bidirectional RNNs</li>
<li>Deep RNNs</li>
</ul>
<p>RNNs in biological neural systems are core of most cognitive function, they are responsible for information processing, maintaining working memory, and generating temporal patterns of activity that facilitate learning. Feedback is basic subunit of intelligence in biological systems!</p>
<p>Neuroscience models:</p>
<ul>
<li>Elman Networks</li>
<li>Jordan networks</li>
<li>Hopfield Networks</li>
<li>Echo State networks</li>
<li>Liquid State Machines</li>
</ul>
<p>Neuroscience applications of RNNs:</p>
<ul>
<li>Working Memory</li>
<li>Sequence Generation</li>
<li>Temporal Pattern recognition</li>
<li>Motor Control</li>
<li>Spontaneous Activity based circuit refinement</li>
</ul>
<p>Optimization techniques for RNNs:</p>
<ul>
<li>Backpropagation Through Time (BPTT)</li>
<li>Real-Time Recurrent Learning (RTRL)</li>
<li>Truncated Backpropagation Through Time (TBPTT)</li>
<li>Surrogate gradient descent (for spiking RNNs) {not really sure if they can be used}</li>
</ul>
<h2 id="point-neuron-based-rnns">Point neuron based RNNs</h2>
<h3 id="vanilla-rnn">Vanilla RNN</h3>
<figure style="text-align: center;">
  <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/Recurrent_neural_network_unfold.svg/500px-Recurrent_neural_network_unfold.svg.png?utm_source=en.wikipedia.org&utm_campaign=parser&utm_content=thumbnail"width="400" alt="Vanilla-RNN">
  <figcaption>image credit: wikipedia</figcaption>
</figure>
<h4 id="algorithm">algorithm</h4>
<ol>
<li>Initialize weights and biases</li>
<li>Compute forward pass
<ul>
<li>at t=0
<ul>
<li>Initialize hidden state: $$ h_0  = 0 $$</li>
</ul>
</li>
<li>For each time step t:
<ul>
<li>Compute hidden state: $$ h_t = f\left(W_{ih} * x_t + W_{hh} * h_{t-1} + b_h)\right) $$</li>
<li>Compute output: $$ y_t = W_{ho} * h_t + b_o $$</li>
<li>[Optional] Apply activation function to output: $$ y_t = g(y_t) $$</li>
</ul>
</li>
</ul>
</li>
<li>Compute loss</li>
<li>Backpropagate errors through time</li>
<li>Update weights and biases</li>
<li>Repeat for multiple epochs</li>
</ol>
<h4 id="limitations-and-motivations-for-advanced-architectures">limitations and motivations for advanced architectures</h4>
<p>Classic RNNs can keep track of arbitrary long-term dependencies in the input sequences. But during training via back-propagation, the long-term gradients which are back-propagated can &ldquo;vanish&rdquo;, RNNs using LSTM units partially solve the vanishing gradient problem, because LSTM units allow gradients to also flow with little to no attenuation. However, LSTM networks can still suffer from the &ldquo;exploding gradient problem&rdquo;.</p>
<hr>
<hr>
<h3 id="long-short-term-memory">Long Short-Term Memory</h3>
<h4 id="introduction-1">introduction</h4>
<p>LSTMs architecture is designed to address the vanishing gradient problem in traditional RNNs by introducing <em>memory cell (or cell)</em> and <em>gating mechanisms</em> that allof the network to maintain and update information over longer sequences (usually short-term memory for RNN that can last thousands of timesteps (thus longterm))<a href="#lstmref1">1</a>.</p>
<p>An LSTM unit is typically composed of a cell and three gates:</p>
<ul>
<li><strong>Cell State (special hidden state)</strong> $C_t$:<br>
-Carries information across arbitary time steps. while the gates regulate the flow of information into and out of the cell state.</li>
<li><strong>Forget Gate</strong> $F_t$:
<ul>
<li>Determines which information from the previous cell state should be discarded.</li>
<li>It maps the previous state and the current input to a value between 0 and 1 (after rounding 0 $\rightarrow$ completely forget, 1 $\rightarrow$ completely retain)</li>
</ul>
</li>
<li><strong>Input Gate</strong> $I_t$:
<ul>
<li>Input gates decide which pieces of new information to store in the current cell state. ( similar mechanism like forget gates)</li>
</ul>
</li>
<li><strong>Output Gate</strong> $O_t$:
<ul>
<li>Output gates control which pieces of information in the current cell state to output. (similar mechanisms)</li>
</ul>
</li>
</ul>
<p>Primary goal of LSTM subunits is to be able to decide when to <code>remember</code> and when to <code>ignore inputs in the hidden state</code> via a dedicated mechanism.</p>
<hr>
<h4 id="input-output-forget-gates-and-memory-cell"><code>Input, output, forget gates and memory cell</code></h4>
<p>At each time step t, the LSTM performs the following operations:</p>
<ul>
<li>Input steam from previous hidden state ( h_{t-1} ) and current input ( x_t ) are processed ahead with 3 fully connected layers thresholded with sigmoid -&gt; (0, 1).
<img alt="image showing computaion of forget gate, input gate, output gate" loading="lazy" src="https://classic.d2l.ai/_images/lstm-0.svg"></li>
</ul>
<p>it can be written as:</p>
<ul>
<li>$$ I_t \ = \ \sigma(X_t W_{xi} + H_{t-1} W_{hi} + b_i) $$</li>
<li>$$ F_t \ = \ \sigma(X_t W_{xf} + H_{t-1} W_{hf} + b_f) $$</li>
<li>$$ O_t \ = \ \sigma(X_t W_{xo} + H_{t-1} W_{ho} + b_o) $$</li>
</ul>
<hr>
<h4 id="candidate-memory-cell-tildec_t"><code>Candidate memory cell</code> $\tilde{C_t}$</h4>
<p><img alt="image showing computation of candidate memory cell" loading="lazy" src="https://classic.d2l.ai/_images/lstm-1.svg"></p>
<ul>
<li>
<p>Memory cell ($C_t$) similar to hidden state ($H_t$) dimensions</p>
</li>
<li>
<p>$$ \tilde{C_t} = tanh(X_t W_{xc} + H_{t-1} W_{hc} + b_c) $$</p>
<ul>
<li>This computation is similar to other gates except the use of tanh activation -&gt; (-1, 1).</li>
</ul>
</li>
</ul>
<hr>
<h4 id="updating-memory-cell-c_t"><code>Updating memory cell</code> $C_t$</h4>
<p><img alt="image showing computation of updating memory cell" loading="lazy" src="https://classic.d2l.ai/_images/lstm-2.svg"></p>
<ul>
<li>
<p>Now we have $\tilde{C_t}$ and $C_{t-1}$</p>
<ul>
<li>The $I_t$ and $F_t$ ranging from 1 to 0 defines how much of $\tilde{C_t}$ and $C_{t-1}$ to keep respectively.</li>
</ul>
</li>
<li>
<p>$$ C_t = F_t \odot C_{t-1} + I_t \odot \tilde{C_t} $$</p>
</li>
</ul>
<hr>
<h4 id="computing-hidden-state-h_t"><code>Computing hidden state</code> $H_t$</h4>
<p><img alt="image showing computation of hidden state" loading="lazy" src="https://classic.d2l.ai/_images/lstm-3.svg"></p>
<ul>
<li>
<p>Output gate $O_t$ defines how much of the $C_t$ to output as hidden state $H_t$.</p>
</li>
<li>
<p>$$ H_t = O_t \odot tanh(C_t) $$</p>
</li>
<li>
<p>LSTM it is simply a gated version of the of the memory cell (final prediction).</p>
</li>
<li>
<p>So whenever output gate is closed, no $H_t$ information is passed to the next layer.</p>
</li>
</ul>
<hr>
<h2 id="optimization-techniques-for-rnns">Optimization techniques for RNNs</h2>
<h3 id="backpropagation-through-time-bptt">Backpropagation Through Time (BPTT)</h3>
<p>The tricky part about RNNs is their recurrent nature, which makes it impossible to apply standard backpropagation directly. But curret approach is to &ldquo;unroll&rdquo; the RNN through time, treating it feedforward network. Now compute the loss at each time step and backpropagate the errors through the unrolled network.</p>
<pre tabindex="0"><code>Time:         t
         
Output:       ŷₜ
              ↑        
Cell:   --&gt; [RNN] --&gt; ...
              ↑    hₜ
Input:        xₜ
</code></pre><pre tabindex="0"><code>Time:     t=0        t=1        t=2        t=3
         
Output:   ŷ₀         ŷ₁         ŷ₂         ŷ₃
           ↑          ↑          ↑          ↑
Cell:    [RNN] --&gt;  [RNN] --&gt;  [RNN] --&gt;  [RNN]
           ↑    h₀    ↑    h₁    ↑    h₂    ↑    h₃
Input:    x₀         x₁         x₂         x₃

Forward:  ────────────────────────────────────&gt;
BPTT:     &lt;────────────────────────────────────
</code></pre><h4 id="additional-resources-and-documentation">additional resources and documentation</h4>
<ul>
<li><a href="">mydocs</a></li>
</ul>
<h2 id="conclusion">Conclusion</h2>
<p>Recurrent Neural Networks are inspired from the neuroscience, a fully cross-coupled perceptron network is equivalent to an infinitely deep feedforward network but we train these networks with backpropagation which has limitations such as vanishing and exploding gradients. To overcome these limitations, more advanced architectures like LSTM and GRU were developed, but the fundamental question still remains, &ldquo;how our brain recurrent motifs learn ?&rdquo;</p>
<h2 id="references">References</h2>
<h3 id="lstm-references">LSTM references</h3>
<p><a id="lstmref1"></a></p>
<ol>
<li><a href="https://www.wikiwand.com/en/articles/Long_short-term_memory">wiki</a>
<a id="lstmref2"></a></li>
<li><a href="https://classic.d2l.ai/chapter_recurrent-modern/lstm.html">Dive into Deep Learning - RNNs</a></li>
</ol>
<h2 id="additional-references-and-tutorials">Additional references and tutorials</h2>
<h3 id="vanilla-rnn-tutorials">Vanilla RNN tutorials</h3>
<ul>
<li><a href="https://en.wikipedia.org/wiki/Recurrent_neural_network">wiki</a></li>
<li><a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.RNN.html">RNNs by torch</a></li>
<li><a href="https://www.kaggle.com/code/namanmanchanda/rnn-in-pytorch">RNN tutorial from kaggle</a></li>
<li><a href="https://medium.com/@noorfatimaafzalbutt/recurrent-neural-networks-rnn-with-pytorch-a-complete-guide-8c40c69032d2">RNN tutorial from medium</a></li>
<li><a href="https://www.codecademy.com/article/rnn-py-torch-time-series-tutorial-complete-guide-to-implementation">RNN tutorial from data_academy</a></li>
<li><a href="https://solardevs.com/blog/rnn-from-scratch-pytorch/">RNN tutorial by solardevs</a></li>
<li><a href="https://www.deeplearningwizard.com/deep_learning/practical_pytorch/pytorch_recurrent_neuralnetwork/#steps_2">RNN tutorial by deeplearning wizard</a></li>
<li><a href="https://www.analyticsvidhya.com/blog/2021/07/understanding-rnn-step-by-step-with-pytorch/">RNN tutorial by analyticsvidhya</a></li>
<li><a href="https://apxml.com/courses/getting-started-with-pytorch/chapter-7-introduction-common-architectures/building-simple-rnn">RNN tutorial APX</a></li>
<li><a href="https://jaketae.github.io/study/pytorch-rnn/">RNN tutorial by Jake</a></li>
<li><a href="https://www.cs.toronto.edu/~lczhang/aps360_20191/lec/w06/rnn.html">RNN tutorial by Utoronto</a></li>
<li><a href="https://towardsdatascience.com/rnns-from-theory-to-pytorch-f0af30b610e1/">RNN tutorial by towardsdatascience</a></li>
</ul>
]]></content:encoded>
    </item>
  </channel>
</rss>
