-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlih.html
677 lines (567 loc) · 29.1 KB
/
dlih.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
672
673
674
675
676
677
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Ist das ein Graph oder kann das weg? Funktionales Deep Learning in Haskell</title>
<meta name="author" content="Raoul Schlotterbeck"/>
<style type="text/css">
.underline { text-decoration: underline; }
</style>
<link rel="stylesheet" href="/nix/store/9xl8b9acpgqmjwr9r39vcb7bgr0cw3i9-source/dist/reveal.css"/>
<link rel="stylesheet" href="./css/themes/active.css" id="theme"/>
<script type="text/javascript" src="/nix/store/ic80bl2rn5c3xjwp92grxszw7xsdv01r-source/es5/tex-chtml.js"></script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section id="sec-title-slide">
<h1 class="title">Ist das ein Graph oder kann das weg? Funktionales Deep Learning in Haskell</h1><h2 class="author">Raoul Schlotterbeck</h2><p class="date">Created: 2023-03-14 Tue 10:10</p>
</section>
<section>
<section id="slide-orgd16ab09">
<h2 id="orgd16ab09"></h2>
<div style="font-size: 80%"><p>
<p>
"<i>As so often, I find that talks that I'm giving are basically explaining what Conal
Elliot and Ed Kmett have done several years ago.</i>"
</p>
</p><figcaption style="font-size: 80%"> - Simon Peyton Jones </figcaption></div><img src="./pics/compiling_to_categories.png" style="height: 40%">
</section>
</section>
<section>
<section id="slide-org09d3ccf">
<h2 id="org09d3ccf"></h2>
<p>
ANOMALY-APP
</p>
</section>
</section>
<section>
<section id="slide-org0e16c9b">
<h2 id="org0e16c9b">Neuronale Netze sind doch nur Funktionen</h2>
<div style="width: 100%; overflow: hidden;"> <div style="width: 550px; float: left; font-size: 90%">
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-definition">neuralNet</span> wL bL <span class="org-haskell-operator">...</span> w1 b1 <span class="org-haskell-operator">=</span>
layer wL bL
<span class="org-haskell-operator">.</span> <span class="org-haskell-operator">...</span>
<span class="org-haskell-operator">.</span> layer w1 b1
<span class="org-haskell-definition">layer</span> weightMatrix biasVector <span class="org-haskell-operator">=</span>
fmap activationFunction
<span class="org-haskell-operator">.</span> vectorAddition biasVector
<span class="org-haskell-operator">.</span> matrixVectorProduct weightMatrix
</pre>
</div>
</div><div style="margin-left: 550px;">
<div id="org0878e2a" class="figure">
<p><img src="./pics/neural_net.png" alt="neural_net.png" />
</p>
</div>
</div><div style="text-align: center;">
<p>
\(\Rightarrow\) Komposition purer Funktionen
</p>
</div></div>
<aside class="notes">
<ul>
<li>Gibt auch andere layers</li>
</ul>
</aside>
</section>
</section>
<section>
<section id="slide-org15d7e1b">
<h2 id="org15d7e1b">Deep Learning Community: Hold my beer</h2>
<p>
<p>
"… layers (die im modernen maschinellen Lernen als <b>zustandsbehaftete</b> Funktionen
mit <b>impliziten Parametern</b> verstanden werden sollten) werden typischerweise als
<b>Python-Klassen</b> dargestellt, deren Konstruktoren ihre Parameter <b>erzeugen und
initialisieren</b>…"
</p>
</p>
<cite style="font-size: 50%">
<p>
Übersetzt aus:
<a href="https://proceedings.neurips.cc/paper/2019/file/bdbca288fee7f92f2bfa9f7012727740-Paper.pdf">https://proceedings.neurips.cc/paper/2019/file/bdbca288fee7f92f2bfa9f7012727740-Paper.pdf</a>
</p>
</cite>
</section>
</section>
<section>
<section id="slide-orgccef526">
<h2 id="orgccef526">Training</h2>
<ul>
<li>Finde Parameter, für die das NN auf einem gegebenen Trainingsdatensatz
möglichst gute Ergebnisse liefert</li>
<li><p>
Was gut ist, wird von einer skalarwertigen Fehlerfunktion beurteilt
</p>
<p>
\(\Rightarrow\) Löse Optimierungsproblem:
</p>
<div style="text-align: center">
<p>
\(\textrm{argmin}_{\omega \in \Omega} (\textrm{loss} \circ \textrm{neuralNet} (\omega; \textrm{data}))\)
</p>
</div></li>
</ul>
</section>
</section>
<section>
<section id="slide-org1a4c384">
<h2 id="org1a4c384">Gradient Descent</h2>
<div style="text-align: center; font-size:90%">
<p>
\(w' = w - \alpha * \frac {\partial f}{\partial w}\)
</p>
</div><hr><div style="width: 100%; height: 350px; overflow: hidden;"> <div style="width: 450px; float: left"><img src="./pics/gradient2.png" style="width: 450px; height: 300px"></div>
<div style="margin-left: 450px;"><img src="./pics/loss_surface3.gif" style="width: 500px; height: 350px"></div></div><hr>
<cite style="font-size: 50%"> <a> http://citadel.sjfc.edu/faculty/kgreen/vector/Block2/pder/node8.html </a>, <a>https://upload.wikimedia.org/wikipedia/commons/a/a3/Gradient_descent.gif </a></cite>
<aside class="notes">
<cite style="font-size: 50%">
<p>
<a href="http://citadel.sjfc.edu/faculty/kgreen/vector/Block2/pder/node8.html">http://citadel.sjfc.edu/faculty/kgreen/vector/Block2/pder/node8.html</a>,
<img src="https://upload.wikimedia.org/wikipedia/commons/a/a3/Gradient_descent.gif" alt="Gradient_descent.gif" />
</p>
<p>
<a href="https://www.science.org/content/article/ai-researchers-allege-machine-learning-alchemy">https://www.science.org/content/article/ai-researchers-allege-machine-learning-alchemy</a>
</p>
</cite>
</aside>
</section>
</section>
<section>
<section id="slide-org0c14107">
<h2 id="org0c14107">Automatic Differentiation</h2>
<div style="text-align: center">
<p>
\(nn = l_L \circ ... \circ l_1\)
</p>
<p>
\(\Rightarrow\) Ableitung ist: \(Dnn = Dl_L \circ ... \circ Dl_1\)
</p>
</div>
<ul>
<li>Funktionskomposition ist assoziativ, d.h. wir können in bliebiger Reihenfolge
auswerten</li>
<li>Aufwand "vorwärts" abhängig von Eingangsdimension (hier: Anzahl der
Parameter, heute bis \(10^{12}\))</li>
<li>Aufwand "rückwärts" abhängig von Ausgangsdimension (hier: 1)</li>
</ul>
</section>
</section>
<section>
<section id="slide-orga41c461">
<h2 id="orga41c461">Reverse Automatic Differentiation</h2>
<p>
\(Dnn(v) = Dl_L(l_{L-1}(...)) \circ ... \circ Dl_1(v)\)
</p>
<p>
Um \(Dl_i\) zu berechnen, müssen wir die Ausgabe von \(l_{i - 1}\) kennen
</p>
<p>
\(\Rightarrow\) "Wengert-Liste"
</p>
<p>
Deep Learning Bibliotheken sind im Wesentlichen Werkzeuge zur Generierung und
Verwaltung von Berechnungsgraphen.
</p>
</section>
</section>
<section>
<section id="slide-org8174035">
<h2 id="org8174035">TensorFlow Graphen</h2>
<div style="overflow-x: hidden; overflow-y: auto; height: 500px; font-size: 50%">
<p>
<img src="./pics/tf_graph.png" alt="tf_graph.png" />
<img src="./pics/tensorboard-01.png" alt="tensorboard-01.png" />
</p>
</div>
</section>
</section>
<section>
<section id="slide-orgb191e6d">
<h2 id="orgb191e6d">Implementierung in TensorFlow</h2>
<div style="font-size: 70%;">
<div class="org-src-container">
<pre class="src src-python">
<span class="org-keyword">class</span> <span class="org-type">SimpleNN</span>:
<span class="org-keyword">def</span> <span class="org-function-name">__init__</span>(<span class="org-keyword">self</span>, dim):
<span class="org-keyword">self</span>.dims = [dim, dim, dim // 2, dim // 2, dim]
<span class="org-keyword">self</span>.<span class="org-variable-name">weights</span> = []
<span class="org-keyword">self</span>.<span class="org-variable-name">biases</span> = []
<span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(4):
<span class="org-keyword">self</span>.weights.append(
tf.Variable(tf.random.normal(shape=(<span class="org-keyword">self</span>.dims[i+1],<span class="org-keyword">self</span>.dims[i])))
)
<span class="org-keyword">self</span>.biases.append(
tf.Variable(tf.random.normal(shape=(<span class="org-keyword">self</span>.dims[i+1],1)))
)
<span class="org-keyword">def</span> <span class="org-function-name">__call__</span>(<span class="org-keyword">self</span>, x):
inputs = tf.convert_to_tensor([x], dtype=tf.float32)
out = tf.matmul(<span class="org-keyword">self</span>.weights[0],
inputs, transpose_b=<span class="org-constant">True</span>) + <span class="org-keyword">self</span>.biases[0]
out = tf.tanh(out)
out = tf.matmul(<span class="org-keyword">self</span>.weights[1], out) + <span class="org-keyword">self</span>.biases[1]
out = tf.nn.relu(out)
out = tf.matmul(<span class="org-keyword">self</span>.weights[2], out) + <span class="org-keyword">self</span>.biases[2]
out = tf.tanh(out)
<span class="org-keyword">return</span> tf.matmul(<span class="org-keyword">self</span>.weights[3], out) + <span class="org-keyword">self</span>.biases[3]
</pre>
</div>
</div>
<aside class="notes">
<ul>
<li>Graphencode</li>
<li>Typen spezialisiert</li>
<li>lässt sich nicht Generalisieren…</li>
<li>… testen</li>
<li>Pythonintegration</li>
<li>dimensionsfehler</li>
</ul>
</aside>
</section>
</section>
<section>
<section id="slide-org995cfdc">
<h2 id="org995cfdc">Neuronale Netze mit ConCat</h2>
<div style="overflow-x: hidden; overflow-y: auto; height: 450px; font-size: 90%">
<div class="org-src-container">
<pre class="src src-haskell">
<span class="org-haskell-definition">simpleNN</span> <span class="org-haskell-operator">::</span>
( <span class="org-haskell-type">KnownNat</span> n,
<span class="org-haskell-type">KnownNat</span> m,
<span class="org-haskell-type">Functor</span> f,
<span class="org-haskell-type">Foldable</span> f,
<span class="org-haskell-type">Floating</span> num,
<span class="org-haskell-operator">...</span>
) <span class="org-haskell-operator">=></span>
<span class="org-haskell-type">SimpleNNParameters</span> f n m num <span class="org-haskell-operator">-></span>
f n num <span class="org-haskell-operator">-></span>
f n num
<span class="org-haskell-definition">simpleNN</span> <span class="org-haskell-operator">=</span>
affine
<span class="org-haskell-operator">@.</span> affTanh
<span class="org-haskell-operator">@.</span> affRelu
<span class="org-haskell-operator">@.</span> affTanh
(<span class="org-haskell-definition">@.</span>) <span class="org-haskell-operator">::</span>
(q s <span class="org-haskell-operator">-></span> b <span class="org-haskell-operator">-></span> c) <span class="org-haskell-operator">-></span>
(p s <span class="org-haskell-operator">-></span> a <span class="org-haskell-operator">-></span> b) <span class="org-haskell-operator">-></span>
((q <span class="org-haskell-type">:*:</span> p) s <span class="org-haskell-operator">-></span> a <span class="org-haskell-operator">-></span> c)
(g <span class="org-haskell-operator">@.</span> f) (q <span class="org-haskell-constructor">:*:</span> p) <span class="org-haskell-operator">=</span> g q <span class="org-haskell-operator">.</span> f p
<span class="org-haskell-keyword">type</span> p <span class="org-haskell-operator">--*</span> q <span class="org-haskell-operator">=</span> q <span class="org-haskell-type">:.:</span> p
<span class="org-haskell-keyword">type</span> <span class="org-haskell-type">Bump</span> h <span class="org-haskell-operator">=</span> h <span class="org-haskell-type">:*:</span> <span class="org-haskell-type">Par1</span>
<span class="org-haskell-definition">bump</span> <span class="org-haskell-operator">::</span> <span class="org-haskell-type">Num</span> s <span class="org-haskell-operator">=></span> a s <span class="org-haskell-operator">-></span> <span class="org-haskell-type">Bump</span> a s
<span class="org-haskell-definition">bump</span> a <span class="org-haskell-operator">=</span> a <span class="org-haskell-constructor">:*:</span> <span class="org-haskell-constructor">Par1</span> 1
<span class="org-haskell-keyword">type</span> a <span class="org-haskell-operator">--+</span> b <span class="org-haskell-operator">=</span> <span class="org-haskell-type">Bump</span> a <span class="org-haskell-operator">--*</span> b
<span class="org-haskell-keyword">type</span> <span class="org-haskell-type">SimpleNNParameters</span> (f <span class="org-haskell-operator">::</span> <span class="org-haskell-type">Nat</span> <span class="org-haskell-operator">-></span> <span class="org-haskell-operator">*</span> <span class="org-haskell-operator">-></span> <span class="org-haskell-operator">*</span>) n m <span class="org-haskell-operator">=</span>
( (f m <span class="org-haskell-operator">--+</span> f n)
<span class="org-haskell-type">:*:</span> (f m <span class="org-haskell-operator">--+</span> f m)
<span class="org-haskell-type">:*:</span> (f n <span class="org-haskell-operator">--+</span> f m)
<span class="org-haskell-type">:*:</span> (f n <span class="org-haskell-operator">--+</span> f n)
)
(<span class="org-haskell-definition"><.></span>) <span class="org-haskell-operator">::</span> (<span class="org-haskell-type">Foldable</span> a, <span class="org-haskell-type">Zip</span> a, <span class="org-haskell-type">Additive</span> s, <span class="org-haskell-type">Num</span> s)
<span class="org-haskell-operator">=></span> a s <span class="org-haskell-operator">-></span> a s <span class="org-haskell-operator">-></span> s
xs <span class="org-haskell-definition"><.></span> ys <span class="org-haskell-operator">=</span> sumA (zipWith (<span class="org-haskell-operator">*</span>) xs ys)
<span class="org-haskell-definition">linear</span> <span class="org-haskell-operator">::</span> (<span class="org-haskell-type">Foldable</span> a, <span class="org-haskell-type">Zip</span> a, <span class="org-haskell-type">Functor</span> b, <span class="org-haskell-type">Additive</span> s, <span class="org-haskell-type">Num</span> s)
<span class="org-haskell-operator">=></span> (a <span class="org-haskell-operator">--*</span> b) s <span class="org-haskell-operator">-></span> (a s <span class="org-haskell-operator">-></span> b s)
<span class="org-haskell-definition">linear</span> (<span class="org-haskell-constructor">Comp1</span> ba) a <span class="org-haskell-operator">=</span> (<span class="org-haskell-operator"><.></span> a) <span class="org-haskell-operator"><$></span> ba
<span class="org-haskell-definition">affine</span> <span class="org-haskell-operator">::</span> (<span class="org-haskell-type">Foldable</span> a, <span class="org-haskell-type">Zip</span> a, <span class="org-haskell-type">Functor</span> b, <span class="org-haskell-type">Additive</span> s, <span class="org-haskell-type">Num</span> s)
<span class="org-haskell-operator">=></span> (a <span class="org-haskell-operator">--+</span> b) s <span class="org-haskell-operator">-></span> (a s <span class="org-haskell-operator">-></span> b s)
<span class="org-haskell-definition">affine</span> m <span class="org-haskell-operator">=</span> linear m <span class="org-haskell-operator">.</span> bump
<span class="org-haskell-definition">affRelu</span> <span class="org-haskell-operator">::</span>
( <span class="org-haskell-type">Foldable</span> a,
<span class="org-haskell-type">Zip</span> a,
<span class="org-haskell-type">Functor</span> b,
<span class="org-haskell-type">Ord</span> s,
<span class="org-haskell-type">Additive</span> s,
<span class="org-haskell-type">Num</span> s
) <span class="org-haskell-operator">=></span>
(a <span class="org-haskell-operator">--+</span> b) s <span class="org-haskell-operator">-></span> (a s <span class="org-haskell-operator">-></span> b s)
<span class="org-haskell-definition">affRelu</span> l <span class="org-haskell-operator">=</span> relus <span class="org-haskell-operator">.</span> affine l
</pre>
</div>
</div>
<aside class="notes">
<ul>
<li>pure Funktion</li>
<li>exakte Rep eines NNs</li>
<li>generische Typen</li>
<li>Dimensionalität</li>
<li>testen</li>
</ul>
</aside>
</section>
</section>
<section>
<section id="slide-orgde354a4">
<h2 id="orgde354a4">ConCat Funktionsweise</h2>
<img src="./pics/concat-pipeline.png" style="width: 90%">
<ul>
<li>Nutzt Isomorphie zwischen Lambda-Kalkülen und kartesisch abgeschlossenen Kategorien (CCC)</li>
<li>Übersetzt Haskell-Core in kategorielle Sprache</li>
<li>Ausdrücke in kategorieller Sprache können in beliebigen CCCs interpretiert werden</li>
<li>Abstrahiert dadurch Haskells Funktionspfeil <code class="src src-haskell"><span class="org-haskell-constructor">(->)</span></code></li>
</ul>
</section>
</section>
<section>
<section id="slide-org9baee87">
<h2 id="org9baee87">Beispiel einer Transformation</h2>
<div class="org-src-container">
<pre class="src src-haskell">
<span class="org-haskell-definition">magSqr</span> <span class="org-haskell-operator">::</span> <span class="org-haskell-type">Num</span> a <span class="org-haskell-operator">=></span> (a, a) <span class="org-haskell-operator">-></span> a
<span class="org-haskell-definition">magSqr</span> (a, b) <span class="org-haskell-operator">=</span> sqr a <span class="org-haskell-operator">+</span> sqr b
</pre>
</div>
<p>
\(\Rightarrow\) ConCat:
</p>
<p>
\(magSqr =\)
\(addC\)
\(\circ (mulC \circ (exl \triangle exl) \triangle mulC \circ (exr \triangle exr))\)
</p>
<p>
In Kategorie der Graphen - <code class="src src-haskell">(a, a) <span class="org-haskell-operator">`Graph`</span> a</code>:
</p>
<div style="text-align: center"><img src="./pics/magSqr.png" height="250px%"><cite style="font-size:50%">
<p>
<a href="https://arxiv.org/abs/1804.00746">https://arxiv.org/abs/1804.00746</a>
</p>
</cite></div>
</section>
</section>
<section>
<section id="slide-orgf72d2f5">
<h2 id="orgf72d2f5">Generalized Derivatives</h2>
<p>
Idee: ergänze Funktionen um ihre Ableitung
</p>
<div style="text-align: center;">
<p>
\(a \mapsto f(a) \Rightarrow a \mapsto (f(a), f'(a))\)
</p>
</div>
<p>
Kategorie der Generalisierten Ableitungen:
</p>
<div class="org-src-container">
<pre class="src src-haskell">
<span class="org-haskell-keyword">newtype</span> <span class="org-haskell-type">GD</span> k a b <span class="org-haskell-operator">=</span> <span class="org-haskell-constructor">D</span> {unD <span class="org-haskell-operator">::</span> a <span class="org-haskell-operator">-></span> b <span class="org-haskell-type">:*</span> (a <span class="org-haskell-operator">`k`</span> b)}
</pre>
</div>
</section>
</section>
<section>
<section id="slide-org6783727">
<h2 id="org6783727">Komposition für Generalized Derivatives</h2>
<div style="text-align: center;">
<p>
\(a \mapsto (f(a), f'(a))\)
</p>
</div>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-keyword">instance</span> <span class="org-haskell-type">Category</span> k <span class="org-haskell-operator">=></span> <span class="org-haskell-type">Category</span> (<span class="org-haskell-type">GD</span> k) <span class="org-haskell-keyword">where</span>
<span class="org-haskell-operator">...</span>
<span class="org-haskell-constructor">D</span> g <span class="org-haskell-operator">.</span> <span class="org-haskell-constructor">D</span> f <span class="org-haskell-operator">=</span>
<span class="org-haskell-constructor">D</span> (<span class="org-haskell-operator">\</span> a <span class="org-haskell-operator">-></span>
<span class="org-haskell-keyword">let</span> (b, f') <span class="org-haskell-operator">=</span> f a
(c, g') <span class="org-haskell-operator">=</span> g b
<span class="org-haskell-keyword">in</span> (c, g' <span class="org-haskell-operator">.</span> f')
)
</pre>
</div>
<div style="text-align: center">
<p>
Kettenregel: \((g \circ f)'(x) = g'(f(x)) \circ f'(x)\)
</p>
</div>
</section>
</section>
<section>
<section id="slide-org28f136a">
<h2 id="org28f136a">Multiplikation für Generalized Derivatives</h2>
<div style="text-align: center;">
<p>
\(a \mapsto (f(a), f'(a))\)
</p>
</div>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-keyword">instance</span> (<span class="org-haskell-type">LinearCat</span> k s, <span class="org-haskell-type">Additive</span> s, <span class="org-haskell-type">Num</span> s) <span class="org-haskell-operator">=></span> <span class="org-haskell-type">NumCat</span> (<span class="org-haskell-type">GD</span> k) s <span class="org-haskell-keyword">where</span>
<span class="org-haskell-operator">...</span>
mulC <span class="org-haskell-operator">=</span> <span class="org-haskell-constructor">D</span> (mulC <span class="org-haskell-operator">&&&</span> <span class="org-haskell-operator">\</span> (u,v) <span class="org-haskell-operator">-></span> scale v <span class="org-haskell-operator">||||</span> scale u)
</pre>
</div>
<div style="text-align: center">
<p>
Produktregel: \((f(x) \cdot g(x))' = f'(x) \cdot g(x) + f(x) \cdot g'(x)\)
</p>
</div>
</section>
</section>
<section>
<section id="slide-orgf1e3f2c">
<h2 id="orgf1e3f2c">Forward Automatic Differentiation</h2>
<div style="text-align: center">
<div id="orgf35f28c" class="figure">
<p><img src="./pics/magSqr_D.png" alt="magSqr_D.png" />
</p>
<p><span class="figure-number">Figure 1: </span><code>magSqr in GD (-+>)</code></p>
</div>
<cite style="font-size: 50%">
<p>
<a href="https://arxiv.org/abs/1804.00746">https://arxiv.org/abs/1804.00746</a>
</p>
</cite></div>
</section>
</section>
<section>
<section id="slide-orga1ec5fd">
<h2 id="orga1ec5fd">Duale Kategorien</h2>
<p>
Im Dual einer Kategorie drehen sich alle Pfeile um
</p>
<div style="text-align: center">
<p>
\(a \rightarrow b \Rightarrow b \rightarrow a\)
</p>
</div>
<p>
In Haskell:
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-keyword">newtype</span> <span class="org-haskell-type">Dual</span> k a b <span class="org-haskell-operator">=</span> <span class="org-haskell-constructor">Dual</span> (b <span class="org-haskell-operator">`k`</span> a)
</pre>
</div>
</section>
</section>
<section>
<section id="slide-org7cad25e">
<h2 id="org7cad25e">Beispiele Dualer Morphismen</h2>
<div class="org-src-container">
<pre class="src src-haskell">
<span class="org-haskell-keyword">instance</span> <span class="org-haskell-type">Category</span> k <span class="org-haskell-operator">=></span> <span class="org-haskell-type">Category</span> (<span class="org-haskell-type">Dual</span> k) <span class="org-haskell-keyword">where</span>
<span class="org-haskell-operator">...</span>
<span class="org-comment-delimiter">-- </span><span class="org-comment">flip :: (a -> b -> c) -> b -> a -> c</span>
(<span class="org-haskell-operator">.</span>) <span class="org-haskell-operator">=</span> inAbst2 (flip (<span class="org-haskell-operator">.</span>))
<span class="org-haskell-keyword">instance</span> <span class="org-haskell-type">CoproductPCat</span> k <span class="org-haskell-operator">=></span> <span class="org-haskell-type">ProductCat</span> (<span class="org-haskell-type">Dual</span> k) <span class="org-haskell-keyword">where</span>
<span class="org-haskell-operator">...</span>
<span class="org-comment-delimiter">-- </span><span class="org-comment">exl :: (a, b) -> a; inlP :: a -> (a, b)</span>
exl <span class="org-haskell-operator">=</span> abst inlP
</pre>
</div>
</section>
</section>
<section>
<section id="slide-org2655c1b">
<h2 id="org2655c1b">Reverse Automatic Differentiation</h2>
<div style="text-align: center">
<div id="org3fbf3ca" class="figure">
<p><img src="./pics/magSqr_dual.png" alt="magSqr_dual.png" width="500" />
</p>
<p><span class="figure-number">Figure 2: </span><code>magSqr in GD (Dual(-+>))</code></p>
</div>
<cite style="font-size: 50%">
<p>
<a href="https://arxiv.org/abs/1804.00746">https://arxiv.org/abs/1804.00746</a>
</p>
</cite></div>
</section>
</section>
<section>
<section id="slide-orgeae7489">
<h2 id="orgeae7489">Graphenfreie Gradienten</h2>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-keyword">type</span> <span class="org-haskell-type">RAD</span> <span class="org-haskell-operator">=</span> <span class="org-haskell-type">GD</span> (<span class="org-haskell-type">Dual</span> (<span class="org-haskell-operator">-+></span>))
<span class="org-haskell-definition">grad</span> <span class="org-haskell-operator">::</span> <span class="org-haskell-type">Num</span> s <span class="org-haskell-operator">=></span> (a <span class="org-haskell-operator">-></span> s) <span class="org-haskell-operator">-></span> (a <span class="org-haskell-operator">-></span> a)
<span class="org-haskell-definition">grad</span> <span class="org-haskell-operator">=</span> friemelOutGrad <span class="org-haskell-operator">.</span> toCcc <span class="org-haskell-operator">@</span><span class="org-haskell-constructor">RAD</span>
<span class="org-haskell-definition">nnGrad</span> <span class="org-haskell-operator">::</span> parameters <span class="org-haskell-operator">-></span> parameters
<span class="org-haskell-definition">nnGrad</span> <span class="org-haskell-operator">=</span> grad (loss <span class="org-haskell-operator">.</span> nn)
</pre>
</div>
</section>
</section>
<section>
<section id="slide-orgc423820">
<h2 id="orgc423820">Beschleunigtes Deep Learning in Haskell mit Accelerate</h2>
<p>
"Data.Array.Accelerate defines an embedded array language for computations for
high-performance computing in Haskell. … These computations may then be online
compiled and executed on a range of architectures."
</p>
<p>
Kategorie der Accelerate-Funktionen:
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-keyword">newtype</span> <span class="org-haskell-type">AccFun</span> a b <span class="org-haskell-keyword">where</span>
<span class="org-haskell-constructor">AccFun</span> <span class="org-haskell-operator">::</span> (<span class="org-haskell-type">AccValue</span> a <span class="org-haskell-operator">-></span> <span class="org-haskell-type">AccValue</span> b) <span class="org-haskell-operator">-></span> <span class="org-haskell-type">AccFun</span> a b
</pre>
</div>
<aside class="notes">
<p>
(- native Haskell accelerate:-> AST)
</p>
<ul>
<li>schreiben Berechnung als Haskell-Code, daraus entsteht accelerate AST</li>
<li>accelerate AST wird zur Laufzeit von LLVM kompiliert</li>
<li>kann dann gegen CPU/GPU/… ausgeführt werden</li>
<li>Plugin erzeugt den acclerate AST</li>
</ul>
</aside>
</section>
</section>
<section>
<section id="slide-org5f82333">
<h2 id="org5f82333">ConCelerate: ConCat + Accelerate</h2>
<div>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-definition">simpleNN</span> <span class="org-haskell-operator">::</span> (<span class="org-haskell-type">SimpleNNConstraints</span> f n m num) <span class="org-haskell-operator">=></span> <span class="org-haskell-type">SimpleNN</span> f n m num
<span class="org-haskell-definition">simpleNN</span> <span class="org-haskell-operator">=</span> affine <span class="org-haskell-operator">@.</span> affTanh <span class="org-haskell-operator">@.</span> affRelu <span class="org-haskell-operator">@.</span> affTanh
<span class="org-haskell-definition">simpleNNGrad</span> <span class="org-haskell-operator">::</span>
(<span class="org-haskell-type">KnownNat</span> m, <span class="org-haskell-type">KnownNat</span> n) <span class="org-haskell-operator">=></span>
(<span class="org-haskell-type">Vector</span> n <span class="org-haskell-type">Double</span>, <span class="org-haskell-type">Vector</span> n <span class="org-haskell-type">Double</span>) <span class="org-haskell-operator">-></span>
<span class="org-haskell-type">SimpleNNParameters</span> n m <span class="org-haskell-type">Double</span> <span class="org-haskell-operator">-></span>
<span class="org-haskell-type">SimpleNNParameters</span> n m <span class="org-haskell-type">Double</span>
<span class="org-haskell-definition">simpleNNGrad</span> <span class="org-haskell-operator">=</span> errGrad simpleNN
<span class="org-haskell-definition">simpleNNGradAccFun</span> <span class="org-haskell-operator">::</span>
(<span class="org-haskell-type">KnownNat</span> m, <span class="org-haskell-type">KnownNat</span> n) <span class="org-haskell-operator">=></span>
(<span class="org-haskell-type">Vector</span> n <span class="org-haskell-type">Double</span>, <span class="org-haskell-type">Vector</span> n <span class="org-haskell-type">Double</span>) <span class="org-haskell-operator">-></span>
<span class="org-haskell-type">SimpleNNParameters</span> n m <span class="org-haskell-type">Double</span> <span class="org-haskell-operator">`AccFun`</span> <span class="org-haskell-type">SimpleNNParameters</span> n m <span class="org-haskell-type">Double</span>
<span class="org-haskell-definition">simpleNNGradAccFun</span> pair <span class="org-haskell-operator">=</span> toCcc (simpleNNGrad pair)
</pre>
</div>
</div>
</section>
</section>
</div>
</div>
<script src="/nix/store/9xl8b9acpgqmjwr9r39vcb7bgr0cw3i9-source/dist/reveal.js"></script>
<script src="/nix/store/9xl8b9acpgqmjwr9r39vcb7bgr0cw3i9-source/plugin/notes/notes.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: false,
center: true,
slideNumber: 'c',
rollingLinks: false,
keyboard: true,
mouseWheel: false,
fragmentInURL: false,
hashOneBasedIndex: false,
pdfSeparateFragments: true,
overview: true,
transition: 'none',
transitionSpeed: 'default',
// Plugins with reveal.js 4.x
plugins: [ RevealNotes ],
// Optional libraries used to extend reveal.js
dependencies: [
]
});
</script>
</body>
</html>