-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.html
2839 lines (2456 loc) · 89.7 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
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>A Gentle Introduction to Deep Learning with Tensorflow - PyCon 2017</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/beige.css">
<link rel="stylesheet" href="css/custom.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/googlecode.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<!-- Title slide -->
<section>
<h2>A gentle introduction to deep learning with TensorFlow</h2>
<p>Michelle Fullwood<br />
@michelleful</p>
<p> </p>
<p>Slides: michelleful.github.io/PyCon2017</p>
<aside class="notes">
Welcome to A Gentle Introduction to Deep Learning.
So this is an intermediate level talk,
</aside>
</section>
<!-- Introduction -->
<section>
<section>
<h2>Prerequisites</h2>
<ul>
<li>Knowledge of concepts of supervised ML</li>
<li>Familiarity with linear and logistic regression</li>
</ul>
<aside class="notes">
and I'm going to assume that you know the concepts
of supervised machine learning and are familiar with
linear and logistic regression. That will be our
STARTING POINT.
</aside>
</section>
<section>
<h2>Target</h2>
(Deep) Feed-forward neural networks
<p>
<img src="images/nnz_mlp.png"
style="width: 20%;">
</p>
<ul>
<li>How they're constructed</li>
<li>Why they work</li>
<li>How to train and optimize them</li>
</ul>
<p style="font-size: 35%; text-align: left;">Image source: Fjodor van Veen (2016) <a href="http://www.asimovinstitute.org/neural-network-zoo/">Neural Network Zoo</a></p>
<aside class="notes">
Our target end point is an in-depth understanding
of the MOST FUNDAMENTAL CLASS OF NEURAL NETWORKS,
FEEDFORWARD NEURAL NETWORKS, which look like this.
</aside>
</section>
<section data-transition="fade-out">
<h2>Deep learning learning curve</h2>
<img src="images/latex_generated_images/learning_curve_nopoints.png"
alt="Completely made-up learning curve"
style="width: 60%;">
<aside class="notes">
So along this completely made-up deep learning learning curve,
I'm going to put our target here.
</aside>
</section>
<section data-transition="fade">
<h2>Deep learning learning curve</h2>
<img src="images/latex_generated_images/learning_curve_target.png"
alt="Our goal is here: completely understanding of the most
fundamental class of neural networks, feedforward networks
or multi-layer perceptrons. How they work, how to train them,
how to optimize them."
style="width: 60%;">
<aside class="notes">
And the way I hope to make this talk "gentle" is by
convincing you that if you meet the prerequisites for this
talk, you aren't here...or here...but here.
</aside>
</section>
<section data-transition="fade">
<h2>Deep learning learning curve</h2>
<img src="images/latex_generated_images/learning_curve_not_here.png"
alt="If you meet the prerequisites for this talk, namely,
you know the fundamentals of supervised machine learning
and are familiar with linear and logistic regression,
then you're not here..."
style="width: 60%;">
</section>
<section data-transition="fade">
<h2>Deep learning learning curve</h2>
<img src="images/latex_generated_images/learning_curve_nor_here.png"
alt="...nor here..."
style="width: 60%;">
</section>
<section data-transition="fade">
<h2>Deep learning learning curve</h2>
<img src="images/latex_generated_images/learning_curve_but_here.png"
alt="but you're actually here!"
style="width: 60%;">
<aside class="notes">
If you know logistic regression, you're but two
tiny steps away from deep learning.
</aside>
</section>
<section>
<table style="border-collapse:collapse;">
<tr>
<td style="border: none; text-align: center">
<img src="images/hammer.png"
alt="To use a metaphor, "
style="width: 50%; display: block;
margin-left: auto; margin-right: auto;">
</td>
<td style="border: none; text-align: center;">
<div class="fragment" data-fragment-index="1">
<img src="images/tensorflow_logo_big.png"
style="width: 55%; display: block;
margin-left: auto; margin-right: auto">
</div>
</td>
</tr>
<tr>
<td style="text-align: center;">Traditional machine learning</td>
<td style="text-align: center;"><div class="fragment" data-fragment-index="1">Deep learning</div></td>
</tr>
</table>
<aside class="notes">
To put things another way, IF TRADITIONAL MACHINE LEARNING
IS A HAMMER, !CLICK! then DEEP LEARNING IS JUST ANOTHER, FANCIER
HAMMER. It might not LOOK like a hammer at first, but, you
know, you can pick that thing up, you can start bashing
things. It USES THE SAME TECHNOLOGIES, IT OBEYS THE
SAME LAWS OF PHYSICS.
But at the same time, it is a PRETTY WEIRD HAMMER with
all these extra knobs and whistles. So we'll
talk about what the extra knobs and whistles buy us
in terms of power and performance.
</aside>
</section>
<section>
<h2>TensorFlow</h2>
<ul>
<li>Popular deep learning toolkit</li>
<li>From Google Brain, Apache-licensed</li>
<li>Python API, makes calls to C++ back-end</li>
<li>Works on CPUs and GPUs</li>
</ul>
<aside class="notes">
And we're going to learn how to do all this in TensorFlow,
an open-source deep learning toolkit out of Google.
</aside>
</section>
</section>
<!-- LINEAR REGRESSION IN NUMPY -->
<section>
<section>
<h2>Linear Regression<br/>from scratch</h2>
<aside class="notes">
OK! Let's talk about linear regression. We're
going to code up a linear regressor FROM SCRATCH.
And as we go through this section, I want you to
FOCUS NOT SO MUCH ON THE CODE, but ON THE
INGREDIENTS. What are they, how do they go together.
</aside>
</section>
<section>
<h2>Linear Regression</h2>
<img src="images/regression_feature_floor_area.png"
style="width:14%; margin:2%"
alt = "Feature: floor area"
class="fragment" data-fragment-index="3">
<img src="images/regression_feature_distance.png"
style="width:18%; margin:2%;"
alt="Feature: distance from public transportation"
class="fragment" data-fragment-index="4">
<img src="images/regression_feature_number_of_rooms.png"
alt="Feature: number of bedrooms"
style="width:14%; margin:2%"
class="fragment" data-fragment-index="5">
<img src="images/right_arrow.png"
alt="are predictors for"
style="width:15%"
class="fragment" data-fragment-index="2">
<img src="images/regression_target_house_price.png"
alt="Target value for regression: house price"
style="width:18%;">
<aside class="notes">
Here's a typical linear regression problem.
We're trying to PREDICT PRICES OF INDIVIDUAL HOUSES.
And we're given three pieces of information
about each house, three features:
!CLICK! FLOOR AREA,
!CLICK! DISTANCE FROM PUBLIC TRANSPORT,
!CLICK! Number of rooms.
</aside>
</section>
<section>
<h2>Inputs</h2>
<img src="images/latex_generated_images/matrix_big_numbers.png"
style="width:80%"
alt = "Represent multiple x's in an mxn matrix
and y's in a mx1 vector">
<aside class="notes">
And we're going to represent our features in a matrix
with as many rows as we have houses and three columns,
one for each of our input features. We'll call that matrix X.
And we're trying to predict this vector Y, which represents
the housing prices.
</aside>
</section>
<section>
<h2>Inputs</h2>
<pre>
<code data-trim data-noescape class="python">
X_train = np.array([
[1250, 350, 3],
[1700, 900, 6],
[1400, 600, 3]
])
Y_train = np.array([345000, 580000, 360000])
</code>
</pre>
<aside class="notes">
Here's what that looks like in numpy.
</aside>
</section>
<section>
<h2>Model</h2>
<p>
Multiply each <b>feature</b> by a <b>weight</b> and add them up.<br/>
Add an <b>intercept</b> to get our final <b>estimate</b>.
</p>
<aside class="notes">
Next we have to consider our MODEL. The model
is the set of functions that we're going to
consider in mapping X to Y. Since this is
linear regression, we'll multiply each feature
by...(off slide)
</aside>
</section>
<section>
<h2>Model</h2>
<img src="images/latex_generated_images/linear_regression_2d_fitline.png"
style="width:50%"
alt = "Linear regression is a straight line">
<aside class="notes">
And that corresponds to drawing the line of best fit
through the data.
</aside>
</section>
<section>
<h2>Model - Parameters</h2>
<pre>
<code data-trim data-noescape class="python">
weights = np.array([300, -10, -1])
intercept = -26497
</code>
</pre>
<aside class="notes">
So the parameters of this model will be the
three weights that correspond to each feature,
and the intercept.
</aside>
</section>
<section data-transition="fade">
<h2>Model - Operations</h2>
<img src="images/latex_generated_images/matrix_mult_4.png"
style="width:70%"
alt = "TODO: show calculation, then show addition of intercept">
<aside class="notes">
And the key operation of this model will be matrix
multiplication of X by the weights. Then we'll add
the intercept element-wise to get our
final prediction.
</aside>
</section>
<section>
<h2>Model - Operations</h2>
<pre>
<code data-trim data-noescape class="python">
def model(X, weights, intercept):
return X.dot(weights) + intercept
Y_hat = model(X_train, weights, intercept)
</code>
</pre>
</section>
<section data-transition="fade-out">
<h2>Model - Cost function</h2>
<img src="images/latex_generated_images/linear_regression_2d_multiplepoints.png"
style="width:50%"
alt = "Okay so we had a pretty bad fit...let's measure it">
<aside class="notes">
Now the next ingredient we'll need is a COST FUNCTION,
also called a LOSS FUNCTION. We need this to measure how
good or bad a set of parameters is, how close our predictions
are getting to the actual values. For example, this
is a really badly-fit line.
</aside>
</section>
<section data-transition="fade">
<h2>Model - Cost function</h2>
<img src="images/latex_generated_images/linear_regression_2d_multiplepoints_witherrorlines.png"
style="width:50%"
alt = "Drop a line from the actual y to the estimated y_hat">
<aside class="notes">
So we'll do is take the difference between the prediction
and the actual value, and square it.
</aside>
</section>
<section data-transition="fade-in">
<h2>Model - Cost function</h2>
<img src="images/latex_generated_images/linear_regression_2d_multiplepoints_betterfit.png"
style="width:50%"
alt = "Drop a line from the actual y to the estimated y_hat">
</section>
<section>
<h2>Cost function</h2>
<pre>
<code data-trim data-noescape class="python">
def cost(Y_hat, Y):
return np.sum((Y_hat - Y)**2)
</code>
</section>
<section>
<h2>Optimization</h2>
<p>Hold X and Y constant.<br/>Adjust <b>parameters</b> to minimize <b>cost</b>.</p>
<aside class="notes">
Now we need to actually find the parameters that give us the best fit.
In other words, holding X and Y constant, we'll adjust our parameters
to minimize the cost.
</aside>
</section>
<section data-transition="fade-out">
<h2>Optimization</h2>
<img src="images/latex_generated_images/cost_function_no_tangents.png"
alt="Graph of cost with respect to weights"
style="width: 50%;">
<aside class="notes">
Each set of parameters will yield a cost, so we can
plot cost against parameter values. Our goal in
optimization is to find the parameters that correspond
to that lowest point.
</aside>
</section>
<section>
<h2>Trial and error</h2>
<img src="images/shooting_hoops.jpg"
alt="Shooting hoops - adjust angle by trial and error"
style="width: 50%;">
<p style="font-size: 35%; text-align: left;">Image source: <a href="https://commons.wikimedia.org/wiki/File:Barack_Obama_playing_basketball.jpg">Wikimedia Commons</a></p>
<aside class="notes">
And we're going to do that by trial and error. By this I don't mean
just trying random sets of parameters and seeing what works best,
but the trial and error you do when you're, say, practising how
to shoot hoops and you're trying to adjust your angle. So you shoot,
and you miss by a couple inches. You're too far to the right. So
you adjust your angle to the left and try again.
</aside>
</section>
<section data-transition="fade">
<h2>Optimization</h2>
<img src="images/latex_generated_images/cost_function_with_tangents.png"
alt="Follow tangents down to the weight with the lowest cost"
style="width: 50%;">
<aside class="notes">
That's what we're going to do also. We'll try a set of parameters,
then we'll calculate our cost, and then we'll follow the gradient
of the cost curve at that point down towards the minimum. This
process is called GRADIENT DESCENT.
</aside>
</section>
<section data-transition="fade-out">
<h2>Optimization</h2>
<img src="images/latex_generated_images/cost_function_goldilocks.png"
alt="Nice trajectory down towards the minimum"
style="width: 50%;">
</section>
<section data-transition="fade-out">
<h2>Optimization - Gradient Calculation</h2>
<p>$$\hat{y} = w_0x_0 + w_1x_1 + w_2x_2 + b$$
$$\epsilon = (y-\hat{y})^2$$</p>
<p> </p>
<p><b>Goal:</b> \(\frac{\partial\epsilon}{\partial w_i}, \frac{\partial\epsilon}{\partial b}\)</p>
<aside class="notes">
So we need to be able to calculate the gradient of
the cost, epsilon, with respect to each of the weights and
the intercept.
</aside>
</section>
<section data-transition="fade">
<h2>Optimization - Gradient Calculation</h2>
<p><b>Chain rule:</b> \(\frac{\partial\epsilon}{\partial w_i} =
\frac{d\epsilon}{d\hat{y}}\frac{\partial\hat{y}}{\partial w_i} \)
<aside class="notes">
Applying the chain rule, we can break that up into
two pieces: the gradient of the cost with respect to
the predicted y, y hat, and the gradient of y hat
with respect to the weight. So let's calculate those.
</aside>
</section>
<section data-transition="fade">
<h2>Optimization - Gradient Calculation</h2>
<p>$$\hat{y} = w_0x_0 + w_1x_1 + w_2x_2 + b$$</p>
<p> </p>
<p>\(\frac{\partial\hat{y}}{\partial w_0} =\)<span class="fragment">\( x_0\)</span></p>
<aside class="notes">
The gradient of y hat with respect to w naught is pretty simple.
All the terms are constant with respect to w naught so those go
to zero and we're left with x naught.
</aside>
</section>
<section data-transition="fade">
<h2>Optimization - Gradient Calculation</h2>
<p>$$\epsilon = (y-\hat{y})^2$$</p>
<p> </p>
<p>\(\frac{d\epsilon}{d\hat{y}} =\) <span class="fragment"><span class="fragment">\(-\)</span>\(2(y-\hat{y})\)</span></p>
<aside class="notes">
For the second gradient, we bring down the power and then apply
the chain rule again to bring out that negative sign.
</aside>
</section>
<section data-transition="fade-in">
<h2>Optimization - Gradient Calculation</h2>
<p>\(\frac{\partial\hat{y}}{\partial w_0} = x_0\)</p>
<p>\(\frac{d\epsilon}{d\hat{y}} = -2(y-\hat{y})\)</p>
<p> </p>
\(\frac{\partial\epsilon}{\partial w_0} =
-2(y-\hat{y})x_0 \)
<aside class="notes">
So to get our desired gradient, we multiply those together
to get this expression. And that goes for all the weights.
</aside>
</section>
<section data-transition="fade-in">
<h2>Optimization - Gradient Calculation</h2>
<p>$$\hat{y} = w_0x_0 + w_1x_1 + w_2x_2 + b\cdot1$$</p>
<p> </p>
\(\frac{\partial\epsilon}{\partial b} =
-2(y-\hat{y})\cdot 1 \)
<aside class="notes">
As for the intercept b, we can consider that a special
weight where the x it corresponds to is always 1. So
that's the form the gradient will take with respect to b.
</aside>
</section>
<section>
<h2>Optimization - Gradient Calculation</h2>
<pre>
<code data-trim data-noescape class="python">
delta_y = y - y_hat
gradient_weights = -2 * delta_y * weights
gradient_intercept = -2 * delta_y * 1
</code>
</pre>
</section>
<section>
<h2>Optimization - Parameter Update</h2>
<pre>
<code data-trim data-noescape class="python">
weights = weights - gradient_weights
intercept = intercept - gradient_intercept
</code>
</pre>
<aside class="notes">
And then we just want to move the weights in the direction
of the gradient, and we do that by subtracting.
</aside>
</section>
<section data-transition="fade-out">
<h2>Optimization - Overshoot</h2>
<img src="images/latex_generated_images/cost_function_overshoot.png"
alt="If we take steps that are too big, we risk overshooting"
style="width: 50%;">
<aside class="notes">
But, just like when you're practising basketball,
you might overcorrect. You're too far to the right, you
adjust your angle to the left, and you wind up too far
to the left. So you move right, and now you've overshot
in the other direction. And maybe you get angrier
and angrier so you wind up even more wildly off as
time goes on. We can do this in gradient descent
also.
</aside>
</section>
<section data-transition="fade">
<h2>Optimization - Undershoot</h2>
<img src="images/latex_generated_images/cost_function_undershoot.png"
alt="If we take steps that are too small, convergence takes forever"
style="width: 50%;">
<aside class="notes">
Or you might have the opposite problem: you're too timid
in making your corrections, so it takes you forever to get
to the minimum. You converge really slowly.
And if you have a cost curve that's uglier than this,
with lots of local minima, you may get stuck inside
a local minimum.
</aside>
</section>
<section>
<h2>Optimization - Parameter Update</h2>
<pre>
<code data-trim data-noescape class="python">
learning_rate = 0.05
weights = weights - \
learning_rate * gradient_weights
intercept = intercept - \
learning_rate * gradient_intercept
</code>
</pre>
<aside class="notes">
So we're going to try and be Goldilocks, and
try to aim for something in between those two.
We regulate this using a hyperparameter
called the learning rate. The larger the
learning rate, the bigger the steps you take.
</aside>
</section>
<section>
<h2>Training</h2>
<pre style="font-size:55%;">
<code data-trim data-noescape class="python">
def training_round(x, y, weights, intercept,
alpha=learning_rate):
# calculate our estimate
y_hat = model(x, weights, intercept)
# calculate error
delta_y = y - y_hat
# calculate gradients
gradient_weights = -2 * delta_y * weights
gradient_intercept = -2 * delta_y
# update parameters
weights = weights - alpha * gradient_weights
intercept = intercept - alpha * gradient_intercept
return weights, intercept
</code>
</pre>
<aside class="notes">
Putting all that together, here's how training goes.
Whatever our current weights and intercept are,
we calculate our prediction, calculate our error,
compute the gradients, and update our parameters
by gradient descent.
</aside>
</section>
<section>
<h2>Training</h2>
<pre style="font-size:60%;">
<code data-trim data-noescape class="python">
NUM_EPOCHS = 100
def train(X, Y):
# initialize parameters
weights = np.random.randn(3)
intercept = 0
# training rounds
for i in range(NUM_EPOCHS):
for (x, y) in zip(X, Y):
weights, intercept = training_round(x, y,
weights, intercept)
</pre>
</code>
<aside class="notes">
That was a single round of training. The entire
training process involves first initializing
our parameters and doing some number of training rounds.
Whatever the weights and intercept are at the end,
that's what we'll use to predict with.
</aside>
</section>
<section>
<h2>Testing</h2>
<pre style="font-size:60%;">
<code data-trim data-noescape class="python">
def test(X_test, Y_test, weights, intercept):
Y_predicted = model(X_test, weights, intercept)
error = cost(Y_predicted, Y_test)
return np.sqrt(np.mean(error))
>>> test(X_test, Y_test, final_weights, final_intercept)
6052.79
</code>
</pre>
<aside class="notes">
And testing is simple: we get our estimate and figure out
how far off we were on average. And on this dataset, we
were about $6000 off.
</aside>
</section>
<section>
Uh, wasn't this supposed to be a talk about neural networks?
Why are we talking about linear regression?
<aside class="notes">
Okay, so you may be wondering: I came here to hear about
deep learning and neural networks. Why are we doing something
so basic as linear regression?
</aside>
</section>
<section>
<h2>Surprise! <br/> You've already made <br/> a neural network!</h2>
<aside class="notes">
Surprise! We actually just made a neural network!
</aside>
</section>
<section data-transition="fade-out">
<h2>Linear regression = <br/> Simplest neural network</h2>
<img src="images/latex_generated_images/linear_regression_as_neural_network.png"
style="width:30%"
alt = "Linear regression is basically the simplest possible neural network">
<aside class="notes">
Linear regression is one of the simplest possible neural networks.
It's so simple that we don't even call it a neural network, because
it preceded neural networks. But if you look at the definition of
a neural network, linear regression fits the bill. We have an input
layer, consisting of three neurons, we have an output layer, consisting
of a single neuron, and we have weights on the edges between those
neurons.
</aside>
</section>
<!--
<section data-transition="fade-in">
<h2>Linear regression = <br/> Simplest neural network</h2>
<img src="images/latex_generated_images/linear_regression_as_neural_network_no_b.png"
style="width:30%"
alt = "Usually we omit the intercept from this diagram">
<aside class="notes">
As a side note about these graphical representations of neural
networks, the intercept is usually omitted, but that's just
to reduce visual clutter.
</aside>
</section> -->
</section> <!-- end linear regression section -->
<!-- Linear regression in TensorFlow -->
<section>
<section>
<h2>Once more, with TensorFlow</h2>
<aside class="notes">
Now that we know that linear regression is a neural
network in disguise, we can rewrite it in TensorFlow.
</aside>
</section>
<section data-transition="fade-out">
<h2></h2>
<ul>
<li>Inputs</li>
<li>Model - Parameters</li>
<li>Model - Operations</li>
<li>Cost function</li>
<li>Optimization</li>
<li>Train</li>
<li>Test</li>
</ul>
<aside class="notes">
What we're going to do is take those seven ingredients we
went through in numpy and recast them in TensorFlow.
</aside>
</section>
<section>
<h2>Inputs → Placeholders</h2>
<pre>
<code data-trim data-noescape class="python">
import tensorflow as tf
X = tf.placeholder(tf.float32, [None, 3])
Y = tf.placeholder(tf.float32, [None, 1])
</code>
</pre>
<aside class="notes">
Ingredient 1. Inputs. Already this looks very different.
Instead of supplying the data directly in numpy arrays,
we're going to have placeholders. The X placeholder says
I'll be a matrix of floats, and I'll have three columns.
The "None" here means I'm going to push through a variable
number of houses each time. You can specify a number here,
but then you'll be stuck with it. For flexibility, we'll just
say None. Similarly, the Y placeholder corresponds to
a single value, so it'll be a single column.
</aside>
</section>
<section>
<h2>Parameters → Variables</h2>
<pre>
<code data-trim data-noescape class="python">
# create tf.Variable(s)
W = tf.get_variable("weights", [3, 1],
initializer=tf.random_normal_initializer())
b = tf.get_variable("intercept", [1],
initializer=tf.constant_initializer(0))
</code>
</pre>
<aside class="notes">
Our parameters will be represented by TensorFlow
variables. We're mapping three neurons to one so the shape
of our weights will be three rows and one column. And
we can specify here how we want to initialize our
weights, which we'll sample from a random normal distribution.
The intercept we'll set to zero.
</aside>
</section>
<section>
<h2>Operations</h2>
<pre>
<code data-trim data-noescape class="python">
Y_hat = tf.matmul(X, W) + b
</code>
</pre>
<aside class="notes">
Our operation will be matrix multiplication and addition.
</aside>
</section>
<section>
<h2>Cost function</h2>
<pre>
<code data-trim data-noescape class="python">
cost = tf.reduce_mean(tf.square(Y_hat - Y))
</code>
</pre>
<aside class="notes">
And this will be our cost function. reduce_mean just means
mean.
</aside>
</section>
<section>
<h2>Optimization</h2>
<pre>
<code data-trim data-noescape class="python">
learning_rate = 0.05
optimizer = tf.train.GradientDescentOptimizer
(learning_rate).minimize(cost)
</code>
</pre>
<aside class="notes">
We'll specify that we're using gradient descent with
a certain learning rate, and the quantity we want to
minimize is cost.
</aside>
</section>
<section>
<h2>Training</h2>
<pre>
<code data-trim data-noescape class="python">
<mark>with tf.Session() as sess:</mark>
# initialize variables
sess.run(tf.global_variables_initializer())
# train
for _ in range(NUM_EPOCHS):
for (X_batch, Y_batch) in get_minibatches(
X_train, Y_train, BATCH_SIZE):
sess.run(optimizer,
feed_dict={
X: X_batch,
Y: Y_batch
})
</code>
</pre>
<aside class="notes">
Now for the training process. First of all, notice
that we're going to do this within a TensorFlow session.
In TensorFlow, nothing happens outside of a session.
It's only within a session that you can start writing
to the CPU or GPU, performing computations. You can't even
add two numbers in TensorFlow without going into a session.
</aside>
</section>
<section>
<h2>Training</h2>
<pre>
<code data-trim data-noescape class="python">
with tf.Session() as sess:
# initialize variables
<mark>sess.run(tf.global_variables_initializer())</mark>
# train
for _ in range(NUM_EPOCHS):
for (X_batch, Y_batch) in get_minibatches(
X_train, Y_train, BATCH_SIZE):
sess.run(optimizer,
feed_dict={
X: X_batch,
Y: Y_batch
})
</code>
</pre>
<aside class="notes">
Then we initialize the variables according to how
we defined them outside of the session.
</aside>
</section>
<section>
<h2>Training</h2>
<pre>
<code data-trim data-noescape class="python">
with tf.Session() as sess:
# initialize variables
sess.run(tf.global_variables_initializer())
# train
for _ in range(NUM_EPOCHS):
<mark>for (X_batch, Y_batch) in get_minibatches(</mark>
<mark>X_train, Y_train, BATCH_SIZE):</mark>
sess.run(optimizer,
feed_dict={
X: X_batch,
Y: Y_batch
})
</code>
</pre>
<aside class="notes">
Now we're going to feed through the actual data. X_train and Y_train
are the actual numpy arrays. We're also going to use minibatches,
where we random.shuffle the data and feed the data through batch
by batch.
</aside>
</section>
<section>
<h2>Training</h2>
<pre>
<code data-trim data-noescape class="python">
with tf.Session() as sess:
# initialize variables
sess.run(tf.global_variables_initializer())
# train
for _ in range(NUM_EPOCHS):
for (X_batch, Y_batch) in get_minibatches(
X_train, Y_train, BATCH_SIZE):
<mark>sess.run(optimizer,</mark>
<mark>feed_dict={</mark>
<mark>X: X_batch,</mark>
<mark>Y: Y_batch</mark>
<mark>})</mark>
</code>
</pre>
<aside class="notes">
And we pass the batches into the optimizer,
inserting them into their respective placeholders.
</aside>
</section>
<section data-transition="fade-out">
<div style="width: 60%; float: left;">
<pre style="font-size: 40%;">
<code data-trim data-noescape class="python">
# Placeholders
X = tf.placeholder(tf.float32, [None, 3])
Y = tf.placeholder(tf.float32, [None, 1])
# Parameters/Variables
W = tf.get_variable("weights", [3, 1],
initializer=tf.random_normal_initializer())
b = tf.get_variable("intercept", [1],
initializer=tf.constant_initializer(0))
# Operations
Y_hat = tf.matmul(X, W) + b
# Cost function
cost = tf.reduce_mean(tf.square(Y_hat - Y))
# Optimization
optimizer = tf.train.GradientDescentOptimizer
(learning_rate).minimize(cost)
# ------------------------------------------------
# Train
with tf.Session() as sess:
# initialize variables
sess.run(tf.global_variables_initializer())
# run training rounds
for _ in range(NUM_EPOCHS):
for X_batch, Y_batch in get_minibatches(
X_train, Y_train, BATCH_SIZE):
sess.run(optimizer,
feed_dict={X: X_batch, Y: Y_batch})
</code>
</pre>
</div>
<aside class="notes">
So here's all that code in one place. I want you
to notice a couple of things. First of all, remember
all that math we did to calculate the gradients? The
code that came out of that is gone.
</aside>
</section>