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
+11-1Lines changed: 11 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -236,7 +236,17 @@
236
236
"source": [
237
237
"The [dataset](http://timeseriesclassification.com/description.php?Dataset=ECG5000) contains 5,000 Time Series examples (obtained with ECG) with 140 timesteps. Each sequence corresponds to a single heartbeat from a single patient with congestive heart failure.\n",
238
238
"\n",
239
-
"> An electrocardiogram (ECG or EKG) is a test that checks how your heart is functioning by measuring the electrical activity of the heart. With each heart beat, an electrical impulse (or wave) travels through your heart. This wave causes the muscle to squeeze and pump blood from the heart. [Source](https://www.heartandstroke.ca/heart/tests/electrocardiogram)\n",
239
+
"> An electrocardiogram (ECG or EKG) is a test that checks how your heart is functioning by measuring the electrical activity of the heart. It measures the electrical activity of your heart. It uses small electrodes attached to your skin to detect the tiny electrical signals that control your heartbeat. With each heart beat, an electrical impulse (or wave) travels through your heart. This wave causes the muscle to squeeze and pump blood from the heart. [Source](https://www.heartandstroke.ca/heart/tests/electrocardiogram)\n",
240
+
"\n",
241
+
"* ___<span style=\"color:Violet\">What it measures</span>___: An ECG records the electrical impulses that cause your heart to beat, showing how the signals travel through your heart's chambers.\n",
242
+
"\n",
243
+
"* ___<span style=\"color:Violet\">How it's done</span>___: During an ECG, electrodes are attached to your chest, arms, and legs using adhesive patches or small suction cups.\n",
244
+
"\n",
245
+
"* ___<span style=\"color:Violet\">What it shows</span>___: The ECG produces a graphical representation of your heart's electrical activity, known as an ECG tracing.\n",
246
+
"\n",
247
+
"* ___<span style=\"color:Violet\">Why it's used</span>___: ECGs are used to diagnose and monitor various heart conditions, including arrhythmias, coronary artery disease, heart attacks, and heart failure.\n",
248
+
"\n",
249
+
"* ___<span style=\"color:Violet\">What it can reveal</span>___: An ECG can help doctors detect changes in heart rate, rhythm, and electrical conduction, which may indicate a heart problem.\n",
<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-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}
1102
+
<divclass="amsmath math notranslate nohighlight" id="equation-830c9138-1a65-49b8-872b-1acddec3394f">
1103
+
<spanclass="eqno">(1)<aclass="headerlink" href="#equation-830c9138-1a65-49b8-872b-1acddec3394f" 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-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}
1116
+
<divclass="amsmath math notranslate nohighlight" id="equation-a3fd887d-05d9-4258-be55-51f090e8e047">
1117
+
<spanclass="eqno">(2)<aclass="headerlink" href="#equation-a3fd887d-05d9-4258-be55-51f090e8e047" 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-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}
1136
+
<divclass="amsmath math notranslate nohighlight" id="equation-9c3f551c-a052-48b9-965d-1ad4e740f523">
1137
+
<spanclass="eqno">(3)<aclass="headerlink" href="#equation-9c3f551c-a052-48b9-965d-1ad4e740f523" 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-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}
1145
+
<divclass="amsmath math notranslate nohighlight" id="equation-47541027-2870-4ee4-9507-0f0e5a526c4d">
1146
+
<spanclass="eqno">(4)<aclass="headerlink" href="#equation-47541027-2870-4ee4-9507-0f0e5a526c4d" 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-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}
1188
+
<divclass="amsmath math notranslate nohighlight" id="equation-8ea9ab45-1c1e-4010-bb7c-cb72e2bc6838">
1189
+
<spanclass="eqno">(5)<aclass="headerlink" href="#equation-8ea9ab45-1c1e-4010-bb7c-cb72e2bc6838" 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-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}
1449
+
<divclass="amsmath math notranslate nohighlight" id="equation-8e853f4e-f7df-491d-8aa1-d10aaf537956">
1450
+
<spanclass="eqno">(6)<aclass="headerlink" href="#equation-8e853f4e-f7df-491d-8aa1-d10aaf537956" 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-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}
1461
+
<divclass="amsmath math notranslate nohighlight" id="equation-02a11d57-f965-4eef-aca2-6a672d249997">
1462
+
<spanclass="eqno">(7)<aclass="headerlink" href="#equation-02a11d57-f965-4eef-aca2-6a672d249997" 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-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}
1501
+
<divclass="amsmath math notranslate nohighlight" id="equation-2165ddf5-f6b4-48e0-b64f-1d4fa56282e1">
1502
+
<spanclass="eqno">(8)<aclass="headerlink" href="#equation-2165ddf5-f6b4-48e0-b64f-1d4fa56282e1" 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-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}
1510
+
<divclass="amsmath math notranslate nohighlight" id="equation-6ea5d2ec-9501-4b7e-b964-36c8d47aa5f7">
1511
+
<spanclass="eqno">(9)<aclass="headerlink" href="#equation-6ea5d2ec-9501-4b7e-b964-36c8d47aa5f7" title="Permalink to this equation">#</a></span>\[\begin{equation}
<h3><spanstyle="color:LightGreen">Data</span><aclass="headerlink" href="#span-style-color-lightgreen-data-span" title="Permalink to this heading">#</a></h3>
665
665
<p>The <aclass="reference external" href="http://timeseriesclassification.com/description.php?Dataset=ECG5000">dataset</a> contains 5,000 Time Series examples (obtained with ECG) with 140 timesteps. Each sequence corresponds to a single heartbeat from a single patient with congestive heart failure.</p>
666
666
<blockquote>
667
-
<div><p>An electrocardiogram (ECG or EKG) is a test that checks how your heart is functioning by measuring the electrical activity of the heart. With each heart beat, an electrical impulse (or wave) travels through your heart. This wave causes the muscle to squeeze and pump blood from the heart. <aclass="reference external" href="https://www.heartandstroke.ca/heart/tests/electrocardiogram">Source</a></p>
667
+
<div><p>An electrocardiogram (ECG or EKG) is a test that checks how your heart is functioning by measuring the electrical activity of the heart. It measures the electrical activity of your heart. It uses small electrodes attached to your skin to detect the tiny electrical signals that control your heartbeat. With each heart beat, an electrical impulse (or wave) travels through your heart. This wave causes the muscle to squeeze and pump blood from the heart. <aclass="reference external" href="https://www.heartandstroke.ca/heart/tests/electrocardiogram">Source</a></p>
668
668
</div></blockquote>
669
+
<ulclass="simple">
670
+
<li><p><em><strong><spanstyle="color:Violet">What it measures</span></strong></em>: An ECG records the electrical impulses that cause your heart to beat, showing how the signals travel through your heart’s chambers.</p></li>
671
+
<li><p><em><strong><spanstyle="color:Violet">How it’s done</span></strong></em>: During an ECG, electrodes are attached to your chest, arms, and legs using adhesive patches or small suction cups.</p></li>
672
+
<li><p><em><strong><spanstyle="color:Violet">What it shows</span></strong></em>: The ECG produces a graphical representation of your heart’s electrical activity, known as an ECG tracing.</p></li>
673
+
<li><p><em><strong><spanstyle="color:Violet">Why it’s used</span></strong></em>: ECGs are used to diagnose and monitor various heart conditions, including arrhythmias, coronary artery disease, heart attacks, and heart failure.</p></li>
674
+
<li><p><em><strong><spanstyle="color:Violet">What it can reveal</span></strong></em>: An ECG can help doctors detect changes in heart rate, rhythm, and electrical conduction, which may indicate a heart problem.</p></li>
0 commit comments