-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
671 lines (649 loc) · 55.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.2.335">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>popgendl_crashcourse</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<script src="popgenDL_crashcourse_files/libs/clipboard/clipboard.min.js"></script>
<script src="popgenDL_crashcourse_files/libs/quarto-html/quarto.js"></script>
<script src="popgenDL_crashcourse_files/libs/quarto-html/popper.min.js"></script>
<script src="popgenDL_crashcourse_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="popgenDL_crashcourse_files/libs/quarto-html/anchor.min.js"></script>
<link href="popgenDL_crashcourse_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="popgenDL_crashcourse_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="popgenDL_crashcourse_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="popgenDL_crashcourse_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="popgenDL_crashcourse_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" integrity="sha512-c3Nl8+7g4LMSTdrm621y7kf9v3SDPnhxLNhcjFJbKECVnmZHTdo+IRO05sNLTH/D3vA6u1X32ehoLC7WFVdheg==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
<script type="application/javascript">define('jquery', [],function() {return window.jQuery;})</script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
</head>
<body class="fullcontent">
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<main class="content" id="quarto-document-content">
<section id="deep-learning-popgen-crash-course" class="level1">
<h1>Deep Learning Popgen Crash Course</h1>
<p>Here’s a quick tutorial on how you might deploy deep learning to solve a popgen problem.</p>
<p>The lab has historically used Tensorflow and Keras, but we’re moving toward PyTorch, so we’ll use that here. A lot of the details overlap, and it’s useful to know both (at least a little).</p>
<ol type="1">
<li>Define your problem</li>
<li>Make an environment with all your dependencies</li>
<li>Generate your training data</li>
<li>Make a generator to feed your data to your model</li>
<li>Define your model architecture (and loss and optimizer)</li>
<li>Train your model</li>
<li>Evaluate your model</li>
</ol>
<section id="define-your-problem" class="level2">
<h2 class="anchored" data-anchor-id="define-your-problem">Define your problem</h2>
<p>As a simple example, let’s predict long term <span class="math inline">\(N_e\)</span> from a single diploid individual. We’ll assume we know recombination and mutation rate and that population size is constant. Using a single diploid simplifies the input quite a bit by not having to worry about permutations of rows, sample size, etc. Those may not be things you can avoid thinking about in the problem you’re working on, but for now, let’s keep it simple.</p>
<section id="make-an-environment-with-all-your-dependencies" class="level3">
<h3 class="anchored" data-anchor-id="make-an-environment-with-all-your-dependencies">Make an environment with all your dependencies</h3>
<p>I’ve made a popgenDL_crashcourse.yml file that you can use to create a conda environment with all the dependencies you need. This is likely as good starting point for most popgenML problems, but easy to imagine other useful libraries you’d want to add.<br>
You can build the environment with</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="ex">mamba</span> env create <span class="at">-f</span> popgenDL_crashcourse.yml</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>Here’s what it looks like</p>
<div class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="op">!</span> cat popgenDL_crashcourse.yml</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#conda yaml file
name: popgenDL_crashcourse
channels:
- bioconda
- conda-forge
- defaults
dependencies:
- pytorch
- torchvision
- cudatoolkit
- ipykernel
- jupyter
- jupyter-cache
- msprime
- gpustat
- matplotlib
- numpy
- pandas
- r-quarto</code></pre>
</div>
</div>
</section>
<section id="get-stuff-imported-and-a-gpu-ready-to-go." class="level3">
<h3 class="anchored" data-anchor-id="get-stuff-imported-and-a-gpu-ready-to-go.">Get stuff imported and a GPU ready to go.</h3>
<p>Next let’s load stuff in and find the least busy GPU.</p>
<div class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> msprime</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> numpy <span class="im">as</span> np</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> numpy.random <span class="im">import</span> default_rng</span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pandas <span class="im">as</span> pd</span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> matplotlib.pyplot <span class="im">as</span> plt</span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> torch</span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> os</span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> gpustat</span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> concurrent.futures</span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> get_idle_gpu():</span>
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true" tabindex="-1"></a> <span class="co">"""</span></span>
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true" tabindex="-1"></a><span class="co"> Utility function which uses the gpustat module to select the</span></span>
<span id="cb4-15"><a href="#cb4-15" aria-hidden="true" tabindex="-1"></a><span class="co"> least busy GPU that is available and then sets the</span></span>
<span id="cb4-16"><a href="#cb4-16" aria-hidden="true" tabindex="-1"></a><span class="co"> CUDA_VISIBLE_DEVICES environment variable so that</span></span>
<span id="cb4-17"><a href="#cb4-17" aria-hidden="true" tabindex="-1"></a><span class="co"> only that GPU is used</span></span>
<span id="cb4-18"><a href="#cb4-18" aria-hidden="true" tabindex="-1"></a><span class="co"> """</span></span>
<span id="cb4-19"><a href="#cb4-19" aria-hidden="true" tabindex="-1"></a> <span class="cf">try</span>:</span>
<span id="cb4-20"><a href="#cb4-20" aria-hidden="true" tabindex="-1"></a> stats <span class="op">=</span> gpustat.GPUStatCollection.new_query()</span>
<span id="cb4-21"><a href="#cb4-21" aria-hidden="true" tabindex="-1"></a> ids <span class="op">=</span> <span class="bu">map</span>(<span class="kw">lambda</span> gpu: <span class="bu">int</span>(gpu.entry[<span class="st">"index"</span>]), stats)</span>
<span id="cb4-22"><a href="#cb4-22" aria-hidden="true" tabindex="-1"></a> ratios <span class="op">=</span> <span class="bu">map</span>(</span>
<span id="cb4-23"><a href="#cb4-23" aria-hidden="true" tabindex="-1"></a> <span class="kw">lambda</span> gpu: <span class="bu">float</span>(gpu.entry[<span class="st">"memory.used"</span>])</span>
<span id="cb4-24"><a href="#cb4-24" aria-hidden="true" tabindex="-1"></a> <span class="op">/</span> <span class="bu">float</span>(gpu.entry[<span class="st">"memory.total"</span>]),</span>
<span id="cb4-25"><a href="#cb4-25" aria-hidden="true" tabindex="-1"></a> stats,</span>
<span id="cb4-26"><a href="#cb4-26" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb4-27"><a href="#cb4-27" aria-hidden="true" tabindex="-1"></a> bestGPU <span class="op">=</span> <span class="bu">min</span>(<span class="bu">zip</span>(ids, ratios), key<span class="op">=</span><span class="kw">lambda</span> x: x[<span class="dv">1</span>])[<span class="dv">0</span>]</span>
<span id="cb4-28"><a href="#cb4-28" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> bestGPU</span>
<span id="cb4-29"><a href="#cb4-29" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-30"><a href="#cb4-30" aria-hidden="true" tabindex="-1"></a> <span class="cf">except</span> <span class="pp">Exception</span>:</span>
<span id="cb4-31"><a href="#cb4-31" aria-hidden="true" tabindex="-1"></a> <span class="cf">pass</span></span>
<span id="cb4-32"><a href="#cb4-32" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-33"><a href="#cb4-33" aria-hidden="true" tabindex="-1"></a><span class="co">#this device variable will get used later during training</span></span>
<span id="cb4-34"><a href="#cb4-34" aria-hidden="true" tabindex="-1"></a>device <span class="op">=</span> torch.device(<span class="ss">f"cuda:</span><span class="sc">{</span>get_idle_gpu()<span class="sc">}</span><span class="ss">"</span> <span class="cf">if</span> torch.cuda.is_available() <span class="cf">else</span> <span class="st">"cpu"</span>)</span>
<span id="cb4-35"><a href="#cb4-35" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(device)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>cuda:0</code></pre>
</div>
</div>
</section>
</section>
<section id="generate-your-training-data" class="level2">
<h2 class="anchored" data-anchor-id="generate-your-training-data">Generate your training data</h2>
<p>As opposed to using emprical data sampled from the real world, we often use simulations to generate training data.</p>
<p>Let’s set up a data class that will store all the details of our simulation and training. It handles doing the simulations and making a data frame that tracks the files that it writes to disk. It will also randomly assign each sim to be training, validation, or test data.</p>
<p>This (or something like it) will help to make sure we aren’t redefining the same parameters in different places, and make our function and method definitions simpler, since we can just pass the data object around rather than individuals parameter values.</p>
<p>The PopGenTraining class is specific to our problem (estimaing Ne), but adopting it for other problems should be straightforward. It’s kinda big, worth the effort for downstream stuff!</p>
<div class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> dataclasses <span class="im">import</span> dataclass</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> typing <span class="im">import</span> List</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a><span class="at">@dataclass</span></span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> PopGenTraining():</span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> <span class="co">"""</span></span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a><span class="co"> Dataclass to hold the parameters and methods for the simulation and training</span></span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a><span class="co"> :param int seed: random seed</span></span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a><span class="co"> :param int N_reps: number of replicates to simulate</span></span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a><span class="co"> :param float sequence_length: length of the simulated sequence</span></span>
<span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a><span class="co"> :param float Ne_scaling: scaling factor for the effective population size</span></span>
<span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a><span class="co"> :param float recombination_rate: recombination rate</span></span>
<span id="cb6-14"><a href="#cb6-14" aria-hidden="true" tabindex="-1"></a><span class="co"> :param float mutation_rate: mutation rate</span></span>
<span id="cb6-15"><a href="#cb6-15" aria-hidden="true" tabindex="-1"></a><span class="co"> :param float Ne_low: lower bound for the effective population size</span></span>
<span id="cb6-16"><a href="#cb6-16" aria-hidden="true" tabindex="-1"></a><span class="co"> :param float Ne_high: upper bound for the effective population size</span></span>
<span id="cb6-17"><a href="#cb6-17" aria-hidden="true" tabindex="-1"></a><span class="co"> :param int numsites: number of sites to simulate</span></span>
<span id="cb6-18"><a href="#cb6-18" aria-hidden="true" tabindex="-1"></a><span class="co"> :param str prefix: prefix for the output files -- full path is encouraged</span></span>
<span id="cb6-19"><a href="#cb6-19" aria-hidden="true" tabindex="-1"></a><span class="co"> :param List[float] training_fractions: fractions of the data to use for training, validation, and testing</span></span>
<span id="cb6-20"><a href="#cb6-20" aria-hidden="true" tabindex="-1"></a><span class="co"> """</span> </span>
<span id="cb6-21"><a href="#cb6-21" aria-hidden="true" tabindex="-1"></a> seed: <span class="bu">int</span></span>
<span id="cb6-22"><a href="#cb6-22" aria-hidden="true" tabindex="-1"></a> N_reps: <span class="bu">int</span></span>
<span id="cb6-23"><a href="#cb6-23" aria-hidden="true" tabindex="-1"></a> sequence_length: <span class="bu">float</span></span>
<span id="cb6-24"><a href="#cb6-24" aria-hidden="true" tabindex="-1"></a> Ne_scaling: <span class="bu">float</span></span>
<span id="cb6-25"><a href="#cb6-25" aria-hidden="true" tabindex="-1"></a> recombination_rate: <span class="bu">float</span></span>
<span id="cb6-26"><a href="#cb6-26" aria-hidden="true" tabindex="-1"></a> mutation_rate: <span class="bu">float</span></span>
<span id="cb6-27"><a href="#cb6-27" aria-hidden="true" tabindex="-1"></a> Ne_low: <span class="bu">float</span></span>
<span id="cb6-28"><a href="#cb6-28" aria-hidden="true" tabindex="-1"></a> Ne_high: <span class="bu">float</span></span>
<span id="cb6-29"><a href="#cb6-29" aria-hidden="true" tabindex="-1"></a> numsites: <span class="bu">int</span></span>
<span id="cb6-30"><a href="#cb6-30" aria-hidden="true" tabindex="-1"></a> prefix: <span class="bu">str</span></span>
<span id="cb6-31"><a href="#cb6-31" aria-hidden="true" tabindex="-1"></a> training_fractions: List[<span class="bu">float</span>]</span>
<span id="cb6-32"><a href="#cb6-32" aria-hidden="true" tabindex="-1"></a> n_cpu: <span class="bu">int</span> <span class="op">=</span> <span class="dv">1</span></span>
<span id="cb6-33"><a href="#cb6-33" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb6-34"><a href="#cb6-34" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> __post_init__(<span class="va">self</span>):</span>
<span id="cb6-35"><a href="#cb6-35" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> <span class="bu">sum</span>(<span class="va">self</span>.training_fractions) <span class="op">!=</span> <span class="dv">1</span>:</span>
<span id="cb6-36"><a href="#cb6-36" aria-hidden="true" tabindex="-1"></a> <span class="cf">raise</span> <span class="pp">ValueError</span>(<span class="st">"training_fractions must sum to 1"</span>)</span>
<span id="cb6-37"><a href="#cb6-37" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb6-38"><a href="#cb6-38" aria-hidden="true" tabindex="-1"></a> training_fraction, validation_fraction, test_fraction <span class="op">=</span> <span class="va">self</span>.training_fractions</span>
<span id="cb6-39"><a href="#cb6-39" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb6-40"><a href="#cb6-40" aria-hidden="true" tabindex="-1"></a> <span class="co"># list of labels for each simulation</span></span>
<span id="cb6-41"><a href="#cb6-41" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.training_labels <span class="op">=</span> [<span class="st">"training"</span>] <span class="op">*</span> <span class="bu">int</span>(training_fraction <span class="op">*</span> <span class="va">self</span>.N_reps) <span class="op">+</span> <span class="op">\</span></span>
<span id="cb6-42"><a href="#cb6-42" aria-hidden="true" tabindex="-1"></a> [<span class="st">"validation"</span>] <span class="op">*</span> <span class="bu">int</span>(validation_fraction <span class="op">*</span> <span class="va">self</span>.N_reps) <span class="op">+</span> <span class="op">\</span></span>
<span id="cb6-43"><a href="#cb6-43" aria-hidden="true" tabindex="-1"></a> [<span class="st">"test"</span>] <span class="op">*</span> <span class="bu">int</span>(test_fraction <span class="op">*</span> <span class="va">self</span>.N_reps)</span>
<span id="cb6-44"><a href="#cb6-44" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb6-45"><a href="#cb6-45" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> <span class="bu">len</span>(<span class="va">self</span>.training_labels) <span class="op">!=</span> <span class="va">self</span>.N_reps:</span>
<span id="cb6-46"><a href="#cb6-46" aria-hidden="true" tabindex="-1"></a> <span class="cf">raise</span> <span class="pp">ValueError</span>(<span class="st">"wrong number of training labels. Check training_fractions"</span>)</span>
<span id="cb6-47"><a href="#cb6-47" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb6-48"><a href="#cb6-48" aria-hidden="true" tabindex="-1"></a> <span class="co"># seed the random number generator</span></span>
<span id="cb6-49"><a href="#cb6-49" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.rng <span class="op">=</span> default_rng(<span class="va">self</span>.seed)</span>
<span id="cb6-50"><a href="#cb6-50" aria-hidden="true" tabindex="-1"></a> <span class="co"># seed for each simulation</span></span>
<span id="cb6-51"><a href="#cb6-51" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.seeds <span class="op">=</span> <span class="va">self</span>.rng.integers(<span class="dv">0</span>, <span class="dv">2</span><span class="op">**</span><span class="dv">32</span>, size <span class="op">=</span> <span class="va">self</span>.N_reps)</span>
<span id="cb6-52"><a href="#cb6-52" aria-hidden="true" tabindex="-1"></a> <span class="co"># The population size values to simulate under! </span></span>
<span id="cb6-53"><a href="#cb6-53" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.Ne <span class="op">=</span> <span class="va">self</span>.rng.uniform(</span>
<span id="cb6-54"><a href="#cb6-54" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.Ne_low, </span>
<span id="cb6-55"><a href="#cb6-55" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.Ne_high, </span>
<span id="cb6-56"><a href="#cb6-56" aria-hidden="true" tabindex="-1"></a> size<span class="op">=</span><span class="va">self</span>.N_reps</span>
<span id="cb6-57"><a href="#cb6-57" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb6-58"><a href="#cb6-58" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-59"><a href="#cb6-59" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> simulate(<span class="va">self</span>, params_tuple):</span>
<span id="cb6-60"><a href="#cb6-60" aria-hidden="true" tabindex="-1"></a> ne, seed, file_name <span class="op">=</span> params_tuple</span>
<span id="cb6-61"><a href="#cb6-61" aria-hidden="true" tabindex="-1"></a> ts <span class="op">=</span> msprime.sim_ancestry(</span>
<span id="cb6-62"><a href="#cb6-62" aria-hidden="true" tabindex="-1"></a> samples <span class="op">=</span> <span class="dv">1</span>,</span>
<span id="cb6-63"><a href="#cb6-63" aria-hidden="true" tabindex="-1"></a> sequence_length <span class="op">=</span> <span class="va">self</span>.sequence_length,</span>
<span id="cb6-64"><a href="#cb6-64" aria-hidden="true" tabindex="-1"></a> recombination_rate <span class="op">=</span> <span class="va">self</span>.recombination_rate,</span>
<span id="cb6-65"><a href="#cb6-65" aria-hidden="true" tabindex="-1"></a> population_size<span class="op">=</span>ne, </span>
<span id="cb6-66"><a href="#cb6-66" aria-hidden="true" tabindex="-1"></a> random_seed<span class="op">=</span>seed</span>
<span id="cb6-67"><a href="#cb6-67" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb6-68"><a href="#cb6-68" aria-hidden="true" tabindex="-1"></a> mts <span class="op">=</span> msprime.sim_mutations(</span>
<span id="cb6-69"><a href="#cb6-69" aria-hidden="true" tabindex="-1"></a> ts, rate <span class="op">=</span> <span class="va">self</span>.mutation_rate, random_seed<span class="op">=</span>seed</span>
<span id="cb6-70"><a href="#cb6-70" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb6-71"><a href="#cb6-71" aria-hidden="true" tabindex="-1"></a> np.save(<span class="bu">file</span><span class="op">=</span>file_name.replace(<span class="st">".npy"</span>, <span class="st">""</span>), arr<span class="op">=</span>mts.tables.sites.position) </span>
<span id="cb6-72"><a href="#cb6-72" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-73"><a href="#cb6-73" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-74"><a href="#cb6-74" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> generate_training(<span class="va">self</span>):</span>
<span id="cb6-75"><a href="#cb6-75" aria-hidden="true" tabindex="-1"></a> file_names <span class="op">=</span> [<span class="ss">f"</span><span class="sc">{</span><span class="va">self</span><span class="sc">.</span>prefix<span class="sc">}</span><span class="ss">_</span><span class="sc">{</span>seed<span class="sc">}</span><span class="ss">_pos.npy"</span> <span class="cf">for</span> seed <span class="kw">in</span> <span class="va">self</span>.seeds]</span>
<span id="cb6-76"><a href="#cb6-76" aria-hidden="true" tabindex="-1"></a> training_dict <span class="op">=</span> {<span class="st">"Ne"</span>: <span class="va">self</span>.Ne, <span class="st">"path"</span>:file_names, <span class="st">"training_labels"</span>:<span class="va">self</span>.training_labels}</span>
<span id="cb6-77"><a href="#cb6-77" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-78"><a href="#cb6-78" aria-hidden="true" tabindex="-1"></a> <span class="cf">with</span> concurrent.futures.ProcessPoolExecutor(max_workers<span class="op">=</span><span class="va">self</span>.n_cpu) <span class="im">as</span> executor:</span>
<span id="cb6-79"><a href="#cb6-79" aria-hidden="true" tabindex="-1"></a> line_out <span class="op">=</span> executor.<span class="bu">map</span>(<span class="va">self</span>.simulate, <span class="bu">zip</span>(training_dict[<span class="st">"Ne"</span>], <span class="va">self</span>.seeds, training_dict[<span class="st">"path"</span>]))</span>
<span id="cb6-80"><a href="#cb6-80" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb6-81"><a href="#cb6-81" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> pd.DataFrame(training_dict)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="co">#make the sims! </span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>path <span class="op">=</span> <span class="st">"/sietch_colab/data_share/popgenDL_crashcourse/sims/sims"</span></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Check whether the specified path exists or not</span></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="kw">not</span> os.path.exists(path):</span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> os.makedirs(path)</span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a><span class="co">#instantiate! (make an object from the class)</span></span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a>popgen_training <span class="op">=</span> PopGenTraining(</span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a> seed <span class="op">=</span> <span class="dv">198</span>, </span>
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a> N_reps<span class="op">=</span><span class="dv">500</span>,</span>
<span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a> sequence_length<span class="op">=</span><span class="fl">2e6</span>,</span>
<span id="cb7-13"><a href="#cb7-13" aria-hidden="true" tabindex="-1"></a> Ne_scaling <span class="op">=</span> <span class="fl">1e5</span>,</span>
<span id="cb7-14"><a href="#cb7-14" aria-hidden="true" tabindex="-1"></a> recombination_rate<span class="op">=</span><span class="fl">1e-9</span>,</span>
<span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a> mutation_rate <span class="op">=</span> <span class="fl">1e-8</span>,</span>
<span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a> Ne_low <span class="op">=</span> <span class="fl">1e3</span>,</span>
<span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a> Ne_high<span class="op">=</span> <span class="fl">1e6</span>,</span>
<span id="cb7-18"><a href="#cb7-18" aria-hidden="true" tabindex="-1"></a> numsites <span class="op">=</span> <span class="bu">int</span>(<span class="fl">1e5</span>),</span>
<span id="cb7-19"><a href="#cb7-19" aria-hidden="true" tabindex="-1"></a> prefix <span class="op">=</span> path,</span>
<span id="cb7-20"><a href="#cb7-20" aria-hidden="true" tabindex="-1"></a> training_fractions <span class="op">=</span> [<span class="fl">0.8</span>, <span class="fl">0.1</span>, <span class="fl">0.1</span>],</span>
<span id="cb7-21"><a href="#cb7-21" aria-hidden="true" tabindex="-1"></a> n_cpu <span class="op">=</span> <span class="dv">25</span>,</span>
<span id="cb7-22"><a href="#cb7-22" aria-hidden="true" tabindex="-1"></a>) </span>
<span id="cb7-23"><a href="#cb7-23" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-24"><a href="#cb7-24" aria-hidden="true" tabindex="-1"></a><span class="co">#access a member of the dataclass</span></span>
<span id="cb7-25"><a href="#cb7-25" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"N_reps is </span><span class="sc">{</span>popgen_training<span class="sc">.</span>N_reps<span class="sc">}</span><span class="ss">"</span>)</span>
<span id="cb7-26"><a href="#cb7-26" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-27"><a href="#cb7-27" aria-hidden="true" tabindex="-1"></a><span class="co">#generate the training data</span></span>
<span id="cb7-28"><a href="#cb7-28" aria-hidden="true" tabindex="-1"></a>path_df <span class="op">=</span> popgen_training.generate_training()</span>
<span id="cb7-29"><a href="#cb7-29" aria-hidden="true" tabindex="-1"></a>path_df.to_csv(<span class="st">"sim_paths.csv"</span>, index<span class="op">=</span><span class="va">False</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>N_reps is 500</code></pre>
</div>
</div>
<div class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>path_df <span class="op">=</span> pd.read_csv(<span class="st">"sim_paths.csv"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>path_df.head()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="6">
<div>
<table class="dataframe table table-sm table-striped">
<thead>
<tr>
<th></th>
<th>Ne</th>
<th>path</th>
<th>training_labels</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>841352.287106</td>
<td>/sietch_colab/data_share/popgenDL_crashcourse/...</td>
<td>training</td>
</tr>
<tr>
<th>1</th>
<td>687189.412917</td>
<td>/sietch_colab/data_share/popgenDL_crashcourse/...</td>
<td>training</td>
</tr>
<tr>
<th>2</th>
<td>778880.007331</td>
<td>/sietch_colab/data_share/popgenDL_crashcourse/...</td>
<td>training</td>
</tr>
<tr>
<th>3</th>
<td>979745.246506</td>
<td>/sietch_colab/data_share/popgenDL_crashcourse/...</td>
<td>training</td>
</tr>
<tr>
<th>4</th>
<td>597455.162151</td>
<td>/sietch_colab/data_share/popgenDL_crashcourse/...</td>
<td>training</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<section id="make-a-generator-to-feed-your-data-to-your-model" class="level3">
<h3 class="anchored" data-anchor-id="make-a-generator-to-feed-your-data-to-your-model">Make a generator to feed your data to your model</h3>
<p>For pretty much any problem of interest, you won’t be able to hold all your data in memory. Instead, you’ll iteratively feed in random batches of samples.<br>
Turns out batching data like this also has benefits for optimization, but even if it didn’t help, we would still need to use it to deal with the size of our data sets.</p>
<p>So we’re gonna make a second Class that uses our our previous one to generate batches of data. PyTorch has a nice DataLoader class that makes this easier, but you still have to tell it how to handle your data by using the <code>__getitem__</code> method.</p>
<p>One very tricky thing to get used to in deep learning is the dimensions of your input. Note here that we use the member <code>numsites</code> to define the number of sites in our simulation. If a simulation had more mutations than <code>numsites</code> it gets truncated, fewer get padded by zeros. Are there better alternatives? Maybe! Try experimenting with alternatives.</p>
<div class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> torch.utils.data <span class="im">import</span> Dataset</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> torch.utils.data <span class="im">import</span> DataLoader</span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> torch.nn <span class="im">as</span> nn</span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> PopulationDataset(Dataset):</span>
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, df, popgen_training: PopGenTraining, training_label <span class="op">=</span> <span class="st">"training"</span>): </span>
<span id="cb11-7"><a href="#cb11-7" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.df <span class="op">=</span> df.query(<span class="ss">f"training_labels == '</span><span class="sc">{</span>training_label<span class="sc">}</span><span class="ss">'"</span>)</span>
<span id="cb11-8"><a href="#cb11-8" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.popgen_training <span class="op">=</span> popgen_training</span>
<span id="cb11-9"><a href="#cb11-9" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb11-10"><a href="#cb11-10" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> <span class="fu">__getitem__</span>(<span class="va">self</span>, index):</span>
<span id="cb11-11"><a href="#cb11-11" aria-hidden="true" tabindex="-1"></a> position <span class="op">=</span> torch.tensor(np.load(<span class="va">self</span>.df.iloc[index].path)).to(torch.float32)</span>
<span id="cb11-12"><a href="#cb11-12" aria-hidden="true" tabindex="-1"></a> Ne <span class="op">=</span> torch.tensor(<span class="va">self</span>.df.iloc[index].Ne<span class="op">/</span><span class="va">self</span>.popgen_training.Ne_scaling).reshape(<span class="dv">1</span>)</span>
<span id="cb11-13"><a href="#cb11-13" aria-hidden="true" tabindex="-1"></a> position_array <span class="op">=</span> torch.zeros(<span class="va">self</span>.popgen_training.numsites, dtype<span class="op">=</span>torch.float32)</span>
<span id="cb11-14"><a href="#cb11-14" aria-hidden="true" tabindex="-1"></a> position_array[:<span class="bu">len</span>(position)] <span class="op">=</span> position[:<span class="va">self</span>.popgen_training.numsites]<span class="op">/</span><span class="va">self</span>.popgen_training.sequence_length</span>
<span id="cb11-15"><a href="#cb11-15" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> position_array.to(device), Ne.to(device)</span>
<span id="cb11-16"><a href="#cb11-16" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb11-17"><a href="#cb11-17" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> <span class="fu">__len__</span>(<span class="va">self</span>):</span>
<span id="cb11-18"><a href="#cb11-18" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="bu">len</span>(<span class="va">self</span>.df)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="feed-the-data-loader" class="level3">
<h3 class="anchored" data-anchor-id="feed-the-data-loader">Feed the data loader</h3>
<p>Now we have a nice setup to feed in training data. PyTorch’s <code>DataLoader</code> handles the rest, including shuffling and batching.</p>
<div class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>training_dfs <span class="op">=</span> [PopulationDataset(path_df,popgen_training, training_label <span class="op">=</span> t) <span class="cf">for</span> t <span class="kw">in</span> [<span class="st">"training"</span>, <span class="st">"validation"</span>, <span class="st">"test"</span>]]</span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a>training_loader, validation_loader, test_loader <span class="op">=</span> [DataLoader(dataset<span class="op">=</span>df, batch_size<span class="op">=</span><span class="dv">10</span>, shuffle<span class="op">=</span><span class="va">True</span>) <span class="cf">for</span> df <span class="kw">in</span> training_dfs]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="define-your-model-architecture-and-loss-and-optimizer" class="level3">
<h3 class="anchored" data-anchor-id="define-your-model-architecture-and-loss-and-optimizer">Define your model architecture (and loss and optimizer)</h3>
<p>Heres we’ll use a MLP (multi-layer perceptron) with a few hidden layers. We’ll use the workhorses of ML for loss and optimization Means Squared Error (MSE) and Stochastic Gradient Descent (SGD).</p>
</section>
<section id="train-your-model" class="level3">
<h3 class="anchored" data-anchor-id="train-your-model">Train your model</h3>
<p>The training loop for PyTorch is longwinded. We could update to PyTorch Lightning, but for now, we’ll be explicit. It’s good to see all the operations that occur, and a nice reminder that neural nets are models with parameters that can be optimized like any other model.</p>
<div class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> torch.nn <span class="im">as</span> nn</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> Model(nn.Module):</span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, input_size, hidden_size, hidden_count, output_size):</span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a> <span class="bu">super</span>().<span class="fu">__init__</span>()</span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.hidden_count <span class="op">=</span> hidden_count</span>
<span id="cb13-7"><a href="#cb13-7" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.layer1 <span class="op">=</span> nn.Linear(input_size, hidden_size)</span>
<span id="cb13-8"><a href="#cb13-8" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.layer2 <span class="op">=</span> nn.Linear(hidden_size, hidden_size)</span>
<span id="cb13-9"><a href="#cb13-9" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.layer3 <span class="op">=</span> nn.Linear(hidden_size, output_size)</span>
<span id="cb13-10"><a href="#cb13-10" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> forward(<span class="va">self</span>, x):</span>
<span id="cb13-11"><a href="#cb13-11" aria-hidden="true" tabindex="-1"></a> x <span class="op">=</span> <span class="va">self</span>.layer1(x)</span>
<span id="cb13-12"><a href="#cb13-12" aria-hidden="true" tabindex="-1"></a> x <span class="op">=</span> nn.ReLU()(x)</span>
<span id="cb13-13"><a href="#cb13-13" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="va">self</span>.hidden_count):</span>
<span id="cb13-14"><a href="#cb13-14" aria-hidden="true" tabindex="-1"></a> x <span class="op">=</span> <span class="va">self</span>.layer2(x)</span>
<span id="cb13-15"><a href="#cb13-15" aria-hidden="true" tabindex="-1"></a> x <span class="op">=</span> nn.ReLU()(x)</span>
<span id="cb13-16"><a href="#cb13-16" aria-hidden="true" tabindex="-1"></a> x <span class="op">=</span> <span class="va">self</span>.layer3(x)</span>
<span id="cb13-17"><a href="#cb13-17" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> x.to(torch.float64)</span>
<span id="cb13-18"><a href="#cb13-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-19"><a href="#cb13-19" aria-hidden="true" tabindex="-1"></a>loss_fn <span class="op">=</span> nn.MSELoss()</span>
<span id="cb13-20"><a href="#cb13-20" aria-hidden="true" tabindex="-1"></a>output_size <span class="op">=</span> <span class="dv">1</span></span>
<span id="cb13-21"><a href="#cb13-21" aria-hidden="true" tabindex="-1"></a>model <span class="op">=</span> Model(popgen_training.numsites, <span class="dv">1000</span>, <span class="dv">4</span>, output_size).to(device)</span>
<span id="cb13-22"><a href="#cb13-22" aria-hidden="true" tabindex="-1"></a>optimizer <span class="op">=</span> torch.optim.SGD(model.parameters(), lr<span class="op">=</span><span class="fl">0.001</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell" data-execution_count="10">
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> epoch <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">20</span>):</span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> x_batch, y_batch <span class="kw">in</span> training_loader:</span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># 1. Generate predictions</span></span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a> pred <span class="op">=</span> model(x_batch)</span>
<span id="cb14-5"><a href="#cb14-5" aria-hidden="true" tabindex="-1"></a> <span class="co">#print(pred)</span></span>
<span id="cb14-6"><a href="#cb14-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># 2. Calculate loss</span></span>
<span id="cb14-7"><a href="#cb14-7" aria-hidden="true" tabindex="-1"></a> loss <span class="op">=</span> loss_fn(pred, y_batch)</span>
<span id="cb14-8"><a href="#cb14-8" aria-hidden="true" tabindex="-1"></a> <span class="co"># 3. Compute gradients</span></span>
<span id="cb14-9"><a href="#cb14-9" aria-hidden="true" tabindex="-1"></a> loss.backward()</span>
<span id="cb14-10"><a href="#cb14-10" aria-hidden="true" tabindex="-1"></a> <span class="co"># 4. Update parameters using gradients</span></span>
<span id="cb14-11"><a href="#cb14-11" aria-hidden="true" tabindex="-1"></a> optimizer.step()</span>
<span id="cb14-12"><a href="#cb14-12" aria-hidden="true" tabindex="-1"></a> <span class="co"># 5. Reset the gradients to zero</span></span>
<span id="cb14-13"><a href="#cb14-13" aria-hidden="true" tabindex="-1"></a> optimizer.zero_grad()</span>
<span id="cb14-14"><a href="#cb14-14" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> epoch <span class="op">%</span> <span class="dv">2</span> <span class="op">==</span> <span class="dv">0</span>:</span>
<span id="cb14-15"><a href="#cb14-15" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f'Epoch </span><span class="sc">{</span>epoch<span class="sc">}</span><span class="ss"> Loss </span><span class="sc">{</span>loss<span class="sc">.</span>item()<span class="sc">:.4f}</span><span class="ss">'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Epoch 0 Loss 0.1793</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Epoch 2 Loss 0.2423</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Epoch 4 Loss 0.0440</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Epoch 6 Loss 0.2862</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Epoch 8 Loss 0.0824</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Epoch 10 Loss 0.0526</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Epoch 12 Loss 0.0723</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Epoch 14 Loss 0.0891</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Epoch 16 Loss 0.1007</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Epoch 18 Loss 0.0811</code></pre>
</div>
</div>
</section>
<section id="evaluate-your-model" class="level3">
<h3 class="anchored" data-anchor-id="evaluate-your-model">Evaluate your model</h3>
<p>Note that <code>training_dfs[2]</code> is the test data. So the model has not seen these examples before.</p>
<div class="cell" data-execution_count="11">
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>fig, ax <span class="op">=</span> plt.subplots()</span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> x_batch, y_batch <span class="kw">in</span> training_dfs[<span class="dv">2</span>]:</span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a> pred <span class="op">=</span> model(x_batch)</span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a> ax.scatter(popgen_training.Ne_scaling<span class="op">*</span>y_batch.item(), popgen_training.Ne_scaling<span class="op">*</span>pred.item(), color <span class="op">=</span> <span class="st">'black'</span>)</span>
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a>one_one <span class="op">=</span> np.linspace(popgen_training.Ne_low, popgen_training.Ne_high, <span class="dv">100</span>)</span>
<span id="cb25-6"><a href="#cb25-6" aria-hidden="true" tabindex="-1"></a>ax.plot(one_one, one_one)</span>
<span id="cb25-7"><a href="#cb25-7" aria-hidden="true" tabindex="-1"></a>ax.set_xlabel(<span class="st">"Simulated $N_e$"</span>)</span>
<span id="cb25-8"><a href="#cb25-8" aria-hidden="true" tabindex="-1"></a>ax.set_ylabel(<span class="st">"Predicted $N_e$"</span>)</span>
<span id="cb25-9"><a href="#cb25-9" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display">
<p><img src="popgenDL_crashcourse_files/figure-html/cell-12-output-1.png" width="590" height="445"></p>
</div>
</div>
</section>
</section>
<section id="postscript" class="level2">
<h2 class="anchored" data-anchor-id="postscript">Postscript</h2>
<p>So that went pretty well. This is pretty bare bones, but hopefully it gives you a sense of how you might approach a popgen problem with deep learning.</p>
<p>Here’s somethign you might try to edit in the above to gain more familiarity with the process.</p>
<ul>
<li><p>Try other architectures, loss functions, and optimizers.</p></li>
<li><p>Modify hyperparameters like the numer of hidden layers. How many hidden layers does the model currently have? What is numsites and what happens if you change it?</p></li>
<li><p>How else could we monitor training progress? Did we use the validation data?</p>
<p>Answer: No we did not, but how can we incorporate it?</p></li>
</ul>
</section>
</section>
</main>
<!-- /main column -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const clipboard = new window.ClipboardJS('.code-copy-button', {
target: function(trigger) {
return trigger.previousElementSibling;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
let tooltip;
if (window.bootstrap) {
button.setAttribute("data-bs-toggle", "tooltip");
button.setAttribute("data-bs-placement", "left");
button.setAttribute("data-bs-title", "Copied!");
tooltip = new bootstrap.Tooltip(button,
{ trigger: "manual",
customClass: "code-copy-button-tooltip",
offset: [0, -8]});
tooltip.show();
}
setTimeout(function() {
if (tooltip) {
tooltip.hide();
button.removeAttribute("data-bs-title");
button.removeAttribute("data-bs-toggle");
button.removeAttribute("data-bs-placement");
}
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
// use id or data attribute instead here
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
const findCites = (el) => {
const parentEl = el.parentElement;
if (parentEl) {
const cites = parentEl.dataset.cites;
if (cites) {
return {
el,
cites: cites.split(' ')
};
} else {
return findCites(el.parentElement)
}
} else {
return undefined;
}
};
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const citeInfo = findCites(ref);
if (citeInfo) {
tippyHover(citeInfo.el, function() {
var popup = window.document.createElement('div');
citeInfo.cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
}
});
</script>
</div> <!-- /content -->
</body></html>