forked from DJCordhose/ml-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2019-mcubed-tf-intro.html
1563 lines (1239 loc) · 46.9 KB
/
2019-mcubed-tf-intro.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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>MCubed TF2</title>
<link rel="stylesheet" href="reveal.js/css/reset.css">
<link rel="stylesheet" href="reveal.js/css/reveal.css">
<!-- <link rel="stylesheet" href="reveal.js/css/theme/black.css"> -->
<link rel="stylesheet" href="reveal.js/css/theme/solarized.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="reveal.js/lib/css/monokai.css">
<style>
/*pre code {*/
/*display: block;*/
/*padding: 0.5em;*/
/*background: #FFFFFF !important;*/
/*color: #000000 !important;*/
/*}*/
.right-img {
margin-left: 10px !important;
float: right;
height: 500px;
}
.todo:before {
content: 'TODO: ';
}
.todo {
color: red !important;
}
code span.line-number {
color: lightcoral;
}
.reveal pre code {
max-height: 1000px !important;
}
img {
border: 0 !important;
box-shadow: 0 0 0 0 !important;
}
.reveal {
-ms-touch-action: auto !important;
touch-action: auto !important;
}
.reveal h2,
.reveal h3,
.reveal h4 {
letter-spacing: 2px;
font-family: 'Amiri', serif;
/* font-family: 'Times New Roman', Times, serif; */
font-weight: bold;
font-style: italic;
letter-spacing: -2px;
text-transform: none !important;
}
.reveal em {
font-weight: bold;
}
.reveal .step-subtitle h1 {
letter-spacing: 1px;
}
.reveal .step-subtitle h2,
.reveal .step-subtitle h3 {
text-transform: none;
font-style: italic;
font-weight: normal;
/* font-weight: 400; */
/* font-family: 'Amiri', serif; */
font-family: 'Lobster', serif;
letter-spacing: 1px;
color: #2aa198;
text-decoration: underline;
}
.reveal .front-page h1,
.reveal .front-page h2 {
font-family: "League Gothic";
font-style: normal;
text-transform: uppercase !important;
letter-spacing: 1px;
}
.reveal .front-page h1 {
font-size: 2.5em !important;
}
.reveal .highlight {
background-color: #D3337B;
color: white;
}
.reveal section img {
background: none;
}
.reveal img.with-border {
border: 1px solid #586e75 !important;
box-shadow: 3px 3px 1px rgba(0, 0, 0, 0.15) !important;
}
.reveal li {
margin-bottom: 8px;
}
/* For li's that use FontAwesome icons as bullet-point */
.reveal ul.fa-ul li {
list-style-type: none;
}
</style>
<!-- Printing and PDF exports -->
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
var printMode = window.location.search.match(/print-pdf/gi);
link.href = printMode ? 'reveal.js/css/print/pdf.css' : 'reveal.js/css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section data-markdown class="preparation">
<textarea data-template>
### Preparation
</textarea>
</section>
<section data-markdown style="font-size: xx-large">
<textarea data-template>
### Workshop: Introduction to Deep Learning with TensorFlow 2
If you are bored
1. #wifi Name: xxx, pwd: xxx
1. Open this slide deck: http://bit.ly/mcubed-tf2
1. Make sure you are ready to work with Colab
* open http://bit.ly/mcubed-tf2-low-level in Chrome
* make it run using the "Run All" command from the "Runtime" menu
* you need to allow execution and must either have a Google login or are willing to create one
_Talk to your neighbors or ask Olli for help_
</textarea>
</section>
<!--
https://www.mcubed.london/sessions/workshop-introduction-to-deep-learning-with-tensorflow-2/
Workshop: Introduction to Deep Learning with TensorFlow 2
We will touch on classic Neural Networks, Convolutional Neural Networks (CNNs) for image processing, and Recurrent
Neural Networks (RNNs) for processing of texts and other sequences.
We will use TensorFlow 2 with low-level and Keras-style layers and provide notebooks hosted on Google’s Colab, that
allow them to run on GPU. Thus there will be no need for any installation, all you need is a Chrome browser. We will use
Python as our language, so basic knowledge in object oriented programming is desirable.
Required audience experience
Basic experience in object oriented programming, machine learning and neural networks.
Objective of the workshop
Giving basic insights into what neural networks can do and how to train them using TensorFlow 2.
-->
<section data-markdown class="todo">
<textarea data-template>
* bit.ly
* http://bit.ly/mcubed-tf2: https://djcordhose.github.io/ml-workshop/2019-mcubed-tf-intro.html
* http://bit.ly/mcubed-tf2-low-level
* Wifi erste Folie
* RNN: Unterschiede LSTM/GRU/simple RNN anhand von langen Ketten besser herausarbeiten (in advanced time series)
* Alle Bilder und Notebooks rüber kopieren die wir brauchen
* Links auf Notebooks anpassen
* Ausdünnen wo geht, alle Details weglassen
* https://twitter.com/karpathy/status/1138699798217781248
*
</textarea>
</section>
<section data-markdown class="todo">
<textarea data-template>
### Grafiken aus AI rüberkopieren
</textarea>
</section>
<section>
<br>
<br>
<h2>Workshop: Introduction to Deep Learning with TensorFlow 2</h2>
<br>
<br>
<p><a target="_blank" href="https://www.mcubed.london/sessions/workshop-introduction-to-deep-learning-with-tensorflow-2/">
MCubed, London, October 2019
</a></p>
<h4><a href="http://zeigermann.eu">Oliver Zeigermann</a> /
<a href="http://twitter.com/djcordhose">@DJCordhose</a>
</h4>
<p><small><a href="http://bit.ly/mcubed-tf2">
http://bit.ly/mcubed-tf2
</a></small></p>
</section>
<section data-markdown class="local">
<textarea data-template>
## Questions, comments, critique are welcome at any time
</textarea>
</section>
<section data-markdown class="todo">
<textarea data-template>
## Scatchpad to share links and information
On Google Drive, everyone with link can read and edit
</textarea>
</section>
<section data-markdown class="local">
<textarea data-template>
## Introduce yourself to your neighbors, please
* What are you working on?
* What do you want to achieve with TensorFlow?
* What do you already know about ML and TensorFlow?
* If necessary please help your neighbors to get to theses slides http://bit.ly/mcubed-tf2 and make the first notebook running http://bit.ly/mcubed-tf2-low-level (described in first slide)
</textarea>
</section>
<section data-markdown class='fragments'>
<textarea data-template>
### Morning: Basics using Low-Level TensorFlow
- basics of artificial neurons
- backpropagation
- linear regression
- classification
### Afternoon: Networks using Keras API
* Tabular Data: Dense Neural Networks
* Sequences: Recurrent Neural Networks
* Images: Convolutional Neural Networks
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Interactive, online introduction to TensorFlow low level API
- understand how artificial neurons can be encoded as a matrix multiplication
- learn how to create your own layers
- get an idea how a loss function and an optimizer can be used to train a neural network
- see which influence activation functions have
<small>
https://colab.research.google.com/github/djcordhose/ml-workshop/blob/master/notebooks/tf2/tf-low-level.ipynb
</small>
</textarea>
</section>
<section data-markdown>
<textarea data-template>
## Block II
### Netzwerke für strukturierte Daten (Fully Connected Layers)
</textarea>
</section>
<section>
<h3>Example: Customer Data - Risk of Accidents</h3>
<img src="img/manning/all.png" height="400px" class="fragment">
<p class="fragment">
<small>How would you rank me (48) for a car having 100 mph top speed, driving 10k miles per year?</small>
</p>
</section>
<section data-markdown>
<textarea data-template>
## Types of Learning
<img src='img/types-of-ml.jpg'>
<small>
https://www.facebook.com/nipsfoundation/posts/795861577420073/
<br>
https://ranzato.github.io/publications/tutorial_deep_unsup_learning_part1_NeurIPS2018.pdf
</small>
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Sample Data
<div style="max-width: 50%; float: left;">
<img src='img/df_head.jpg' height="450">
</div>
<div style="max-width: 50%; float: right;">
<br>
<br>
<br>
<ul>
<li>0 - red: many accidents</li>
<li>1 - green: few or no accidents</li>
<li>2 - yellow: in the middle</li>
</ul>
</div>
</textarea>
</section>
<!-- <section data-markdown>
<textarea data-template>
### Classification vs Regression
_Regressions predict a quantity, and classifications predict a label_
1. Regression: Fitting a line through data points
2. Classification: What category can be derived from data
<img src="img/sketch/classification.jpg" height="300px">
<small>
_What type of problem are we dealing with here?_
</small>
</textarea>
</section> -->
<section data-markdown>
<textarea data-template>
## Shared Exercise
_Sketch the Architecture of our model_
* How does the input look like?
* And the output?
* How to to connect them?
* How to encode our data to match the network structure?
_Key to architecture: What do we want to predict and what do we have as input?_
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### What goes in?
<img src='img/insurance/data_encoding.jpg'>
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### What comes out?
<img src='img/insurance/encoding2.jpg'>
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Role of the Hidden Layer(s)
<img src='img/insurance/encoding3.jpg'>
</textarea>
</section>
<section>
<h3>Next step: Encode this with Keras Layers</h3>
<p><small>Sequential Model</small></p>
<pre><code contenteditable data-trim class="fragment line-numbers python">
model = keras.Sequential()
</code></pre>
<p><small>Fully Connected Hidden Layer</small></p>
<pre><code contenteditable data-trim class="fragment line-numbers python">
model.add(Dense(units=50, input_dim=3))
</code></pre>
<p><small>Softmax Output Layer</small></p>
<pre><code contenteditable data-trim class="fragment line-numbers python">
model.add(Dense(units=3, activation='softmax'))
</code></pre>
<small>
<a href="https://www.tensorflow.org/alpha/guide/keras/">
https://www.tensorflow.org/alpha/guide/keras/
</a>
</small>
</p>
</section>
<!-- <section data-markdown>
<textarea data-template>
### Intuition for the learning process
_network stretches and folds the paper until it can find a line to separate red from blue_
<video controls
poster='video/layer-linear.jpg'
src="video/layer.mp4" type="video/mp4" height="300"></video>
<small>
https://twitter.com/random_forests/status/1084618439602298881
<br>
http://colah.github.io/posts/2014-03-NN-Manifolds-Topology/
<br>
https://cs.stanford.edu/people/karpathy/convnetjs/
<br>
https://brohrer.github.io/what_nns_learn.html
</small>
</textarea>
</section> -->
<section data-markdown>
<textarea data-template>
<h3>What does the neural network learn?</h3>
<p>Optimal values of weights (+biases) for all neurons</p>
<pre><code contenteditable data-trim class="line-numbers python">
model.summary()</code></pre>
<pre><code contenteditable data-trim class="line-numbers python">
_________________________________________________________________
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
hidden1 (Dense) (None, 50) 200
_________________________________________________________________
softmax (Dense) (None, 3) 153
=================================================================
Total params: 353
Trainable params: 353
Non-trainable params: 0
_________________________________________________________________</code></pre>
<small>
How parameters are related to loss is expressed by the chain rule:
<br>
https://towardsdatascience.com/back-propagation-demystified-in-7-minutes-4294d71a04d7
</small>
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### How do we determine the loss?
<pre><code contenteditable data-trim class="line-numbers python">
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam')
</code></pre>
<pre><code contenteditable data-trim class="line-numbers python">
model.fit(X, y)</code></pre>
<div style="font-size: xx-large">
* cross entropy can be used as an error measure when a network's outputs can be thought of as representing independent hypotheses
* activations can be understood as representing the probability that each hypothesis might be true
* the loss indicates the distance between what the network believes this distribution should be, and what the teacher says it should be
* can use a sparse instead of a one-hot-encoding
</div>
<small>
https://en.wikipedia.org/wiki/Cross_entropy
http://www.cse.unsw.edu.au/~billw/cs9444/crossentropy.html
</small>
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Loss over time while training
<img src='img/loss.png'>
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Evaluation Metrics: Accuracy
<script type="math/tex; mode=display">
accuracy = {\frac {correct\;predictions}{number\;of\;samples}}
</script>
_often given for training and test data separately_
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Accuracy over time while training
<img src='img/training.png'>
</textarea>
</section>
<section data-markdown>
<textarea data-template>
## Exercise
_Create the TensorFlow model and at make it train_
* We will start with the notebook provided and go though it step by step
* Write down a model that trains at least a bit
* How many hidden layers?
* How many neurons per layer?
* How good can you get?
* Optional: Can you create a plot for loss over time?
<small>
https://colab.research.google.com/github/djcordhose/ai/blob/master/notebooks/tf2/nn-model.ipynb
</small>
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Generalization
_We do not have any idea how well our model performs in the real world, yet_
</textarea>
</section>
<section data-markdown>
<textarea data-template>
## Supervised Learning Process Flow
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Training
<img src='img/flow-train.jpg'>
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Use some training data for validation
<img class='fragment' src='img/insurance/generalization1.jpg'>
</textarea>
</section>
<!-- <section data-markdown>
<textarea data-template>
### Prediction
<img src='img/flow-prediction.jpg' height="500">
</textarea>
</section>
-->
<section data-markdown>
<textarea data-template>
### Splitting data sets
<pre><code contenteditable data-trim class="line-numbers python">
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test =
train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
</code></pre>
<small>
https://colab.research.google.com/github/djcordhose/ai/blob/master/notebooks/tf2/nn-training.ipynb
</small>
<!-- <small>
In the real world, test and training often are split anyway and come from different sources
</small> -->
</textarea>
</section>
<section data-markdown>
<textarea data-template>
## Regularization
</textarea>
</section>
<section id='overfitting'>
<h3>The Issue: Overfitting</h2>
<div>
<div style="float: left">
<img src="img/scans/elements/80_percent.jpg" height="200" class="fragment" data-fragment-index='1'>
<p>
<small><em>Training Score</em></small>
</p>
</div>
<div style="float: left" class="fragment" data-fragment-index='5'>
<img src="img/scans/elements/down.jpg" height="200">
</div>
<div style="float: left" class="fragment" data-fragment-index='4'>
<img src="img/scans/elements/up.jpg" height="200">
</div>
<div style="float: left">
<img src="img/scans/elements/70_percent.jpg" height="200" class="fragment" data-fragment-index='2'>
<p>
<small><em>Test Score</em></small>
</p>
</div>
</div>
<p style="clear: both" class="fragment" data-fragment-index='3'><em>Training and test scores clearly divert</em></p>
</section>
<section data-markdown>
<textarea data-template>
### Regularization
_Process to counter overfitting_
* When there are more variables than data points,
the problem may not have a unique solution
* There may be multiple (perhaps infinitely many) solutions that fit the data equally well
* The existence of more variables than data points,
the existence of multiple solutions, and overfitting often coincide
<small>
https://stats.stackexchange.com/questions/223486/modelling-with-more-variables-than-data-points/223517#223517
</small>
</textarea>
</section>
<section>
<h3>Illustration using Loss Landscape</h2>
<div>
<div style="float: left" class="fragment">
<img src="img/resnet56_noshort_small.jpg" height="350">
<p>
<small><em>deep network, sharp surface, many solutions</em></small>
</p>
</div>
<div style="float: right" class="fragment">
<img src="img/resnet56_small.jpg" height="350">
<p>
<small><em>residual shortcuts, smooth surface, naturally converging</em></small>
</p>
</div>
<p style="clear: both"><small>ResNet Architecture having 56 layers
<br>
<a href='https://github.com/tomgoldstein/loss-landscape#visualizing-3d-loss-surface'>
https://github.com/tomgoldstein/loss-landscape#visualizing-3d-loss-surface
</a>
</small></p>
</section>
<section data-markdown>
<textarea data-template>
### First approach: Train for fewer epochs
<img src='img/accuracy.png'>
_Watch where training and validation accuracy diverge and stop training there_
<small>
Early stopping possible:
<br>
https://keras.io/callbacks/#earlystopping
<br>
https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/EarlyStopping
</small>
</textarea>
</section>
<section id='overfitting-capacity'>
<h3>Second approach: Reduce capacity of model</h2>
<div style="float: left; width: 400px" class="fragment" data-fragment-index='1'>
<img src="img/scans/elements/model-large.jpg" height="200">
<p>
<small><em>Original model</em></small>
</p>
</div>
<div style="float: left; width: 200px" class="fragment" data-fragment-index='2'>
<br>
<img src="img/scans/elements/right.jpg">
<br>
</div>
<div style="float: left; width: 500px" class="fragment" data-fragment-index='3'>
<br>
<img src="img/scans/elements/model-small.jpg" height="100">
<br>
<br>
<p>
<small><em>Smaller model</em><br>less hidden layers, less neurons per layer</small>
</p>
</div>
<p style="clear: both" class="fragment" data-fragment-index='4'><em>Intuition: Give model less capacity to simply memorize data</em></p>
</section>
<section id='overfitting-dropout'>
<h3>Third approach: Use Dropout</h2>
<p><em>Dropouts only train a certain percentage of neurons per batch</em></p>
<div style="float: left; width: 400px" class="fragment" data-fragment-index='1'>
<img src="img/scans/elements/model-large.jpg" height="225">
<p>
<small><em>Original model</em></small>
</p>
</div>
<div style="float: left; width: 200px" class="fragment" data-fragment-index='2'>
<br>
<img src="img/scans/elements/right.jpg">
<br>
</div>
<div style="float: left; width: 500px" class="fragment" data-fragment-index='3'>
<br>
<img src="img/scans/elements/model-emsemble.jpg" height="100">
<br>
<br>
<p>
<small><em>Ensemble of small models</em> (each one overfits on its specific batch)<br></small>
</p>
</div>
<p style="clear: both" class="fragment" data-fragment-index='4'><em>Intuition: Combination of models makes result more robust</em></p>
</section>
<section data-markdown id='overfitting-bn'>
<textarea data-template>
### Fourth approach: Batch Normalization
<ul>
<li class="fragment">Subtracts batch mean
<li class="fragment">Multiplies by standard deviation
</ul>
<!-- <img src='img/scans/elements/sigmoid.jpg' class="fragment" height="200"> -->
<p class="fragment"><em>Intuition: Makes model robust by adding noise</em></p>
<p class="fragment"><small><em>Bonus:</em> Lets model train faster by fighting vanishing gradients</small></p>
<small>
http://gradsci.org/batchnorm
<br>
https://www.youtube.com/watch?v=ZOabsYbmBRM
<br>
Batch Norm is often frowned upon, because it is brittle magic and a small change in implementation can cause a big effect: https://twitter.com/martin_wicke/status/1092217017396953088
</small>
</textarea>
</section>
<section data-markdown style="font-size: xx-large">
<textarea data-template>
### Fifth approach: L1/L2 weight Regularization
* make model less complex by forcing low values for weights (less complexity, more regular)
* adds penalty term to loss function
* L1 (Lasso Regression): penalty is proportional to the absolute value of the weights coefficients
* helps drive the weights of irrelevant or barely relevant features to exactly 0
* L2 ( Ridge Regression): penalty is proportional to the square of value of the weights coefficients
* heavily penalizes especially large coefficients
<small style="font-size: large">
https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/keras/overfit_and_underfit.ipynb#scrollTo=4rHoVWcswFLa
<br>
https://towardsdatascience.com/l1-and-l2-regularization-methods-ce25e7fc831c
</small>
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Sixth approach: Get more training data
_if you can_
if not
* try augmenting existing data
* use transfer learning
</textarea>
</section>
<section data-markdown style='font-size: xx-large' class='advanced'>
<textarea data-template>
### How big should your network be
* Universal Approximation Theorem:
one hidden layer containing a finite number of neurons can approximate any continuous functions to arbitrary accuracy
* does not guarantee whether the model can be trained or generalizes properly
* There exists a two-layer neural network with ReLU activations
and 2n+d weights that can represent any function on a sample of size n in d dimensions
* can learn unstructured random noise perfectly
* these are theoretical insights
* what really works needs to be shown be experiment
* more layers might reduce overall number of neurons
* 2-3 hidden layers is a good rule of thumb
<small>
https://lilianweng.github.io/lil-log/2019/03/14/are-deep-neural-networks-dramatically-overfitted.html
https://arxiv.org/abs/1611.03530
</small>
</textarea>
</section>
<section data-markdown style='font-size: xx-large' class='advanced'>
<textarea data-template>
### Local minima?
* local optimal points in the objective landscape almost always lay in saddle-points or plateaus rather than valleys
* there is always a subset of dimensions containing paths to leave local optima and keep on exploring
<img src='img/optimization-landscape-shape.png'>
<small>
https://lilianweng.github.io/lil-log/2019/03/14/are-deep-neural-networks-dramatically-overfitted.html
https://arxiv.org/abs/1406.2572
<br>
https://www.offconvex.org/2016/03/22/saddlepoints/
</small>
</textarea>
</section>
<section data-markdown class="smaller">
<textarea data-template>
## Exercise
_Regularize your model_
* Find out how to apply those regularizations from the notebooks supplied
* You can just as well start with this notebook
* Make sure you are optimizing for test, not for train score
* How good can you get?
<!-- * Advanced:
1. Add Early Stopping Callback
1. Add L1/L2 weight Regularization to your model
* Run notebook from previous slide to see how this works -->
<small>
https://colab.research.google.com/github/djcordhose/ai/blob/master/notebooks/tf2/nn-reg.ipynb
</small>
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Best known model using 3 dimensions
Up to 80% of accuracy
<small>
https://colab.research.google.com/github/djcordhose/ai/blob/master/notebooks/tf2/nn-final.ipynb
</small>
</textarea>
</section>
<section data-markdown>
<textarea data-template>
## Block III
### Netzwerke für Sequenzen und Texte (Recurrent Layers)
</textarea>
</section>
<section>
<h3>How does this sequence continue?</h3>
<pre><code contenteditable data-trim class="line-numbers python">
[10, 20, 30, 40, 50, 60, 70, 80, 90]
</code></pre>
<p>Question: How do we train a network to predict the next number?</p>
</section>
<section data-markdown>
<textarea data-template>
### Challenge: Dense Networks have no memory of previous events
They lack capability to deal with sequential data, which is required to predict time series or "understand" text
</textarea>
</section>
<section data-markdown class='advanced'>
<textarea data-template>
### Solution: Recurrent Neural Networks (RNNs)
_If training vanilla neural nets is optimization over functions, training recurrent nets is optimization over programs._
RNNs are Turing-Complete
<small>
http://karpathy.github.io/2015/05/21/rnn-effectiveness/
<br>
http://binds.cs.umass.edu/papers/1995_Siegelmann_Science.pdf
</small>
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### RNNs - Networks with Loops
<img src='img/nlp/colah/RNN-rolled.png' height="450px">
<small>
http://colah.github.io/posts/2015-08-Understanding-LSTMs/
</small>
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Unrolling the loop
<img src='img/nlp/colah/RNN-unrolled.png'>
<small>
http://colah.github.io/posts/2015-08-Understanding-LSTMs/
</small>
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Simple RNN
<img src='img/nlp/fchollet_rnn.png'>
<script type="math/tex; mode=display">
output_t = \tanh(W input_t + U output_{t-1} + b)
</script>
<small>
<a href="https://livebook.manning.com/#!/book/deep-learning-with-python/chapter-6/129">
Deep Learning with Python, Chapter 6, François Chollet, Manning
</a>
</small>
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Question
_Even having a network that can deal with time sequences, how do you train it using our data?_
</textarea>
</section>
<section>
<h3>First Step: Slice and Splice data to have a training set</h3>
<p>Training Data, sliced to only use 3 past events</p>
<pre><code contenteditable data-trim class="fragment line-numbers python">
[10, 20, 30] => 40
[20, 30, 40] => 50
[30, 40, 50] => 60
[40, 50, 60] => 70
[50, 60, 70] => 80
[60, 70, 80] => 90