Created
April 2, 2019 15:37
-
-
Save marcopeix/96a8b95cfd338bd1cb2a1613ab3e7cbb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def model(X_train, Y_train, X_test, Y_test, learning_rate=0.009, | |
num_epochs=400, minibatch_size=64, print_cost=True): | |
ops.reset_default_graph() # to be able to rerun the model without overwriting tf variables | |
tf.set_random_seed(1) # to keep results consistent (tensorflow seed) | |
seed = 3 # to keep results consistent (numpy seed) | |
(m, n_H0, n_W0, n_C0) = X_train.shape | |
n_y = Y_train.shape[1] | |
costs = [] # To keep track of the cost | |
# Create Placeholders of the correct shape | |
X, Y = create_placeholders(n_H0, n_W0, n_C0, n_y) | |
# Initialize parameters | |
parameters = initialize_parameters() | |
# Forward propagation: Build the forward propagation in the tensorflow graph | |
Z3 = forward_propagation(X, parameters) | |
# Cost function: Add cost function to tensorflow graph | |
cost = compute_cost(Z3, Y) | |
# Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer that minimizes the cost. | |
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) | |
# Initialize all the variables globally | |
init = tf.global_variables_initializer() | |
# Start the session to compute the tensorflow graph | |
with tf.Session() as sess: | |
# Run the initialization | |
sess.run(init) | |
# Do the training loop | |
for epoch in range(num_epochs): | |
minibatch_cost = 0. | |
num_minibatches = int(m / minibatch_size) # number of minibatches of size minibatch_size in the train set | |
seed = seed + 1 | |
minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed) | |
for minibatch in minibatches: | |
# Select a minibatch | |
(minibatch_X, minibatch_Y) = minibatch | |
# Run the session to execute the optimizer and the cost, the feedict should contain a minibatch for (X,Y). | |
_ , temp_cost = sess.run([optimizer, cost], feed_dict={X:minibatch_X, Y:minibatch_Y}) | |
minibatch_cost += temp_cost / num_minibatches | |
# Print the cost every epoch | |
if print_cost == True and epoch % 100 == 0: | |
print ("Cost after epoch %i: %f" % (epoch, minibatch_cost)) | |
if print_cost == True and epoch % 1 == 0: | |
costs.append(minibatch_cost) | |
# plot the cost | |
plt.plot(np.squeeze(costs)) | |
plt.ylabel('cost') | |
plt.xlabel('iterations (per tens)') | |
plt.title("Learning rate =" + str(learning_rate)) | |
plt.show() | |
# Calculate the correct predictions | |
predict_op = tf.argmax(Z3, 1) | |
correct_prediction = tf.equal(predict_op, tf.argmax(Y, 1)) | |
# Calculate accuracy on the test set | |
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) | |
print(accuracy) | |
train_accuracy = accuracy.eval({X: X_train, Y: Y_train}) | |
test_accuracy = accuracy.eval({X: X_test, Y: Y_test}) | |
print("Train Accuracy:", train_accuracy) | |
print("Test Accuracy:", test_accuracy) | |
return train_accuracy, test_accuracy, parameters |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment