You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _sources/_sources/lectures/UnsupervisedLearningAnomalyDetection.ipynb
+44-2Lines changed: 44 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -181,6 +181,48 @@
181
181
"While our Time Series data is univariate (we have only 1 feature), the code should work for multivariate datasets (multiple features) with little or no modification. Feel free to try it!"
"A <span style=\"color:Violet\">Long Short-Term Memory</span> (LSTM) is a type of Recurrent Neural Network (RNN) designed to handle long-term dependencies in sequential data, such as text, time series, and speech. LSTMs are known for their ability to mitigate the vanishing gradient problem that plagues standard RNNs, allowing them to learn and remember information over longer sequences of data.\n",
196
+
"\n",
197
+
"Key Features of LSTMs:\n",
198
+
"\n",
199
+
"* ___<span style=\"color:Violet\">Memory Cell</span>___: LSTMs introduce a memory cell that acts as a \"memory\" for the network, allowing it to store and retrieve information over time.\n",
200
+
"\n",
201
+
"* ___<span style=\"color:Violet\">Gates</span>___: LSTMs use \"gates\" (input, forget, and output gates) to control the flow of information into, out of, and within the memory cell.\n",
202
+
"\n",
203
+
"* ___<span style=\"color:Violet\">Vanishing Gradient Problem</span>___: LSTMs are designed to prevent the gradients from vanishing or exploding as they propagate through the network over time, making them more effective for learning long-term relationships in sequential data.\n",
204
+
"\n",
205
+
"* ___<span style=\"color:Violet\">Sequence Learning</span>___: LSTMs are particularly well-suited for tasks that involve processing sequential data, such as natural language processing (language modeling, machine translation), speech recognition, and time series forecasting.\n",
206
+
"\n",
207
+
"How LSTMs Work:\n",
208
+
"\n",
209
+
"1. ___<span style=\"color:Violet\">Input</span>___: The LSTM receives an input sequence, where each input represents a time step. \n",
210
+
"\n",
211
+
"2. ___<span style=\"color:Violet\">Gates</span>___: The gates regulate the flow of information into the memory cell and the output from the cell. \n",
212
+
"\n",
213
+
"3. ___<span style=\"color:Violet\">Memory Cell</span>___: The memory cell stores and updates its internal state based on the input and the previous state. \n",
214
+
"\n",
215
+
"4. ___<span style=\"color:Violet\">Output</span>___: The LSTM produces an output at each time step based on the current cell state and the input.\n",
216
+
"\n",
217
+
"Advantages of LSTMs:\n",
218
+
"\n",
219
+
"* ___<span style=\"color:Violet\">Long-term dependencies</span>___: LSTMs are capable of learning long-term dependencies in sequential data.\n",
220
+
"\n",
221
+
"* ___<span style=\"color:Violet\">Vanishing gradient problem</span>___: LSTMs mitigate the vanishing gradient problem, making them more effective for processing long sequences.\n",
222
+
"\n",
223
+
"* ___<span style=\"color:Violet\">Wide range of applications</span>___: LSTMs have been successfully applied to many sequence learning tasks."
<h2><spanstyle="color:Orange">Computing the Reweighted Padded Attention Mask</span><aclass="headerlink" href="#span-style-color-orange-computing-the-reweighted-padded-attention-mask-span" title="Permalink to this heading">#</a></h2>
1101
1101
<p>Lets create some numbers so we can get a better idea of how this works. Let the tokens be <spanclass="math notranslate nohighlight">\(X = [10, 2, \text{<pad>}]\)</span>, so the third token is a padding token. Lets then also pretend, we pass this to our model, and when we go to compute our attention <spanclass="math notranslate nohighlight">\(QK^T\)</span>. The raw output before the Softmax is below:</p>
1102
-
<divclass="amsmath math notranslate nohighlight" id="equation-8a35a337-a51d-4693-9249-265321965daf">
1103
-
<spanclass="eqno">(1)<aclass="headerlink" href="#equation-8a35a337-a51d-4693-9249-265321965daf" title="Permalink to this equation">#</a></span>\[\begin{equation}
1102
+
<divclass="amsmath math notranslate nohighlight" id="equation-c70790f6-1a83-47ed-acde-dc45b63490b5">
1103
+
<spanclass="eqno">(1)<aclass="headerlink" href="#equation-c70790f6-1a83-47ed-acde-dc45b63490b5" title="Permalink to this equation">#</a></span>\[\begin{equation}
<p>If we ignore padding and everything right now, we can compute softmax for row of the matrix above:</p>
1116
-
<divclass="amsmath math notranslate nohighlight" id="equation-756eb811-31e8-485a-b729-a4247b7c9dfe">
1117
-
<spanclass="eqno">(2)<aclass="headerlink" href="#equation-756eb811-31e8-485a-b729-a4247b7c9dfe" title="Permalink to this equation">#</a></span>\[\begin{equation}
1116
+
<divclass="amsmath math notranslate nohighlight" id="equation-2e2338aa-104a-4755-9040-eba4460729fd">
1117
+
<spanclass="eqno">(2)<aclass="headerlink" href="#equation-2e2338aa-104a-4755-9040-eba4460729fd" title="Permalink to this equation">#</a></span>\[\begin{equation}
<p>But what we need is to mask out all the tokens in this matrix related to padding. Just like we did in <aclass="reference external" href="https://github.com/priyammaz/HAL-DL-From-Scratch/tree/main/PyTorch%20for%20NLP/GPT">GPT</a>, we will fill in the indexes of the that we want to mask with <spanclass="math notranslate nohighlight">\(-\infty\)</span>. If only the last token was a padding token in our sequence, then the attention before the softmax should be written as:</p>
1136
-
<divclass="amsmath math notranslate nohighlight" id="equation-5709591d-6e65-4893-b935-aaf48acf26e3">
1137
-
<spanclass="eqno">(3)<aclass="headerlink" href="#equation-5709591d-6e65-4893-b935-aaf48acf26e3" title="Permalink to this equation">#</a></span>\[\begin{equation}
1136
+
<divclass="amsmath math notranslate nohighlight" id="equation-4c716b8f-af62-4d56-97fe-b00066113dda">
1137
+
<spanclass="eqno">(3)<aclass="headerlink" href="#equation-4c716b8f-af62-4d56-97fe-b00066113dda" title="Permalink to this equation">#</a></span>\[\begin{equation}
1138
1138
\begin{bmatrix}
1139
1139
7 & -8 & -\infty \\
1140
1140
-3 & 2 & -\infty \\
1141
1141
1 & 6 & -\infty \\
1142
1142
\end{bmatrix}
1143
1143
\end{equation}\]</div>
1144
1144
<p>Taking the softmax of the rows of this matrix then gives:</p>
1145
-
<divclass="amsmath math notranslate nohighlight" id="equation-93194f43-7a1e-400a-b6aa-733b2d292957">
1146
-
<spanclass="eqno">(4)<aclass="headerlink" href="#equation-93194f43-7a1e-400a-b6aa-733b2d292957" title="Permalink to this equation">#</a></span>\[\begin{equation}
1145
+
<divclass="amsmath math notranslate nohighlight" id="equation-ad16f16b-eced-4c4d-9cac-87c514955daf">
1146
+
<spanclass="eqno">(4)<aclass="headerlink" href="#equation-ad16f16b-eced-4c4d-9cac-87c514955daf" title="Permalink to this equation">#</a></span>\[\begin{equation}
1147
1147
\text{Softmax}
1148
1148
\begin{bmatrix}
1149
1149
7 & -8 & -\infty \\
@@ -1185,8 +1185,8 @@ <h3><span style="color:LightGreen">Repeating to Match Attention Matrix Shape</sp
1185
1185
<p><codeclass="docutils literal notranslate"><spanclass="pre">attn.shape</span></code> - (Batch x seq_len x seq_len)</p>
1186
1186
<p><codeclass="docutils literal notranslate"><spanclass="pre">mask.shape</span></code> - (Batch x seq_len)</p>
1187
1187
<p>It is clear that our mask is missing a dimension, and we need to repeat it. Lets take sequence_1 for instance that has a mask of [True, True, True, False]. Because the sequence length here is 4, lets repeat this row 4 times:</p>
1188
-
<divclass="amsmath math notranslate nohighlight" id="equation-6aafbce1-68b0-4a7c-ab18-ad24ddc78de2">
1189
-
<spanclass="eqno">(5)<aclass="headerlink" href="#equation-6aafbce1-68b0-4a7c-ab18-ad24ddc78de2" title="Permalink to this equation">#</a></span>\[\begin{bmatrix}
1188
+
<divclass="amsmath math notranslate nohighlight" id="equation-9abc9429-fbf9-4e1e-9a38-f772a6250c1f">
1189
+
<spanclass="eqno">(5)<aclass="headerlink" href="#equation-9abc9429-fbf9-4e1e-9a38-f772a6250c1f" title="Permalink to this equation">#</a></span>\[\begin{bmatrix}
1190
1190
\textrm{True} & \textrm{True} & \textrm{True} & \textrm{False} \\
1191
1191
\textrm{True} & \textrm{True} & \textrm{True} & \textrm{False} \\
1192
1192
\textrm{True} & \textrm{True} & \textrm{True} & \textrm{False} \\
<h3><spanstyle="color:LightGreen">Computing the Reweighted Causal Attention Mask</span><aclass="headerlink" href="#span-style-color-lightgreen-computing-the-reweighted-causal-attention-mask-span" title="Permalink to this heading">#</a></h3>
1448
1448
<p>Lets pretend the raw outputs of <spanclass="math notranslate nohighlight">\(QK^T\)</span>, before the softmax, is below:</p>
1449
-
<divclass="amsmath math notranslate nohighlight" id="equation-bcf05244-fb23-4fbf-ae04-7ee0c29b85f7">
1450
-
<spanclass="eqno">(6)<aclass="headerlink" href="#equation-bcf05244-fb23-4fbf-ae04-7ee0c29b85f7" title="Permalink to this equation">#</a></span>\[\begin{equation}
1449
+
<divclass="amsmath math notranslate nohighlight" id="equation-a3bcd113-24b6-4be0-8074-bfa67eef5331">
1450
+
<spanclass="eqno">(6)<aclass="headerlink" href="#equation-a3bcd113-24b6-4be0-8074-bfa67eef5331" title="Permalink to this equation">#</a></span>\[\begin{equation}
1451
1451
\begin{bmatrix}
1452
1452
7 & -8 & 6 \\
1453
1453
-3 & 2 & 4 \\
@@ -1458,8 +1458,8 @@ <h3><span style="color:LightGreen">Computing the Reweighted Causal Attention Mas
<p>Then, we can compute softmax for row of the matrix above:</p>
1461
-
<divclass="amsmath math notranslate nohighlight" id="equation-78810f9a-25e8-4d4e-82d7-529671c15bd8">
1462
-
<spanclass="eqno">(7)<aclass="headerlink" href="#equation-78810f9a-25e8-4d4e-82d7-529671c15bd8" title="Permalink to this equation">#</a></span>\[\begin{equation}
1461
+
<divclass="amsmath math notranslate nohighlight" id="equation-658af593-8f9f-4139-be56-fb460ca2b1eb">
1462
+
<spanclass="eqno">(7)<aclass="headerlink" href="#equation-658af593-8f9f-4139-be56-fb460ca2b1eb" title="Permalink to this equation">#</a></span>\[\begin{equation}
1463
1463
\text{Softmax}
1464
1464
\begin{bmatrix}
1465
1465
7 & -8 & 6 \\
@@ -1498,17 +1498,17 @@ <h3><span style="color:LightGreen">Computing the Reweighted Causal Attention Mas
<p>So we have exactly what we want! The attention weight of the last value is set to 0, so when we are on the second vector <spanclass="math notranslate nohighlight">\(x_2\)</span>, we cannot look forward to the future value vectors <spanclass="math notranslate nohighlight">\(v_3\)</span>, and the remaining parts add up to 1 so its still a probability vector! To do this correctly for the entire matrix, we can just substitute in the top triangle of <spanclass="math notranslate nohighlight">\(QK^T\)</span> with <spanclass="math notranslate nohighlight">\(-\infty\)</span>. This would look like:</p>
1501
-
<divclass="amsmath math notranslate nohighlight" id="equation-7f0464ed-96a0-4b1f-a906-6faf2d1e6d86">
1502
-
<spanclass="eqno">(8)<aclass="headerlink" href="#equation-7f0464ed-96a0-4b1f-a906-6faf2d1e6d86" title="Permalink to this equation">#</a></span>\[\begin{equation}
1501
+
<divclass="amsmath math notranslate nohighlight" id="equation-5c744a47-0d2a-4619-9242-21ec929d3bb8">
1502
+
<spanclass="eqno">(8)<aclass="headerlink" href="#equation-5c744a47-0d2a-4619-9242-21ec929d3bb8" title="Permalink to this equation">#</a></span>\[\begin{equation}
1503
1503
\begin{bmatrix}
1504
1504
7 & -\infty & -\infty \\
1505
1505
-3 & 2 & -\infty \\
1506
1506
1 & 6 & -2 \\
1507
1507
\end{bmatrix}
1508
1508
\end{equation}\]</div>
1509
1509
<p>Taking the softmax of the rows of this matrix then gives:</p>
1510
-
<divclass="amsmath math notranslate nohighlight" id="equation-4b059b5b-dc0d-4cdf-b66c-995c1317ec75">
1511
-
<spanclass="eqno">(9)<aclass="headerlink" href="#equation-4b059b5b-dc0d-4cdf-b66c-995c1317ec75" title="Permalink to this equation">#</a></span>\[\begin{equation}
1510
+
<divclass="amsmath math notranslate nohighlight" id="equation-9ab9add6-4fdb-430f-8eab-90022d1c4d48">
1511
+
<spanclass="eqno">(9)<aclass="headerlink" href="#equation-9ab9add6-4fdb-430f-8eab-90022d1c4d48" title="Permalink to this equation">#</a></span>\[\begin{equation}
0 commit comments