Skip to content

Commit d198e3a

Browse files
committed
update week 46
1 parent e4215ff commit d198e3a

File tree

9 files changed

+2566
-324
lines changed

9 files changed

+2566
-324
lines changed

doc/pub/week46/html/._week46-bs001.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,10 @@ <h2 id="plan-for-week-46" class="anchor">Plan for week 46 </h2>
323323
<div class="panel-body">
324324
<!-- subsequent paragraphs come in larger fonts, so start with a paragraph -->
325325
<ol>
326-
<li> Work on and discussions of project 3</li>
326+
<li> Intro to and mathematics of Recurrent Neural Networks (RNNs)</li>
327+
<li> Lecture notes at <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/pub/week46/ipynb/week46.ipynb" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/blob/master/doc/pub/week46/ipynb/week46.ipynb</tt></a>
328+
<!-- o Video of lecture at <a href="https://youtu.be/TfRKUfdJwB4" target="_self"><tt>https://youtu.be/TfRKUfdJwB4</tt></a> -->
329+
<!-- o Whiteboard notes at <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2024/NotesNovember11.pdf" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2024/NotesNovember11.pdf</tt></a> --></li>
327330
</ol>
328331
</div>
329332
</div>
@@ -333,10 +336,7 @@ <h2 id="plan-for-week-46" class="anchor">Plan for week 46 </h2>
333336
<div class="panel-body">
334337
<!-- subsequent paragraphs come in larger fonts, so start with a paragraph -->
335338
<ol>
336-
<li> Intro to and mathematics of Recurrent Neural Networks (RNNs)</li>
337-
<li> Lecture notes at <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/pub/week46/ipynb/week46.ipynb" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/blob/master/doc/pub/week46/ipynb/week46.ipynb</tt></a>
338-
<!-- o Video of lecture at <a href="https://youtu.be/TfRKUfdJwB4" target="_self"><tt>https://youtu.be/TfRKUfdJwB4</tt></a> -->
339-
<!-- o Whiteboard notes at <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2024/NotesNovember11.pdf" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2024/NotesNovember11.pdf</tt></a> --></li>
339+
<li> Work on and discussions of project 3, project available from November 10,(evening), deadline December 15 at midnight.</li>
340340
</ol>
341341
</div>
342342
</div>

doc/pub/week46/html/._week46-bs013.html

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -447,15 +447,6 @@ <h2 id="corresponding-example-using-pytorch" class="anchor">Corresponding exampl
447447
<span style="color: #408080; font-style: italic"># Update sequence by removing first element and adding new prediction</span>
448448
current_sequence <span style="color: #666666">=</span> torch<span style="color: #666666">.</span>cat([current_sequence[:, <span style="color: #666666">1</span>:, :], pred<span style="color: #666666">.</span>unsqueeze(<span style="color: #666666">1</span>)], dim<span style="color: #666666">=1</span>)
449449

