1
+ from __future__ import print_function , division
2
+ from builtins import range
3
+ # Note: you may need to update your version of future
4
+ # sudo pip install -U future
5
+
1
6
# For the class Data Science: Practical Deep Learning Concepts in Theano and TensorFlow
2
7
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
3
8
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
@@ -13,7 +18,7 @@ class HiddenLayer(object):
13
18
def __init__ (self , M1 , M2 ):
14
19
self .M1 = M1
15
20
self .M2 = M2
16
- W = np .random .randn (M1 , M2 ) / np .sqrt (2.0 / M1 )
21
+ W = np .random .randn (M1 , M2 ) * np .sqrt (2.0 / M1 )
17
22
b = np .zeros (M2 )
18
23
self .W = tf .Variable (W .astype (np .float32 ))
19
24
self .b = tf .Variable (b .astype (np .float32 ))
@@ -28,7 +33,7 @@ def __init__(self, hidden_layer_sizes, p_keep):
28
33
self .hidden_layer_sizes = hidden_layer_sizes
29
34
self .dropout_rates = p_keep
30
35
31
- def fit (self , X , Y , lr = 1e-4 , mu = 0.9 , decay = 0.9 , epochs = 8 , batch_sz = 100 , split = True , print_every = 20 ):
36
+ def fit (self , X , Y , lr = 1e-4 , mu = 0.9 , decay = 0.9 , epochs = 15 , batch_sz = 100 , split = True , print_every = 20 ):
32
37
# make a validation set
33
38
X , Y = shuffle (X , Y )
34
39
X = X .astype (np .float32 )
@@ -48,7 +53,7 @@ def fit(self, X, Y, lr=1e-4, mu=0.9, decay=0.9, epochs=8, batch_sz=100, split=Tr
48
53
h = HiddenLayer (M1 , M2 )
49
54
self .hidden_layers .append (h )
50
55
M1 = M2
51
- W = np .random .randn (M1 , K ) / np .sqrt (M1 )
56
+ W = np .random .randn (M1 , K ) * np .sqrt (2.0 / M1 )
52
57
b = np .zeros (K )
53
58
self .W = tf .Variable (W .astype (np .float32 ))
54
59
self .b = tf .Variable (b .astype (np .float32 ))
@@ -71,44 +76,61 @@ def fit(self, X, Y, lr=1e-4, mu=0.9, decay=0.9, epochs=8, batch_sz=100, split=Tr
71
76
)
72
77
train_op = tf .train .RMSPropOptimizer (lr , decay = decay , momentum = mu ).minimize (cost )
73
78
# train_op = tf.train.MomentumOptimizer(lr, momentum=mu).minimize(cost)
79
+ # train_op = tf.train.AdamOptimizer(lr).minimize(cost)
74
80
prediction = self .predict (inputs )
75
81
76
- n_batches = N / batch_sz
82
+ # validation cost will be calculated separately since nothing will be dropped
83
+ test_logits = self .forward_test (inputs )
84
+ test_cost = tf .reduce_mean (
85
+ tf .nn .sparse_softmax_cross_entropy_with_logits (
86
+ logits = test_logits ,
87
+ labels = labels
88
+ )
89
+ )
90
+
91
+ n_batches = N // batch_sz
77
92
costs = []
78
93
init = tf .global_variables_initializer ()
79
94
with tf .Session () as session :
80
95
session .run (init )
81
- for i in xrange (epochs ):
82
- print "epoch:" , i , "n_batches:" , n_batches
96
+ for i in range (epochs ):
97
+ print ( "epoch:" , i , "n_batches:" , n_batches )
83
98
X , Y = shuffle (X , Y )
84
- for j in xrange (n_batches ):
99
+ for j in range (n_batches ):
85
100
Xbatch = X [j * batch_sz :(j * batch_sz + batch_sz )]
86
101
Ybatch = Y [j * batch_sz :(j * batch_sz + batch_sz )]
87
102
88
103
session .run (train_op , feed_dict = {inputs : Xbatch , labels : Ybatch })
89
104
90
105
if j % print_every == 0 :
91
- c = session .run (cost , feed_dict = {inputs : Xvalid , labels : Yvalid })
106
+ c = session .run (test_cost , feed_dict = {inputs : Xvalid , labels : Yvalid })
92
107
p = session .run (prediction , feed_dict = {inputs : Xvalid })
93
108
costs .append (c )
94
109
e = error_rate (Yvalid , p )
95
- print "i:" , i , "j:" , j , "nb:" , n_batches , "cost:" , c , "error rate:" , e
110
+ print ( "i:" , i , "j:" , j , "nb:" , n_batches , "cost:" , c , "error rate:" , e )
96
111
97
112
plt .plot (costs )
98
113
plt .show ()
99
114
100
115
def forward (self , X ):
101
- # no need to define different functions for train and predict
102
- # tf.nn.dropout takes care of the differences for us
116
+ # tf.nn.dropout scales inputs by 1/p_keep
117
+ # therefore, during test time, we don't have to scale anything
103
118
Z = X
104
119
Z = tf .nn .dropout (Z , self .dropout_rates [0 ])
105
120
for h , p in zip (self .hidden_layers , self .dropout_rates [1 :]):
106
121
Z = h .forward (Z )
107
122
Z = tf .nn .dropout (Z , p )
108
123
return tf .matmul (Z , self .W ) + self .b
109
124
125
+ def forward_test (self , X ):
126
+ Z = X
127
+ Z = tf .nn .dropout (Z , self .dropout_rates [0 ])
128
+ for h , p in zip (self .hidden_layers , self .dropout_rates [1 :]):
129
+ Z = h .forward (Z )
130
+ return tf .matmul (Z , self .W ) + self .b
131
+
110
132
def predict (self , X ):
111
- pY = self .forward (X )
133
+ pY = self .forward_test (X )
112
134
return tf .argmax (pY , 1 )
113
135
114
136
0 commit comments