forked from rasbt/python-machine-learning-book-2nd-edition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch14.py
877 lines (575 loc) · 21.3 KB
/
ch14.py
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
# coding: utf-8
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
# *Python Machine Learning 2nd Edition* by [Sebastian Raschka](https://sebastianraschka.com) and Vahid Mirjalili, Packt Publishing Ltd. 2017
#
# Code Repository: https://github.com/rasbt/python-machine-learning-book-2nd-edition
#
# Code License: [MIT License](https://github.com/rasbt/python-machine-learning-book-2nd-edition/blob/master/LICENSE.txt)
# # Python Machine Learning - Code Examples
# Note that the optional watermark extension is a small IPython notebook plugin that I developed to make the code reproducible. You can just skip the following line(s).
# *The use of `watermark` is optional. You can install this IPython extension via "`pip install watermark`". For more information, please see: https://github.com/rasbt/watermark.*
# # Chapter 14 - Going Deeper: The Mechanics of TensorFlow
# - [TensorFlow in a nutshell](#TensorFlow-in-a-nutshell)
# - [Understanding TensorFlow's computation graphs](#Understanding-TensorFlow's-computation-graphs)
# - [Working with TensorFlow’s placeholders, variables, and operations](#Working-with-TensorFlow’s-placeholders,-variables,-and-operations)
# - [Using placeholders for feeding data as input to a model in TensorFlow](#Using-placeholders-for-feeding-data-as-input-to-a-model-in-TensorFlow)
# - [Defining placeholders](#Defining-placeholders)
# - [Feeding placeholders with data](#Feeding-placeholders-with-data)
# - [Defining placeholders for data arrays with varying batchsizes](#Defining-placeholders-for-data-arrays-with-varying-batchsizes)
# - [Variables in TensorFlow](#Variables-in-TensorFlow)
# - [Defining variables](#Defining-variables)
# - [Initializing variables](#Initializing-variables)
# - [Variable scope](#Variable-scope)
# - [Reusing variables](#Reusing-variables)
# - [Building a regression model](#Building-a-regression-model)
# - [Executing objects in a TensorFlow graph using their names](#Executing-objects-in-a-TensorFlow-graph-using-their-names)
# - [Saving and restoring a model in TensorFlow](#Saving-and-restoring-a-model-in-TensorFlow)
# - [Transforming Tensors as multidimensional data arrays](#Transforming-Tensors-as-multidimensional-data-arrays)
# - [Utilizing control flow mechanics in building graphs](#Utilizing-control-flow-mechanics-in-building-graphs)
# - [Visualizing the graph with TensorBoard](#Visualizing-the-graph-with-TensorBoard)
# - [Summary](#Summary)
# ## TensorFlow in a nutshell
# **How to get the rank and shape of a tensor**
g = tf.Graph()
## define the computation graph
with g.as_default():
## define tensors t1, t2, t3:
t1 = tf.constant(np.pi)
t2 = tf.constant([1, 2, 3, 4])
t3 = tf.constant([[1, 2], [3, 4]])
## get their ranks
r1 = tf.rank(t1)
r2 = tf.rank(t2)
r3 = tf.rank(t3)
## get their shapes
s1 = t1.get_shape()
s2 = t2.get_shape()
s3 = t3.get_shape()
print('Shapes:', s1, s2, s3)
with tf.Session(graph=g) as sess:
print('Ranks:',
r1.eval(),
r2.eval(),
r3.eval())
# ## Understanding TensorFlow's computation graph
Image("images/14_02.png")
g = tf.Graph()
## add nodes to the graph
with g.as_default():
a = tf.constant(1, name='a')
b = tf.constant(2, name='b')
c = tf.constant(3, name='c')
z = 2*(a-b) + c
## launch the graph
with tf.Session(graph=g) as sess:
print('2*(a-b)+c => ', sess.run(z))
# ## Working with TensorFlow’s placeholders, variables, and operations
# ### Using placeholders for feeding data as input to a model in TensorFlow
# #### Defining placeholders
g = tf.Graph()
with g.as_default():
tf_a = tf.placeholder(tf.int32, shape=[],
name='tf_a')
tf_b = tf.placeholder(tf.int32, shape=[],
name='tf_b')
tf_c = tf.placeholder(tf.int32, shape=[],
name='tf_c')
r1 = tf_a-tf_b
r2 = 2*r1
z = r2 + tf_c
# #### Feeding placeholders with data
## launch the previous graph
with tf.Session(graph=g) as sess:
feed = {tf_a: 1,
tf_b: 2,
tf_c: 3}
print('z:',
sess.run(z, feed_dict=feed))
# Execution with and without feeding tf_c:
## launch the previous graph
with tf.Session(graph=g) as sess:
## execution without feeding tf_c
feed = {tf_a: 1,
tf_b: 2}
print('r1:',
sess.run(r1, feed_dict=feed))
print('r2:',
sess.run(r2, feed_dict=feed))
## execution with feeding tf_c
feed = {tf_a: 1,
tf_b: 2,
tf_c: 3}
print('r1:',
sess.run(r1, feed_dict=feed))
print('r2:',
sess.run(r2, feed_dict=feed))
# ### Defining placeholders for data arrays with varying batchsizes
# Placeholder for varying batchsizes:
g = tf.Graph()
with g.as_default():
tf_x = tf.placeholder(tf.float32,
shape=[None, 2],
name='tf_x')
x_mean = tf.reduce_mean(tf_x,
axis=0,
name='mean')
np.random.seed(123)
np.set_printoptions(precision=2)
with tf.Session(graph=g) as sess:
x1 = np.random.uniform(low=0, high=1,
size=(5,2))
print('Feeding data with shape', x1.shape)
print('Result:', sess.run(x_mean,
feed_dict={tf_x:x1}))
x2 = np.random.uniform(low=0, high=1,
size=(10,2))
print('Feeding data with shape', x2.shape)
print('Result:', sess.run(x_mean,
feed_dict={tf_x:x2}))
print(tf_x)
# ### Variables in TensorFlow
#
# #### Defining Variables
g1 = tf.Graph()
with g1.as_default():
w = tf.Variable(np.array([[1, 2, 3, 4],
[5, 6, 7, 8]]), name='w')
print(w)
# #### Initializing variables
## initialize w and evaluate it
with tf.Session(graph=g1) as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(w))
## add the init_op to the graph
with g1.as_default():
init_op = tf.global_variables_initializer()
## initialize w with init_op and evaluate it
with tf.Session(graph=g1) as sess:
sess.run(init_op)
print(sess.run(w))
g2 = tf.Graph()
with g2.as_default():
w1 = tf.Variable(1, name='w1')
init_op = tf.global_variables_initializer()
w2 = tf.Variable(2, name='w2')
with tf.Session(graph=g2) as sess:
sess.run(init_op)
print('w1:', sess.run(w1))
# Error if a variable is not initialized:
with tf.Session(graph=g2) as sess:
try:
sess.run(init_op)
print('w2:', sess.run(w2))
except tf.errors.FailedPreconditionError as e:
print(e)
# #### Variable scope
g = tf.Graph()
with g.as_default():
with tf.variable_scope('net_A'):
with tf.variable_scope('layer-1'):
w1 = tf.Variable(tf.random_normal(
shape=(10,4)), name='weights')
with tf.variable_scope('layer-2'):
w2 = tf.Variable(tf.random_normal(
shape=(20,10)), name='weights')
with tf.variable_scope('net_B'):
with tf.variable_scope('layer-1'):
w3 = tf.Variable(tf.random_normal(
shape=(10,4)), name='weights')
print(w1)
print(w2)
print(w3)
# #### Reusing variables
######################
## Helper functions ##
######################
def build_classifier(data, labels, n_classes=2):
data_shape = data.get_shape().as_list()
weights = tf.get_variable(name='weights',
shape=(data_shape[1], n_classes),
dtype=tf.float32)
bias = tf.get_variable(name='bias',
initializer=tf.zeros(shape=n_classes))
print(weights)
print(bias)
logits = tf.add(tf.matmul(data, weights),
bias,
name='logits')
print(logits)
return logits, tf.nn.softmax(logits)
def build_generator(data, n_hidden):
data_shape = data.get_shape().as_list()
w1 = tf.Variable(
tf.random_normal(shape=(data_shape[1],
n_hidden)),
name='w1')
b1 = tf.Variable(tf.zeros(shape=n_hidden),
name='b1')
hidden = tf.add(tf.matmul(data, w1), b1,
name='hidden_pre-activation')
hidden = tf.nn.relu(hidden, 'hidden_activation')
w2 = tf.Variable(
tf.random_normal(shape=(n_hidden,
data_shape[1])),
name='w2')
b2 = tf.Variable(tf.zeros(shape=data_shape[1]),
name='b2')
output = tf.add(tf.matmul(hidden, w2), b2,
name = 'output')
return output, tf.nn.sigmoid(output)
########################
## Defining the graph ##
########################
batch_size=64
g = tf.Graph()
with g.as_default():
tf_X = tf.placeholder(shape=(batch_size, 100),
dtype=tf.float32,
name='tf_X')
## build the generator
with tf.variable_scope('generator'):
gen_out1 = build_generator(data=tf_X,
n_hidden=50)
## build the classifier
with tf.variable_scope('classifier') as scope:
## classifier for the original data:
cls_out1 = build_classifier(data=tf_X,
labels=tf.ones(
shape=batch_size))
## reuse the classifier for generated data
scope.reuse_variables()
cls_out2 = build_classifier(data=gen_out1[1],
labels=tf.zeros(
shape=batch_size))
init_op = tf.global_variables_initializer()
## alternative way
g = tf.Graph()
with g.as_default():
tf_X = tf.placeholder(shape=(batch_size, 100),
dtype=tf.float32,
name='tf_X')
## build the generator
with tf.variable_scope('generator'):
gen_out1 = build_generator(data=tf_X,
n_hidden=50)
## build the classifier
with tf.variable_scope('classifier'):
## classifier for the original data:
cls_out1 = build_classifier(data=tf_X,
labels=tf.ones(
shape=batch_size))
with tf.variable_scope('classifier', reuse=True):
## reuse the classifier for generated data
cls_out2 = build_classifier(data=gen_out1[1],
labels=tf.zeros(
shape=batch_size))
init_op = tf.global_variables_initializer()
# ### Building a regression model
## define a graph
g = tf.Graph()
## define the computation graph
with g.as_default():
## placeholders
tf.set_random_seed(123)
tf_x = tf.placeholder(shape=(None),
dtype=tf.float32,
name='tf_x')
tf_y = tf.placeholder(shape=(None),
dtype=tf.float32,
name='tf_y')
## define the variable (model parameters)
weight = tf.Variable(
tf.random_normal(
shape=(1, 1),
stddev=0.25),
name='weight')
bias = tf.Variable(0.0, name='bias')
## build the model
y_hat = tf.add(weight * tf_x, bias,
name='y_hat')
print(y_hat)
## compute the cost
cost = tf.reduce_mean(tf.square(tf_y - y_hat),
name='cost')
print(cost)
## train
optim = tf.train.GradientDescentOptimizer(
learning_rate=0.001)
train_op = optim.minimize(cost, name='train_op')
## create a random toy dataset for regression
np.random.seed(0)
def make_random_data():
x = np.random.uniform(low=-2, high=4, size=200)
y = []
for t in x:
r = np.random.normal(loc=0.0,
scale=(0.5 + t*t/3),
size=None)
y.append(r)
return x, 1.726*x -0.84 + np.array(y)
x, y = make_random_data()
plt.plot(x, y, 'o')
# plt.savefig('images/14_03.png', dpi=300)
plt.show()
# ### Executing objects in a TensorFlow graph using their names
## train/test splits:
x_train, y_train = x[:100], y[:100]
x_test, y_test = x[100:], y[100:]
## training the model
n_epochs = 500
training_costs = []
with tf.Session(graph=g) as sess:
## first, run the variables initializer
sess.run(tf.global_variables_initializer())
## train the model for n_epochs
for e in range(n_epochs):
c, _ = sess.run([cost, train_op],
feed_dict={tf_x: x_train,
tf_y: y_train})
training_costs.append(c)
if not e % 50:
print('Epoch %4d: %.4f' % (e, c))
plt.plot(training_costs)
# plt.savefig('images/14_04.png', dpi=300)
# Executing with variable names:
## train/test splits
x_train, y_train = x[:100], y[:100]
x_test, y_test = x[100:], y[100:]
## plot trainng data
plt.plot(x_train, y_train, 'o')
plt.show()
## training the model
n_epochs = 500
training_costs = []
with tf.Session(graph=g) as sess:
## first, run the variables initializer
sess.run(tf.global_variables_initializer())
## train the model for n_eopchs
for e in range(n_epochs):
c, _ = sess.run(['cost:0', 'train_op'],
feed_dict={'tf_x:0': x_train,
'tf_y:0': y_train})
training_costs.append(c)
if not e % 50:
print('Epoch %4d: %.4f' % (e, c))
# ## Saving and restoring a model in TensorFlow
## add saver to the graph
with g.as_default():
saver = tf.train.Saver()
## training the model
n_epochs = 500
training_costs = []
with tf.Session(graph=g) as sess:
## first, run the variables initializer
sess.run(tf.global_variables_initializer())
## train the model for n_epochs
for e in range(n_epochs):
c, _ = sess.run(['cost:0', 'train_op'],
feed_dict={'tf_x:0':x_train,
'tf_y:0':y_train})
training_costs.append(c)
if not e % 50:
print('Epoch %4d: %.4f' % (e, c))
saver.save(sess, './trained-model')
# Restoring the saved model:
## new file: loading a trained model
## and run the model on test set
g2 = tf.Graph()
with tf.Session(graph=g2) as sess:
new_saver = tf.train.import_meta_graph(
'./trained-model.meta')
new_saver.restore(sess, './trained-model')
y_pred = sess.run('y_hat:0',
feed_dict={'tf_x:0' : x_test})
print('SSE: %.4f' % (np.sum(np.square(y_pred - y_test))))
x_arr = np.arange(-2, 4, 0.1)
g2 = tf.Graph()
with tf.Session(graph=g2) as sess:
new_saver = tf.train.import_meta_graph(
'./trained-model.meta')
new_saver.restore(sess, './trained-model')
y_arr = sess.run('y_hat:0',
feed_dict={'tf_x:0' : x_arr})
plt.figure()
plt.plot(x_train, y_train, 'bo')
plt.plot(x_test, y_test, 'bo', alpha=0.3)
plt.plot(x_arr, y_arr.T[:, 0], '-r', lw=3)
# plt.savefig('images/14_05.png', dpi=400)
plt.show()
# ## Transforming Tensors as multidimensional data arrays
g = tf.Graph()
with g.as_default():
arr = np.array([[1., 2., 3., 3.5],
[4., 5., 6., 6.5],
[7., 8., 9., 9.5]])
T1 = tf.constant(arr, name='T1')
print(T1)
s = T1.get_shape()
print('Shape of T1 is', s)
T2 = tf.Variable(tf.random_normal(
shape=s))
print(T2)
T3 = tf.Variable(tf.random_normal(
shape=(s.as_list()[0],)))
print(T3)
with g.as_default():
T4 = tf.reshape(T1, shape=[1, 1, -1],
name='T4')
print(T4)
T5 = tf.reshape(T1, shape=[1, 3, -1],
name='T5')
print(T5)
with tf.Session(graph = g) as sess:
print(sess.run(T4))
print()
print(sess.run(T5))
with g.as_default():
T6 = tf.transpose(T5, perm=[2, 1, 0],
name='T6')
print(T6)
T7 = tf.transpose(T5, perm=[0, 2, 1],
name='T7')
print(T7)
with g.as_default():
t5_splt = tf.split(T5,
num_or_size_splits=2,
axis=2, name='T8')
print(t5_splt)
g = tf.Graph()
with g.as_default():
t1 = tf.ones(shape=(5, 1),
dtype=tf.float32, name='t1')
t2 = tf.zeros(shape=(5, 1),
dtype=tf.float32, name='t2')
print(t1)
print(t2)
with g.as_default():
t3 = tf.concat([t1, t2], axis=0, name='t3')
print(t3)
t4 = tf.concat([t1, t2], axis=1, name='t4')
print(t4)
with tf.Session(graph = g) as sess:
print(t3.eval())
print()
print(t4.eval())
# ## Utilizing control flow mechanics in building graphs
## Python control flow
x, y = 1.0, 2.0
g = tf.Graph()
with g.as_default():
tf_x = tf.placeholder(dtype=tf.float32,
shape=None, name='tf_x')
tf_y = tf.placeholder(dtype=tf.float32,
shape=None, name='tf_y')
if x < y:
res = tf.add(tf_x, tf_y, name='result_add')
else:
res = tf.subtract(tf_x, tf_y, name='result_sub')
print('Object: ', res)
with tf.Session(graph=g) as sess:
print('x < y: %s -> Result:' % (x < y),
res.eval(feed_dict={'tf_x:0': x,
'tf_y:0': y}))
x, y = 2.0, 1.0
print('x < y: %s -> Result:' % (x < y),
res.eval(feed_dict={'tf_x:0': x,
'tf_y:0': y}))
## uncomment the next line if you want to visualize the graph in TensorBoard:
file_writer = tf.summary.FileWriter(logdir='./logs/py-cflow/', graph=g)
## TensorFlow control flow
x, y = 1.0, 2.0
g = tf.Graph()
with g.as_default():
tf_x = tf.placeholder(dtype=tf.float32,
shape=None, name='tf_x')
tf_y = tf.placeholder(dtype=tf.float32,
shape=None, name='tf_y')
res = tf.cond(tf_x < tf_y,
lambda: tf.add(tf_x, tf_y,
name='result_add'),
lambda: tf.subtract(tf_x, tf_y,
name='result_sub'))
print('Object:', res)
with tf.Session(graph=g) as sess:
print('x < y: %s -> Result:' % (x < y),
res.eval(feed_dict={'tf_x:0': x,
'tf_y:0': y}))
x, y = 2.0, 1.0
print('x < y: %s -> Result:' % (x < y),
res.eval(feed_dict={'tf_x:0': x,
'tf_y:0': y}))
#file_writer = tf.summary.FileWriter(logdir='./logs/tf-cond/', graph=g)
# ## Visualizing the graph with TensorBoard
###########################
## Helper functions ##
###########################
def build_classifier(data, labels, n_classes=2):
data_shape = data.get_shape().as_list()
weights = tf.get_variable(name = 'weights',
shape=(data_shape[1],
n_classes),
dtype=tf.float32)
bias = tf.get_variable(name='bias',
initializer=tf.zeros(
shape=n_classes))
print(weights)
print(bias)
logits = tf.add(tf.matmul(data, weights),
bias,
name='logits')
print(logits)
return logits, tf.nn.softmax(logits)
def build_generator(data, n_hidden):
data_shape = data.get_shape().as_list()
w1 = tf.Variable(
tf.random_normal(shape=(data_shape[1],
n_hidden)),
name='w1')
b1 = tf.Variable(tf.zeros(shape=n_hidden),
name='b1')
hidden = tf.add(tf.matmul(data, w1), b1,
name='hidden_pre-activation')
hidden = tf.nn.relu(hidden, 'hidden_activation')
w2 = tf.Variable(
tf.random_normal(shape=(n_hidden,
data_shape[1])),
name='w2')
b2 = tf.Variable(tf.zeros(shape=data_shape[1]),
name='b2')
output = tf.add(tf.matmul(hidden, w2), b2,
name = 'output')
return output, tf.nn.sigmoid(output)
###########################
## Building the graph ##
###########################
batch_size=64
g = tf.Graph()
with g.as_default():
tf_X = tf.placeholder(shape=(batch_size, 100),
dtype=tf.float32,
name='tf_X')
## build the generator
with tf.variable_scope('generator'):
gen_out1 = build_generator(data=tf_X,
n_hidden=50)
## build the classifier
with tf.variable_scope('classifier') as scope:
## classifier for the original data:
cls_out1 = build_classifier(data=tf_X,
labels=tf.ones(
shape=batch_size))
## reuse the classifier for generated data
scope.reuse_variables()
cls_out2 = build_classifier(data=gen_out1[1],
labels=tf.zeros(
shape=batch_size))
with tf.Session(graph = g) as sess:
sess.run(tf.global_variables_initializer())
file_writer = tf.summary.FileWriter(logdir='logs/', graph=g)
# ## Summary
# ...
# ---
#
# Readers may ignore the next cell.