450-
<span style="color: #408080; font-style: italic"># Plot results</span>
451-
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">12</span>, <span style="color: #666666">6</span>))
452-
plt<span style="color: #666666">.</span>plot(t[<span style="color: #666666">800</span>:]<span style="color: #666666">.</span>numpy(), val_data<span style="color: #666666">.</span>numpy(), label<span style="color: #666666">=</span><span style="color: #BA2121">&#39;True values&#39;</span>)
453-
plt<span style="color: #666666">.</span>plot(t[<span style="color: #666666">800</span>:]<span style="color: #666666">.</span>numpy(), predictions, label<span style="color: #666666">=</span><span style="color: #BA2121">&#39;Predictions&#39;</span>)
454-
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">&#39;Sine Wave Prediction&#39;</span>)
455-
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&#39;Time&#39;</span>)
456-
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">&#39;Value&#39;</span>)
457-
plt<span style="color: #666666">.</span>legend()
458-
plt<span style="color: #666666">.</span>show()
459450

460451
<span style="color: #408080; font-style: italic"># Plot training and validation loss</span>
461452
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">10</span>, <span style="color: #666666">5</span>))

doc/pub/week46/html/week46-reveal.html

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -198,22 +198,22 @@ <h4>Week 46, November 10-14</h4>
198198
<h2 id="plan-for-week-46">Plan for week 46 </h2>
199199

200200
<div class="alert alert-block alert-block alert-text-normal">
201-
<b>Lab sessions on Tuesday and Wednesday</b>
201+
<b>Material for the lecture on Monday November 10, 2025</b>
202202
<p>
203203
<ol>
204-
<p><li> Work on and discussions of project 3</li>
204+
<p><li> Intro to and mathematics of Recurrent Neural Networks (RNNs)</li>
205+
<p><li> Lecture notes at <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/pub/week46/ipynb/week46.ipynb" target="_blank"><tt>https://github.com/CompPhysics/MachineLearning/blob/master/doc/pub/week46/ipynb/week46.ipynb</tt></a>
206+
<!-- o Video of lecture at <a href="https://youtu.be/TfRKUfdJwB4" target="_blank"><tt>https://youtu.be/TfRKUfdJwB4</tt></a> -->
207+
<!-- o Whiteboard notes at <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2024/NotesNovember11.pdf" target="_blank"><tt>https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2024/NotesNovember11.pdf</tt></a> --></li>
205208
</ol>
206209
</div>
207210

208211

209212
<div class="alert alert-block alert-block alert-text-normal">
210-
<b>Material for the lecture on Monday November 10, 2025</b>
213+
<b>Lab sessions on Tuesday and Wednesday</b>
211214
<p>
212215
<ol>
213-
<p><li> Intro to and mathematics of Recurrent Neural Networks (RNNs)</li>
214-
<p><li> Lecture notes at <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/pub/week46/ipynb/week46.ipynb" target="_blank"><tt>https://github.com/CompPhysics/MachineLearning/blob/master/doc/pub/week46/ipynb/week46.ipynb</tt></a>
215-
<!-- o Video of lecture at <a href="https://youtu.be/TfRKUfdJwB4" target="_blank"><tt>https://youtu.be/TfRKUfdJwB4</tt></a> -->
216-
<!-- o Whiteboard notes at <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2024/NotesNovember11.pdf" target="_blank"><tt>https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2024/NotesNovember11.pdf</tt></a> --></li>
216+
<p><li> Work on and discussions of project 3, project available from November 10,(evening), deadline December 15 at midnight.</li>
217217
</ol>
218218
</div>
219219
</section>
@@ -592,15 +592,6 @@ <h2 id="corresponding-example-using-pytorch">Corresponding example using PyTorch
592592
<span style="color: #228B22"># Update sequence by removing first element and adding new prediction</span>
593593
current_sequence = torch.cat([current_sequence[:, <span style="color: #B452CD">1</span>:, :], pred.unsqueeze(<span style="color: #B452CD">1</span>)], dim=<span style="color: #B452CD">1</span>)
594594

595-
<span style="color: #228B22"># Plot results</span>
596-
plt.figure(figsize=(<span style="color: #B452CD">12</span>, <span style="color: #B452CD">6</span>))
597-
plt.plot(t[<span style="color: #B452CD">800</span>:].numpy(), val_data.numpy(), label=<span style="color: #CD5555">&#39;True values&#39;</span>)
598-
plt.plot(t[<span style="color: #B452CD">800</span>:].numpy(), predictions, label=<span style="color: #CD5555">&#39;Predictions&#39;</span>)
599-
plt.title(<span style="color: #CD5555">&#39;Sine Wave Prediction&#39;</span>)
600-
plt.xlabel(<span style="color: #CD5555">&#39;Time&#39;</span>)
601-
plt.ylabel(<span style="color: #CD5555">&#39;Value&#39;</span>)
602-
plt.legend()
603-
plt.show()
604595

605596
<span style="color: #228B22"># Plot training and validation loss</span>
606597
plt.figure(figsize=(<span style="color: #B452CD">10</span>, <span style="color: #B452CD">5</span>))
@@ -1740,6 +1731,7 @@ <h2 id="the-runge-kutta-4-code">The Runge-Kutta-4 code </h2>
17401731
<section>
17411732
<h2 id="using-the-above-data-to-train-an-rnn">Using the above data to train an RNN </h2>
17421733

1734+
<p>In the code here we have reworked the previous example in order to generate data that can be handled by recurrent neural networks in order to train our model.</p>
17431735

17441736
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
17451737
<div class="cell border-box-sizing code_cell rendered">
@@ -1754,18 +1746,16 @@ <h2 id="using-the-above-data-to-train-an-rnn">Using the above data to train an R
17541746
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
17551747

17561748

1757-
<span style="color: #228B22"># ============================</span>
1758-
<span style="color: #228B22"># 1. Physics model (from rk4.py)</span>
1759-
<span style="color: #228B22"># ============================</span>
1749+
<span style="color: #228B22"># Newton&#39;s equation for harmonic oscillations with external force</span>
17601750

1761-
<span style="color: #228B22"># Global parameters (same idea as in rk4.py)</span>
1751+
<span style="color: #228B22"># Global parameters</span>
17621752
gamma = <span style="color: #B452CD">0.2</span> <span style="color: #228B22"># damping</span>
17631753
Omegatilde = <span style="color: #B452CD">0.5</span> <span style="color: #228B22"># driving frequency</span>
17641754
Ftilde = <span style="color: #B452CD">1.0</span> <span style="color: #228B22"># driving amplitude</span>
17651755

17661756
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">spring_force</span>(v, x, t):
17671757
<span style="color: #CD5555">&quot;&quot;&quot;</span>
1768-
<span style="color: #CD5555"> SpringForce from rk4.py:</span>
1758+
<span style="color: #CD5555"> SpringForce:</span>
17691759
<span style="color: #CD5555"> note: divided by mass =&gt; returns acceleration</span>
17701760
<span style="color: #CD5555"> a = -2*gamma*v - x + Ftilde*cos(Omegatilde * t)</span>
17711761
<span style="color: #CD5555"> &quot;&quot;&quot;</span>
@@ -1774,7 +1764,6 @@ <h2 id="using-the-above-data-to-train-an-rnn">Using the above data to train an R
17741764

17751765
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">rk4_trajectory</span>(DeltaT=<span style="color: #B452CD">0.001</span>, tfinal=<span style="color: #B452CD">20.0</span>, x0=<span style="color: #B452CD">1.0</span>, v0=<span style="color: #B452CD">0.0</span>):
17761766
<span style="color: #CD5555">&quot;&quot;&quot;</span>
1777-
<span style="color: #CD5555"> Reimplementation of RK4 integrator from rk4.py.</span>
17781767
<span style="color: #CD5555"> Returns t, x, v arrays.</span>
17791768
<span style="color: #CD5555"> &quot;&quot;&quot;</span>
17801769
n = <span style="color: #658b00">int</span>(np.ceil(tfinal / DeltaT))
@@ -1817,9 +1806,7 @@ <h2 id="using-the-above-data-to-train-an-rnn">Using the above data to train an R
18171806
<span style="color: #8B008B; font-weight: bold">return</span> t, x, v
18181807

18191808

1820-
<span style="color: #228B22"># =====================================</span>
1821-
<span style="color: #228B22"># 2. Sequence generation for RNN training</span>
1822-
<span style="color: #228B22"># =====================================</span>
1809+
<span style="color: #228B22"># Sequence generation for RNN training</span>
18231810

18241811
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">create_sequences</span>(x, seq_len):
18251812
<span style="color: #CD5555">&quot;&quot;&quot;</span>
@@ -1862,9 +1849,7 @@ <h2 id="using-the-above-data-to-train-an-rnn">Using the above data to train an R
18621849
<span style="color: #8B008B; font-weight: bold">return</span> <span style="color: #658b00">self</span>.inputs[idx], <span style="color: #658b00">self</span>.targets[idx]
18631850

18641851

1865-
<span style="color: #228B22"># ==============================</span>
1866-
<span style="color: #228B22"># 3. RNN model (LSTM-based)</span>
1867-
<span style="color: #228B22"># ==============================</span>
1852+
<span style="color: #228B22"># RNN model (LSTM-based in this example)</span>
18681853

18691854
<span style="color: #8B008B; font-weight: bold">class</span> <span style="color: #008b45; font-weight: bold">RNNPredictor</span>(nn.Module):
18701855
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">__init__</span>(<span style="color: #658b00">self</span>, input_size=<span style="color: #B452CD">1</span>, hidden_size=<span style="color: #B452CD">32</span>, num_layers=<span style="color: #B452CD">1</span>, output_size=<span style="color: #B452CD">1</span>):
@@ -1882,9 +1867,7 @@ <h2 id="using-the-above-data-to-train-an-rnn">Using the above data to train an R
18821867
<span style="color: #8B008B; font-weight: bold">return</span> out
18831868

18841869

1885-
<span style="color: #228B22"># ==============================</span>
1886-
<span style="color: #228B22"># 4. Training loop</span>
1887-
<span style="color: #228B22"># ==============================</span>
1870+
<span style="color: #228B22"># Training part</span>
18881871

18891872
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">train_model</span>(
18901873
seq_len=<span style="color: #B452CD">50</span>,
@@ -1933,9 +1916,7 @@ <h2 id="using-the-above-data-to-train-an-rnn">Using the above data to train an R
19331916
<span style="color: #8B008B; font-weight: bold">return</span> model, dataset
19341917

19351918

1936-
<span style="color: #228B22"># ==============================</span>
1937-
<span style="color: #228B22"># 5. Evaluation / visualization</span>
1938-
<span style="color: #228B22"># ==============================</span>
1919+
<span style="color: #228B22"># Evaluation / visualization</span>
19391920

19401921
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">evaluate_and_plot</span>(model, dataset, seq_len=<span style="color: #B452CD">50</span>, device=<span style="color: #8B008B; font-weight: bold">None</span>):
19411922
<span style="color: #8B008B; font-weight: bold">if</span> device <span style="color: #8B008B">is</span> <span style="color: #8B008B; font-weight: bold">None</span>:
@@ -1946,13 +1927,10 @@ <h2 id="using-the-above-data-to-train-an-rnn">Using the above data to train an R
19461927
<span style="color: #228B22"># Take a single sequence from the dataset</span>
19471928
x_seq, y_seq = dataset[<span style="color: #B452CD">0</span>] <span style="color: #228B22"># shapes: (seq_len, 1), (seq_len, 1)</span>
19481929
x_input = x_seq.unsqueeze(<span style="color: #B452CD">0</span>).to(device) <span style="color: #228B22"># (1, seq_len, 1)</span>
1949-
19501930
<span style="color: #228B22"># Model prediction (next-step for whole sequence)</span>
19511931
y_pred = model(x_input).cpu().numpy().squeeze(-<span style="color: #B452CD">1</span>).squeeze(<span style="color: #B452CD">0</span>) <span style="color: #228B22"># (seq_len,)</span>
1952-
19531932
<span style="color: #228B22"># True target</span>
19541933
y_true = y_seq.numpy().squeeze(-<span style="color: #B452CD">1</span>) <span style="color: #228B22"># (seq_len,)</span>
1955-
19561934
<span style="color: #228B22"># Plot comparison</span>
19571935
plt.figure()
19581936
plt.plot(y_true, label=<span style="color: #CD5555">&quot;True x(t+Δt)&quot;</span>, linestyle=<span style="color: #CD5555">&quot;-&quot;</span>)
@@ -1964,10 +1942,7 @@ <h2 id="using-the-above-data-to-train-an-rnn">Using the above data to train an R
19641942
plt.tight_layout()
19651943
plt.show()
19661944

1967-
1968-
<span style="color: #228B22"># ==============================</span>
1969-
<span style="color: #228B22"># 6. Main</span>
1970-
<span style="color: #228B22"># ==============================</span>
1945+
<span style="color: #228B22"># This is the main part of the code where we define the network</span>
19711946

19721947
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #00688B">__name__</span> == <span style="color: #CD5555">&quot;__main__&quot;</span>:
19731948
<span style="color: #228B22"># Hyperparameters can be tweaked as you like</span>

0 commit comments

Comments
 (0)