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/PhysicsInformedNeuralNetworks.ipynb
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -427,7 +427,7 @@
427
427
"* Initial version: Mark Neubauer\n",
428
428
"* 1D harmonic oscillator example is based on the blog post [\"So, what is a physics-informed neural network?\"](https://benmoseley.blog/my-research/so-what-is-a-physics-informed-neural-network/). This problem was inspired by the following blog post: https://beltoforion.de/en/harmonic_oscillator/.\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>
1120
1120
<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>
1121
-
<divclass="amsmath math notranslate nohighlight" id="equation-8db11ec8-5537-4341-8562-daa883bd6f56">
1122
-
<spanclass="eqno">(1)<aclass="headerlink" href="#equation-8db11ec8-5537-4341-8562-daa883bd6f56" title="Permalink to this equation">#</a></span>\[\begin{equation}
1121
+
<divclass="amsmath math notranslate nohighlight" id="equation-66693103-b228-4c52-9405-6c950e1dfcfc">
1122
+
<spanclass="eqno">(1)<aclass="headerlink" href="#equation-66693103-b228-4c52-9405-6c950e1dfcfc" 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>
1135
-
<divclass="amsmath math notranslate nohighlight" id="equation-d7da5bab-f1a7-4356-8a51-4f406a6d697d">
1136
-
<spanclass="eqno">(2)<aclass="headerlink" href="#equation-d7da5bab-f1a7-4356-8a51-4f406a6d697d" title="Permalink to this equation">#</a></span>\[\begin{equation}
1135
+
<divclass="amsmath math notranslate nohighlight" id="equation-5903e3a7-442b-44c6-8e54-1f72c57651d0">
1136
+
<spanclass="eqno">(2)<aclass="headerlink" href="#equation-5903e3a7-442b-44c6-8e54-1f72c57651d0" 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>
1155
-
<divclass="amsmath math notranslate nohighlight" id="equation-6a24ca1f-3752-41e7-a409-eb6a3f5e4ae8">
1156
-
<spanclass="eqno">(3)<aclass="headerlink" href="#equation-6a24ca1f-3752-41e7-a409-eb6a3f5e4ae8" title="Permalink to this equation">#</a></span>\[\begin{equation}
1155
+
<divclass="amsmath math notranslate nohighlight" id="equation-a9fd1360-8895-4f50-9d1e-3f84b6a88b6a">
1156
+
<spanclass="eqno">(3)<aclass="headerlink" href="#equation-a9fd1360-8895-4f50-9d1e-3f84b6a88b6a" title="Permalink to this equation">#</a></span>\[\begin{equation}
1157
1157
\begin{bmatrix}
1158
1158
7 & -8 & -\infty \\
1159
1159
-3 & 2 & -\infty \\
1160
1160
1 & 6 & -\infty \\
1161
1161
\end{bmatrix}
1162
1162
\end{equation}\]</div>
1163
1163
<p>Taking the softmax of the rows of this matrix then gives:</p>
1164
-
<divclass="amsmath math notranslate nohighlight" id="equation-b9ec2f62-1be7-4bab-9687-cbd431b5b8db">
1165
-
<spanclass="eqno">(4)<aclass="headerlink" href="#equation-b9ec2f62-1be7-4bab-9687-cbd431b5b8db" title="Permalink to this equation">#</a></span>\[\begin{equation}
1164
+
<divclass="amsmath math notranslate nohighlight" id="equation-d126aa5f-414d-40bc-b02f-57de984a5a8f">
1165
+
<spanclass="eqno">(4)<aclass="headerlink" href="#equation-d126aa5f-414d-40bc-b02f-57de984a5a8f" title="Permalink to this equation">#</a></span>\[\begin{equation}
1166
1166
\text{Softmax}
1167
1167
\begin{bmatrix}
1168
1168
7 & -8 & -\infty \\
@@ -1204,8 +1204,8 @@ <h3><span style="color:LightGreen">Repeating to Match Attention Matrix Shape</sp
1204
1204
<p><codeclass="docutils literal notranslate"><spanclass="pre">attn.shape</span></code> - (Batch x seq_len x seq_len)</p>
1205
1205
<p><codeclass="docutils literal notranslate"><spanclass="pre">mask.shape</span></code> - (Batch x seq_len)</p>
1206
1206
<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>
1207
-
<divclass="amsmath math notranslate nohighlight" id="equation-89c53b7f-df9f-4054-b2d2-4c98a0fab720">
1208
-
<spanclass="eqno">(5)<aclass="headerlink" href="#equation-89c53b7f-df9f-4054-b2d2-4c98a0fab720" title="Permalink to this equation">#</a></span>\[\begin{bmatrix}
1207
+
<divclass="amsmath math notranslate nohighlight" id="equation-54b2dc89-f68c-4179-8b44-2919a01b8f14">
1208
+
<spanclass="eqno">(5)<aclass="headerlink" href="#equation-54b2dc89-f68c-4179-8b44-2919a01b8f14" title="Permalink to this equation">#</a></span>\[\begin{bmatrix}
1209
1209
\textrm{True} & \textrm{True} & \textrm{True} & \textrm{False} \\
1210
1210
\textrm{True} & \textrm{True} & \textrm{True} & \textrm{False} \\
1211
1211
\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>
1467
1467
<p>Lets pretend the raw outputs of <spanclass="math notranslate nohighlight">\(QK^T\)</span>, before the softmax, is below:</p>
1468
-
<divclass="amsmath math notranslate nohighlight" id="equation-f1bd3b36-d087-4430-b415-3f77ded224cc">
1469
-
<spanclass="eqno">(6)<aclass="headerlink" href="#equation-f1bd3b36-d087-4430-b415-3f77ded224cc" title="Permalink to this equation">#</a></span>\[\begin{equation}
1468
+
<divclass="amsmath math notranslate nohighlight" id="equation-b5400ed0-b403-46c3-9114-f1a39648e888">
1469
+
<spanclass="eqno">(6)<aclass="headerlink" href="#equation-b5400ed0-b403-46c3-9114-f1a39648e888" title="Permalink to this equation">#</a></span>\[\begin{equation}
1470
1470
\begin{bmatrix}
1471
1471
7 & -8 & 6 \\
1472
1472
-3 & 2 & 4 \\
@@ -1477,8 +1477,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>
1480
-
<divclass="amsmath math notranslate nohighlight" id="equation-dc2631a9-ae11-4fde-a276-e4464548a039">
1481
-
<spanclass="eqno">(7)<aclass="headerlink" href="#equation-dc2631a9-ae11-4fde-a276-e4464548a039" title="Permalink to this equation">#</a></span>\[\begin{equation}
1480
+
<divclass="amsmath math notranslate nohighlight" id="equation-3117d899-b5d1-4fc0-a13a-3431bb40865e">
1481
+
<spanclass="eqno">(7)<aclass="headerlink" href="#equation-3117d899-b5d1-4fc0-a13a-3431bb40865e" title="Permalink to this equation">#</a></span>\[\begin{equation}
1482
1482
\text{Softmax}
1483
1483
\begin{bmatrix}
1484
1484
7 & -8 & 6 \\
@@ -1517,17 +1517,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>
1520
-
<divclass="amsmath math notranslate nohighlight" id="equation-5d8bf146-85f6-4458-94f9-7093ed348a84">
1521
-
<spanclass="eqno">(8)<aclass="headerlink" href="#equation-5d8bf146-85f6-4458-94f9-7093ed348a84" title="Permalink to this equation">#</a></span>\[\begin{equation}
1520
+
<divclass="amsmath math notranslate nohighlight" id="equation-14e79be7-3542-4c18-8ff9-4168ea636de1">
1521
+
<spanclass="eqno">(8)<aclass="headerlink" href="#equation-14e79be7-3542-4c18-8ff9-4168ea636de1" title="Permalink to this equation">#</a></span>\[\begin{equation}
1522
1522
\begin{bmatrix}
1523
1523
7 & -\infty & -\infty \\
1524
1524
-3 & 2 & -\infty \\
1525
1525
1 & 6 & -2 \\
1526
1526
\end{bmatrix}
1527
1527
\end{equation}\]</div>
1528
1528
<p>Taking the softmax of the rows of this matrix then gives:</p>
1529
-
<divclass="amsmath math notranslate nohighlight" id="equation-1fa44a22-79a9-4d02-bd59-a1e079520661">
1530
-
<spanclass="eqno">(9)<aclass="headerlink" href="#equation-1fa44a22-79a9-4d02-bd59-a1e079520661" title="Permalink to this equation">#</a></span>\[\begin{equation}
1529
+
<divclass="amsmath math notranslate nohighlight" id="equation-4a3cd56a-86ea-4c3d-b9ea-34cc4f401018">
1530
+
<spanclass="eqno">(9)<aclass="headerlink" href="#equation-4a3cd56a-86ea-4c3d-b9ea-34cc4f401018" title="Permalink to this equation">#</a></span>\[\begin{equation}
<li><p>1D harmonic oscillator example is based on the blog post <aclass="reference external" href="https://benmoseley.blog/my-research/so-what-is-a-physics-informed-neural-network/">“So, what is a physics-informed neural network?”</a>. This problem was inspired by the following blog post: <aclass="reference external" href="https://beltoforion.de/en/harmonic_oscillator/">https://beltoforion.de/en/harmonic_oscillator/</a>.</p></li>
0 commit comments