1
+ import h5py
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ import math
5
+
6
+ def load_dataset ():
7
+ train_dataset = h5py .File ('datasets/train_signs.h5' , "r" )
8
+ train_set_x_orig = np .array (train_dataset ["train_set_x" ][:]) # your train set features
9
+ train_set_y_orig = np .array (train_dataset ["train_set_y" ][:]) # your train set labels
10
+
11
+ test_dataset = h5py .File ('datasets/test_signs.h5' , "r" )
12
+ test_set_x_orig = np .array (test_dataset ["test_set_x" ][:]) # your test set features
13
+ test_set_y_orig = np .array (test_dataset ["test_set_y" ][:]) # your test set labels
14
+
15
+ classes = np .array (test_dataset ["list_classes" ][:]) # the list of classes
16
+
17
+ train_set_y_orig = train_set_y_orig .reshape ((1 , train_set_y_orig .shape [0 ]))
18
+ test_set_y_orig = test_set_y_orig .reshape ((1 , test_set_y_orig .shape [0 ]))
19
+
20
+ return train_set_x_orig , train_set_y_orig , test_set_x_orig , test_set_y_orig , classes
21
+
22
+
23
+ def random_mini_batches (X , Y , mini_batch_size = 64 , seed = 0 ):
24
+ """
25
+ Creates a list of random minibatches from (X, Y)
26
+
27
+ Arguments:
28
+ X -- input data, of shape (input size, number of examples)
29
+ Y -- true "label" vector (containing 0 if cat, 1 if non-cat), of shape (1, number of examples)
30
+ mini_batch_size - size of the mini-batches, integer
31
+ seed -- this is only for the purpose of grading, so that you're "random minibatches are the same as ours.
32
+
33
+ Returns:
34
+ mini_batches -- list of synchronous (mini_batch_X, mini_batch_Y)
35
+ """
36
+
37
+ m = X .shape [1 ] # number of training examples
38
+ mini_batches = []
39
+ np .random .seed (seed )
40
+
41
+ # Step 1: Shuffle (X, Y)
42
+ permutation = list (np .random .permutation (m ))
43
+ shuffled_X = X [:, permutation ]
44
+ shuffled_Y = Y [:, permutation ].reshape ((Y .shape [0 ],m ))
45
+
46
+ # Step 2: Partition (shuffled_X, shuffled_Y). Minus the end case.
47
+ num_complete_minibatches = math .floor (m / mini_batch_size ) # number of mini batches of size mini_batch_size in your partitionning
48
+ for k in range (0 , num_complete_minibatches ):
49
+ mini_batch_X = shuffled_X [:, k * mini_batch_size : k * mini_batch_size + mini_batch_size ]
50
+ mini_batch_Y = shuffled_Y [:, k * mini_batch_size : k * mini_batch_size + mini_batch_size ]
51
+ mini_batch = (mini_batch_X , mini_batch_Y )
52
+ mini_batches .append (mini_batch )
53
+
54
+ # Handling the end case (last mini-batch < mini_batch_size)
55
+ if m % mini_batch_size != 0 :
56
+ mini_batch_X = shuffled_X [:, num_complete_minibatches * mini_batch_size : m ]
57
+ mini_batch_Y = shuffled_Y [:, num_complete_minibatches * mini_batch_size : m ]
58
+ mini_batch = (mini_batch_X , mini_batch_Y )
59
+ mini_batches .append (mini_batch )
60
+
61
+ return mini_batches
62
+
63
+ def convert_to_one_hot (Y , C ):
64
+ Y = np .eye (C )[Y .reshape (- 1 )].T
65
+ return Y
66
+
67
+ def predict (X , parameters ):
68
+
69
+ W1 = tf .convert_to_tensor (parameters ["W1" ])
70
+ b1 = tf .convert_to_tensor (parameters ["b1" ])
71
+ W2 = tf .convert_to_tensor (parameters ["W2" ])
72
+ b2 = tf .convert_to_tensor (parameters ["b2" ])
73
+ W3 = tf .convert_to_tensor (parameters ["W3" ])
74
+ b3 = tf .convert_to_tensor (parameters ["b3" ])
75
+
76
+ params = {"W1" : W1 ,
77
+ "b1" : b1 ,
78
+ "W2" : W2 ,
79
+ "b2" : b2 ,
80
+ "W3" : W3 ,
81
+ "b3" : b3 }
82
+
83
+ x = tf .placeholder ("float" , [12288 , 1 ])
84
+
85
+ z3 = forward_propagation (x , params )
86
+ p = tf .argmax (z3 )
87
+
88
+ with tf .Session () as sess :
89
+ prediction = sess .run (p , feed_dict = {x : X })
90
+
91
+ return prediction
92
+
93
+
94
+ def create_placeholders (n_x , n_y ):
95
+ """
96
+ Creates the placeholders for the tensorflow session.
97
+
98
+ Arguments:
99
+ n_x -- scalar, size of an image vector (num_px * num_px = 64 * 64 * 3 = 12288)
100
+ n_y -- scalar, number of classes (from 0 to 5, so -> 6)
101
+
102
+ Returns:
103
+ X -- placeholder for the data input, of shape [n_x, None] and dtype "float"
104
+ Y -- placeholder for the input labels, of shape [n_y, None] and dtype "float"
105
+
106
+ Tips:
107
+ - You will use None because it let's us be flexible on the number of examples you will for the placeholders.
108
+ In fact, the number of examples during test/train is different.
109
+ """
110
+
111
+ ### START CODE HERE ### (approx. 2 lines)
112
+ X = tf .placeholder ("float" , [n_x , None ])
113
+ Y = tf .placeholder ("float" , [n_y , None ])
114
+ ### END CODE HERE ###
115
+
116
+ return X , Y
117
+
118
+
119
+ def initialize_parameters ():
120
+ """
121
+ Initializes parameters to build a neural network with tensorflow. The shapes are:
122
+ W1 : [25, 12288]
123
+ b1 : [25, 1]
124
+ W2 : [12, 25]
125
+ b2 : [12, 1]
126
+ W3 : [6, 12]
127
+ b3 : [6, 1]
128
+
129
+ Returns:
130
+ parameters -- a dictionary of tensors containing W1, b1, W2, b2, W3, b3
131
+ """
132
+
133
+ tf .set_random_seed (1 ) # so that your "random" numbers match ours
134
+
135
+ ### START CODE HERE ### (approx. 6 lines of code)
136
+ W1 = tf .get_variable ("W1" , [25 ,12288 ], initializer = tf .contrib .layers .xavier_initializer (seed = 1 ))
137
+ b1 = tf .get_variable ("b1" , [25 ,1 ], initializer = tf .zeros_initializer ())
138
+ W2 = tf .get_variable ("W2" , [12 ,25 ], initializer = tf .contrib .layers .xavier_initializer (seed = 1 ))
139
+ b2 = tf .get_variable ("b2" , [12 ,1 ], initializer = tf .zeros_initializer ())
140
+ W3 = tf .get_variable ("W3" , [6 ,12 ], initializer = tf .contrib .layers .xavier_initializer (seed = 1 ))
141
+ b3 = tf .get_variable ("b3" , [6 ,1 ], initializer = tf .zeros_initializer ())
142
+ ### END CODE HERE ###
143
+
144
+ parameters = {"W1" : W1 ,
145
+ "b1" : b1 ,
146
+ "W2" : W2 ,
147
+ "b2" : b2 ,
148
+ "W3" : W3 ,
149
+ "b3" : b3 }
150
+
151
+ return parameters
152
+
153
+
154
+ def compute_cost (z3 , Y ):
155
+ """
156
+ Computes the cost
157
+
158
+ Arguments:
159
+ z3 -- output of forward propagation (output of the last LINEAR unit), of shape (10, number of examples)
160
+ Y -- "true" labels vector placeholder, same shape as z3
161
+
162
+ Returns:
163
+ cost - Tensor of the cost function
164
+ """
165
+
166
+ # to fit the tensorflow requirement for tf.nn.softmax_cross_entropy_with_logits()
167
+ logits = tf .transpose (z3 )
168
+ labels = tf .transpose (Y )
169
+
170
+ ### START CODE HERE ### (1 line of code)
171
+ cost = tf .reduce_mean (tf .nn .softmax_cross_entropy_with_logits (logits = logits , labels = labels ))
172
+ ### END CODE HERE ###
173
+
174
+ return cost
175
+
176
+
177
+
178
+
179
+
180
+
181
+
182
+ def model (X_train , Y_train , X_test , Y_test , learning_rate = 0.0001 ,
183
+ num_epochs = 1500 , minibatch_size = 32 , print_cost = True ):
184
+ """
185
+ Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.
186
+
187
+ Arguments:
188
+ X_train -- training set, of shape (input size = 12288, number of training examples = 1080)
189
+ Y_train -- test set, of shape (output size = 6, number of training examples = 1080)
190
+ X_test -- training set, of shape (input size = 12288, number of training examples = 120)
191
+ Y_test -- test set, of shape (output size = 6, number of test examples = 120)
192
+ learning_rate -- learning rate of the optimization
193
+ num_epochs -- number of epochs of the optimization loop
194
+ minibatch_size -- size of a minibatch
195
+ print_cost -- True to print the cost every 100 epochs
196
+
197
+ Returns:
198
+ parameters -- parameters learnt by the model. They can then be used to predict.
199
+ """
200
+
201
+ ops .reset_default_graph () # to be able to rerun the model without overwriting tf variables
202
+ tf .set_random_seed (1 ) # to keep consistent results
203
+ seed = 3 # to keep consistent results
204
+ (n_x , m ) = X_train .shape # (n_x: input size, m : number of examples in the train set)
205
+ n_y = Y_train .shape [0 ] # n_y : output size
206
+ costs = [] # To keep track of the cost
207
+
208
+ # Create Placeholders of shape (n_x, n_y)
209
+ ### START CODE HERE ### (1 line)
210
+ X , Y = create_placeholders (n_x , n_y )
211
+ ### END CODE HERE ###
212
+
213
+ # Initialize parameters
214
+ ### START CODE HERE ### (1 line)
215
+ parameters = initialize_parameters ()
216
+ ### END CODE HERE ###
217
+
218
+ # Forward propagation: Build the forward propagation in the tensorflow graph
219
+ ### START CODE HERE ### (1 line)
220
+ z3 = forward_propagation (X , parameters )
221
+ ### END CODE HERE ###
222
+
223
+ # Cost function: Add cost function to tensorflow graph
224
+ ### START CODE HERE ### (1 line)
225
+ cost = compute_cost (z3 , Y )
226
+ ### END CODE HERE ###
227
+
228
+ # Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer.
229
+ ### START CODE HERE ### (1 line)
230
+ optimizer = tf .train .AdamOptimizer (learning_rate = learning_rate ).minimize (cost )
231
+ ### END CODE HERE ###
232
+
233
+ # Initialize all the variables
234
+ init = tf .global_variables_initializer ()
235
+
236
+ # Start the session to compute the tensorflow graph
237
+ with tf .Session () as sess :
238
+
239
+ # Run the initialization
240
+ sess .run (init )
241
+
242
+ # Do the training loop
243
+ for epoch in range (num_epochs ):
244
+
245
+ minibatch_cost = 0.
246
+ num_minibatches = int (m / minibatch_size ) # number of minibatches of size minibatch_size in the train set
247
+ seed = seed + 1
248
+ minibatches = random_mini_batches (X_train , Y_train , minibatch_size , seed )
249
+
250
+ for minibatch in minibatches :
251
+
252
+ # Select a minibatch
253
+ (minibatch_X , minibatch_Y ) = minibatch
254
+
255
+ # IMPORTANT: The line that runs the graph on a minibatch.
256
+ # Run the session to execute the optimizer and the cost, the feedict should contain a minibatch for (X,Y).
257
+ ### START CODE HERE ### (1 line)
258
+ _ , temp_cost = sess .run ([optimizer , cost ], feed_dict = {X : minibatch_X , Y : minibatch_Y })
259
+ ### END CODE HERE ###
260
+
261
+ minibatch_cost += temp_cost / num_minibatches
262
+
263
+ # Print the cost every epoch
264
+ if print_cost == True and epoch % 100 == 0 :
265
+ print ("Cost after epoch %i: %f" % (epoch , minibatch_cost ))
266
+ if print_cost == True and epoch % 5 == 0 :
267
+ costs .append (minibatch_cost )
268
+
269
+ # plot the cost
270
+ plt .plot (np .squeeze (costs ))
271
+ plt .ylabel ('cost' )
272
+ plt .xlabel ('iterations (per tens)' )
273
+ plt .title ("Learning rate =" + str (learning_rate ))
274
+ plt .show ()
275
+
276
+ # lets save the parameters in a variable
277
+ parameters = sess .run (parameters )
278
+ print ("Parameters have been trained!" )
279
+
280
+ # Calculate the correct predictions
281
+ correct_prediction = tf .equal (tf .argmax (z3 ), tf .argmax (Y ))
282
+
283
+ # Calculate accuracy on the test set
284
+ accuracy = tf .reduce_mean (tf .cast (correct_prediction , "float" ))
285
+
286
+ print ("Train Accuracy:" , accuracy .eval ({X : X_train , Y : Y_train }))
287
+ print ("Test Accuracy:" , accuracy .eval ({X : X_test , Y : Y_test }))
288
+
289
+ return parameters
0 commit comments