|
| 1 | +<!-- |
| 2 | +HTML file automatically generated from DocOnce source |
| 3 | +(https://github.com/doconce/doconce/) |
| 4 | +doconce format html back.do.txt --pygments_html_style=perldoc --html_style=solarized3 --html_links_in_new_window --html_output=back-solarized --no_mako |
| 5 | +--> |
| 6 | +<html> |
| 7 | +<head> |
| 8 | +<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 9 | +<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" /> |
| 10 | +<meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 11 | +<meta name="description" content="Advanced machine learning and data analysis for the physical sciences"> |
| 12 | +<title>Advanced machine learning and data analysis for the physical sciences</title> |
| 13 | +<link href="https://cdn.rawgit.com/doconce/doconce/master/bundled/html_styles/style_solarized_box/css/solarized_light_code.css" rel="stylesheet" type="text/css" title="light"/> |
| 14 | +<script src="https://cdn.rawgit.com/doconce/doconce/master/bundled/html_styles/style_solarized_box/js/highlight.pack.js"></script> |
| 15 | +<script>hljs.initHighlightingOnLoad();</script> |
| 16 | +<link href="https://thomasf.github.io/solarized-css/solarized-light.min.css" rel="stylesheet"> |
| 17 | +<style type="text/css"> |
| 18 | +h1 {color: #b58900;} /* yellow */ |
| 19 | +/* h1 {color: #cb4b16;} orange */ |
| 20 | +/* h1 {color: #d33682;} magenta, the original choice of thomasf */ |
| 21 | +code { padding: 0px; background-color: inherit; } |
| 22 | +pre { |
| 23 | + border: 0pt solid #93a1a1; |
| 24 | + box-shadow: none; |
| 25 | +} |
| 26 | +div { text-align: justify; text-justify: inter-word; } |
| 27 | +.tab { |
| 28 | + padding-left: 1.5em; |
| 29 | +} |
| 30 | +div.toc p,a { |
| 31 | + line-height: 1.3; |
| 32 | + margin-top: 1.1; |
| 33 | + margin-bottom: 1.1; |
| 34 | +} |
| 35 | +</style> |
| 36 | +</head> |
| 37 | + |
| 38 | +<!-- tocinfo |
| 39 | +{'highest level': 2, |
| 40 | + 'sections': [('Imports and Utilities', 2, None, 'imports-and-utilities')]} |
| 41 | +end of tocinfo --> |
| 42 | + |
| 43 | +<body> |
| 44 | +<!-- ------------------- main content ---------------------- --> |
| 45 | +<center> |
| 46 | +<h1>Advanced machine learning and data analysis for the physical sciences</h1> |
| 47 | +</center> <!-- document title --> |
| 48 | + |
| 49 | +<!-- author(s): Morten Hjorth-Jensen --> |
| 50 | +<center> |
| 51 | +<b>Morten Hjorth-Jensen</b> |
| 52 | +</center> |
| 53 | +<!-- institution --> |
| 54 | +<center> |
| 55 | +<b>Department of Physics and Center for Computing in Science Education, University of Oslo, Norway</b> |
| 56 | +</center> |
| 57 | +<br> |
| 58 | +<center> |
| 59 | +<h4>May 8, 2025</h4> |
| 60 | +</center> <!-- date --> |
| 61 | +<br> |
| 62 | + |
| 63 | +<!-- !split --><br><br><br><br><br><br><br><br><br><br> |
| 64 | +<h2 id="imports-and-utilities">Imports and Utilities </h2> |
| 65 | + |
| 66 | + |
| 67 | +<!-- code=python (!bc pycod) typeset with pygments style "perldoc" --> |
| 68 | +<div class="cell border-box-sizing code_cell rendered"> |
| 69 | + <div class="input"> |
| 70 | + <div class="inner_cell"> |
| 71 | + <div class="input_area"> |
| 72 | + <div class="highlight" style="background: #eeeedd"> |
| 73 | + <pre style="line-height: 125%;"><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">torch</span> |
| 74 | +<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">torch.nn</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">nn</span> |
| 75 | +<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">torch.nn.functional</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">F</span> |
| 76 | +<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">torchvision</span> <span style="color: #8B008B; font-weight: bold">import</span> datasets, transforms |
| 77 | +<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">torch.utils.data</span> <span style="color: #8B008B; font-weight: bold">import</span> DataLoader |
| 78 | +<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> |
| 79 | +<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">math</span> |
| 80 | + |
| 81 | +device = <span style="color: #CD5555">'cuda'</span> <span style="color: #8B008B; font-weight: bold">if</span> torch.cuda.is_available() <span style="color: #8B008B; font-weight: bold">else</span> <span style="color: #CD5555">'cpu'</span> |
| 82 | + |
| 83 | +<span style="color: #228B22"># Training settings</span> |
| 84 | +batch_size = <span style="color: #B452CD">128</span> |
| 85 | +epochs = <span style="color: #B452CD">5</span> |
| 86 | +lr = <span style="color: #B452CD">2e-4</span> |
| 87 | +img_size = <span style="color: #B452CD">28</span> |
| 88 | +channels = <span style="color: #B452CD">1</span> |
| 89 | + |
| 90 | +<span style="color: #228B22"># Diffusion hyperparameters</span> |
| 91 | +T = <span style="color: #B452CD">300</span> <span style="color: #228B22"># number of diffusion steps [oai_citation:5‡Medium](https://papers-100-lines.medium.com/diffusion-models-from-scratch-mnist-data-tutorial-in-100-lines-of-pytorch-code-a609e1558cee?utm_source=chatgpt.com)</span> |
| 92 | +beta_start, beta_end = <span style="color: #B452CD">1e-4</span>, <span style="color: #B452CD">0.02</span> |
| 93 | +betas = torch.linspace(beta_start, beta_end, T, device=device) <span style="color: #228B22"># linear schedule [oai_citation:6‡Medium](https://medium.com/data-science/diffusion-model-from-scratch-in-pytorch-ddpm-9d9760528946?utm_source=chatgpt.com)</span> |
| 94 | +alphas = <span style="color: #B452CD">1.</span> - betas |
| 95 | +alphas_cumprod = torch.cumprod(alphas, dim=<span style="color: #B452CD">0</span>) |
| 96 | + |
| 97 | +transform = transforms.Compose([ |
| 98 | + transforms.ToTensor(), |
| 99 | + transforms.Normalize((<span style="color: #B452CD">0.5</span>,), (<span style="color: #B452CD">0.5</span>,)), |
| 100 | +]) |
| 101 | + |
| 102 | +train_ds = datasets.MNIST(<span style="color: #CD5555">'.'</span>, train=<span style="color: #8B008B; font-weight: bold">True</span>, download=<span style="color: #8B008B; font-weight: bold">True</span>, transform=transform) |
| 103 | +train_loader = DataLoader(train_ds, batch_size=batch_size, shuffle=<span style="color: #8B008B; font-weight: bold">True</span>) |
| 104 | + |
| 105 | +<span style="color: #8B008B; font-weight: bold">class</span> <span style="color: #008b45; font-weight: bold">SimpleUNet</span>(nn.Module): |
| 106 | + <span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">__init__</span>(<span style="color: #658b00">self</span>, c): |
| 107 | + <span style="color: #658b00">super</span>().<span style="color: #008b45">__init__</span>() |
| 108 | + <span style="color: #658b00">self</span>.enc1 = nn.Conv2d(c, <span style="color: #B452CD">64</span>, <span style="color: #B452CD">3</span>, padding=<span style="color: #B452CD">1</span>) |
| 109 | + <span style="color: #658b00">self</span>.enc2 = nn.Conv2d(<span style="color: #B452CD">64</span>, <span style="color: #B452CD">128</span>, <span style="color: #B452CD">3</span>, padding=<span style="color: #B452CD">1</span>) |
| 110 | + <span style="color: #658b00">self</span>.dec1 = nn.ConvTranspose2d(<span style="color: #B452CD">128</span>, <span style="color: #B452CD">64</span>, <span style="color: #B452CD">3</span>, padding=<span style="color: #B452CD">1</span>) |
| 111 | + <span style="color: #658b00">self</span>.dec2 = nn.ConvTranspose2d(<span style="color: #B452CD">64</span>, c, <span style="color: #B452CD">3</span>, padding=<span style="color: #B452CD">1</span>) |
| 112 | + <span style="color: #658b00">self</span>.act = nn.ReLU() |
| 113 | + <span style="color: #228B22"># timestep embedding to condition on t</span> |
| 114 | + <span style="color: #658b00">self</span>.time_mlp = nn.Sequential( |
| 115 | + nn.Linear(<span style="color: #B452CD">1</span>, <span style="color: #B452CD">128</span>), <span style="color: #228B22"># Changed from 64 to 128</span> |
| 116 | + nn.ReLU(), |
| 117 | + nn.Linear(<span style="color: #B452CD">128</span>, <span style="color: #B452CD">128</span>), <span style="color: #228B22"># Changed from 64 to 128</span> |
| 118 | + ) |
| 119 | + |
| 120 | + <span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">forward</span>(<span style="color: #658b00">self</span>, x, t): |
| 121 | + <span style="color: #228B22"># x: [B, C, H, W], t: [B]</span> |
| 122 | + h = <span style="color: #658b00">self</span>.act(<span style="color: #658b00">self</span>.enc1(x)) |
| 123 | + h = <span style="color: #658b00">self</span>.act(<span style="color: #658b00">self</span>.enc2(h)) |
| 124 | + <span style="color: #228B22"># add time embedding</span> |
| 125 | + t = t.unsqueeze(-<span style="color: #B452CD">1</span>) |
| 126 | + temb = <span style="color: #658b00">self</span>.time_mlp(t) |
| 127 | + temb = temb.view(-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">128</span>, <span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>) <span style="color: #228B22"># Changed from 64 to 128</span> |
| 128 | + h = h + temb |
| 129 | + h = <span style="color: #658b00">self</span>.act(<span style="color: #658b00">self</span>.dec1(h)) |
| 130 | + <span style="color: #8B008B; font-weight: bold">return</span> <span style="color: #658b00">self</span>.dec2(h) |
| 131 | + |
| 132 | +<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">q_sample</span>(x0, t, noise=<span style="color: #8B008B; font-weight: bold">None</span>): |
| 133 | + <span style="color: #CD5555">"""Add noise to x0 at timestep t."""</span> |
| 134 | + <span style="color: #8B008B; font-weight: bold">if</span> noise <span style="color: #8B008B">is</span> <span style="color: #8B008B; font-weight: bold">None</span>: |
| 135 | + noise = torch.randn_like(x0) |
| 136 | + sqrt_acp = alphas_cumprod[t]**<span style="color: #B452CD">0.5</span> |
| 137 | + sqrt_1macp = (<span style="color: #B452CD">1</span> - alphas_cumprod[t])**<span style="color: #B452CD">0.5</span> |
| 138 | + <span style="color: #8B008B; font-weight: bold">return</span> sqrt_acp.view(-<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>)*x0 + sqrt_1macp.view(-<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>)*noise |
| 139 | + |
| 140 | +<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">diffusion_loss</span>(model, x0): |
| 141 | + <span style="color: #CD5555">"""Compute MSE between predicted noise and true noise."""</span> |
| 142 | + B = x0.size(<span style="color: #B452CD">0</span>) |
| 143 | + t = torch.randint(<span style="color: #B452CD">0</span>, T, (B,), device=device).long() |
| 144 | + noise = torch.randn_like(x0) |
| 145 | + x_noisy = q_sample(x0, t, noise) |
| 146 | + pred_noise = model(x_noisy, t.float()/T) |
| 147 | + <span style="color: #8B008B; font-weight: bold">return</span> F.mse_loss(pred_noise, noise) |
| 148 | + |
| 149 | +model = SimpleUNet(channels).to(device) |
| 150 | +opt = torch.optim.Adam(model.parameters(), lr=lr) |
| 151 | + |
| 152 | +<span style="color: #8B008B; font-weight: bold">for</span> epoch <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(epochs): |
| 153 | + total_loss = <span style="color: #B452CD">0</span> |
| 154 | + <span style="color: #8B008B; font-weight: bold">for</span> x, _ <span style="color: #8B008B">in</span> train_loader: |
| 155 | + x = x.to(device) |
| 156 | + loss = diffusion_loss(model, x) |
| 157 | + opt.zero_grad() |
| 158 | + loss.backward() |
| 159 | + opt.step() |
| 160 | + total_loss += loss.item() |
| 161 | + <span style="color: #658b00">print</span>(<span style="color: #CD5555">f"Epoch {</span>epoch+<span style="color: #B452CD">1</span><span style="color: #CD5555">}/{</span>epochs<span style="color: #CD5555">}, Loss: {</span>total_loss/<span style="color: #658b00">len</span>(train_loader)<span style="color: #CD5555">:.4f}"</span>) |
| 162 | + |
| 163 | +<span style="color: #707a7c">@torch</span>.no_grad() |
| 164 | +<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">p_sample_loop</span>(model, shape): |
| 165 | + x = torch.randn(shape, device=device) |
| 166 | + <span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">reversed</span>(<span style="color: #658b00">range</span>(T)): |
| 167 | + t = torch.full((shape[<span style="color: #B452CD">0</span>],), i, device=device).float()/T |
| 168 | + eps_pred = model(x, t) |
| 169 | + beta_t = betas[i] |
| 170 | + alpha_t = alphas[i] |
| 171 | + acp_t = alphas_cumprod[i] |
| 172 | + coef1 = <span style="color: #B452CD">1</span> / alpha_t.sqrt() |
| 173 | + coef2 = beta_t / ( (<span style="color: #B452CD">1</span> - acp_t).sqrt() ) |
| 174 | + x = coef1*(x - coef2*eps_pred) |
| 175 | + <span style="color: #8B008B; font-weight: bold">if</span> i > <span style="color: #B452CD">0</span>: |
| 176 | + z = torch.randn_like(x) |
| 177 | + sigma = beta_t.sqrt() |
| 178 | + x = x + sigma*z |
| 179 | + <span style="color: #8B008B; font-weight: bold">return</span> x |
| 180 | + |
| 181 | +<span style="color: #228B22"># Generate samples</span> |
| 182 | +samples = p_sample_loop(model, (<span style="color: #B452CD">16</span>, channels, img_size, img_size)) |
| 183 | +samples = samples.clamp(-<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>).cpu() |
| 184 | +grid = torchvision.utils.make_grid(samples, nrow=<span style="color: #B452CD">4</span>, normalize=<span style="color: #8B008B; font-weight: bold">True</span>) |
| 185 | +plt.figure(figsize=(<span style="color: #B452CD">5</span>,<span style="color: #B452CD">5</span>)) |
| 186 | +plt.imshow(grid.permute(<span style="color: #B452CD">1</span>,<span style="color: #B452CD">2</span>,<span style="color: #B452CD">0</span>)) |
| 187 | +plt.axis(<span style="color: #CD5555">'off'</span>) |
| 188 | +</pre> |
| 189 | +</div> |
| 190 | + </div> |
| 191 | + </div> |
| 192 | + </div> |
| 193 | + <div class="output_wrapper"> |
| 194 | + <div class="output"> |
| 195 | + <div class="output_area"> |
| 196 | + <div class="output_subarea output_stream output_stdout output_text"> |
| 197 | + </div> |
| 198 | + </div> |
| 199 | + </div> |
| 200 | + </div> |
| 201 | +</div> |
| 202 | + |
| 203 | +<!-- ------------------- end of main content --------------- --> |
| 204 | +<center style="font-size:80%"> |
| 205 | +<!-- copyright --> © 1999-2025, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license |
| 206 | +</center> |
| 207 | +</body> |
| 208 | +</html> |
| 209 | + |
0 commit comments