Programming Neural Networks¶
This notebook contains the code for my blog on Programming Neural Networks
In [1]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
import os
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
import os
%matplotlib inline
In [2]:
Copied!
def load_dataset():
train_dataset = h5py.File('/content/drive/MyDrive/Temporary/train_catvnoncat.h5', "r")
train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels
test_dataset = h5py.File('/content/drive/MyDrive/Temporary/test_catvnoncat.h5', "r")
test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels
classes = np.array(test_dataset["list_classes"][:]) # the list of classes
train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes
load_dataset()
def load_dataset():
train_dataset = h5py.File('/content/drive/MyDrive/Temporary/train_catvnoncat.h5', "r")
train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels
test_dataset = h5py.File('/content/drive/MyDrive/Temporary/test_catvnoncat.h5', "r")
test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels
classes = np.array(test_dataset["list_classes"][:]) # the list of classes
train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes
load_dataset()
Out[2]:
(array([[[[ 17, 31, 56], [ 22, 33, 59], [ 25, 35, 62], ..., [ 1, 28, 57], [ 1, 26, 56], [ 1, 22, 51]], [[ 25, 36, 62], [ 28, 38, 64], [ 30, 40, 67], ..., [ 1, 27, 56], [ 1, 25, 55], [ 2, 21, 51]], [[ 32, 40, 67], [ 34, 42, 69], [ 35, 42, 70], ..., [ 1, 25, 55], [ 0, 24, 54], [ 1, 21, 51]], ..., [[ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0], ..., [ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0]], [[ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0], ..., [ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0]], [[ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0], ..., [ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0]]], [[[196, 192, 190], [193, 186, 182], [188, 179, 174], ..., [ 90, 142, 200], [ 90, 142, 201], [ 90, 142, 201]], [[230, 229, 229], [204, 199, 197], [193, 186, 181], ..., [ 91, 143, 201], [ 91, 143, 201], [ 91, 143, 201]], [[232, 225, 224], [235, 234, 234], [208, 205, 202], ..., [ 91, 144, 202], [ 91, 144, 202], [ 92, 144, 202]], ..., [[ 18, 17, 15], [ 14, 14, 13], [ 29, 29, 32], ..., [ 83, 81, 81], [ 84, 82, 83], [ 82, 81, 82]], [[ 22, 20, 18], [ 16, 15, 14], [ 25, 24, 24], ..., [ 82, 80, 80], [ 83, 81, 82], [ 82, 81, 81]], [[ 45, 43, 39], [ 61, 59, 54], [ 81, 78, 74], ..., [ 83, 82, 81], [ 84, 82, 82], [ 82, 80, 81]]], [[[ 82, 71, 68], [ 89, 83, 83], [100, 98, 104], ..., [131, 132, 137], [126, 124, 124], [105, 97, 95]], [[ 95, 91, 97], [104, 104, 113], [110, 115, 126], ..., [135, 134, 135], [127, 122, 119], [111, 105, 103]], [[ 94, 85, 83], [ 97, 89, 90], [110, 109, 115], ..., [136, 134, 131], [127, 120, 117], [116, 108, 104]], ..., [[ 96, 116, 131], [ 97, 115, 130], [103, 123, 139], ..., [152, 155, 157], [146, 149, 152], [130, 133, 134]], [[ 90, 108, 123], [ 92, 108, 121], [100, 119, 134], ..., [150, 152, 155], [144, 146, 147], [134, 135, 134]], [[ 86, 102, 116], [ 87, 103, 115], [ 94, 114, 127], ..., [154, 156, 160], [146, 148, 152], [138, 141, 142]]], ..., [[[143, 155, 165], [184, 190, 198], [142, 149, 155], ..., [ 99, 92, 102], [120, 98, 102], [100, 84, 95]], [[151, 149, 139], [173, 179, 185], [105, 135, 141], ..., [ 91, 87, 99], [119, 99, 104], [120, 95, 101]], [[204, 190, 185], [180, 185, 195], [117, 155, 177], ..., [ 96, 88, 101], [125, 103, 110], [120, 100, 110]], ..., [[ 41, 80, 116], [ 41, 80, 116], [ 41, 78, 115], ..., [ 63, 75, 98], [ 60, 72, 98], [ 60, 70, 96]], [[ 71, 90, 121], [ 73, 91, 123], [ 74, 91, 124], ..., [ 79, 101, 142], [ 80, 100, 140], [ 82, 101, 139]], [[ 71, 88, 122], [ 73, 92, 128], [ 76, 95, 131], ..., [ 81, 106, 150], [ 85, 108, 151], [ 85, 107, 149]]], [[[ 22, 24, 23], [ 23, 25, 24], [ 24, 26, 25], ..., [ 24, 29, 25], [ 23, 25, 22], [ 20, 22, 21]], [[ 22, 24, 23], [ 23, 25, 24], [ 23, 26, 25], ..., [ 22, 28, 23], [ 20, 23, 22], [ 19, 21, 21]], [[ 22, 24, 22], [ 23, 25, 24], [ 23, 26, 25], ..., [ 23, 27, 23], [ 20, 23, 21], [ 18, 20, 19]], ..., [[ 8, 5, 0], [ 9, 6, 1], [ 9, 6, 1], ..., [ 4, 5, 0], [ 5, 4, 0], [ 4, 5, 0]], [[ 7, 5, 0], [ 8, 5, 1], [ 9, 6, 1], ..., [ 4, 5, 0], [ 4, 5, 0], [ 4, 5, 0]], [[ 7, 5, 0], [ 8, 5, 0], [ 9, 6, 1], ..., [ 4, 5, 0], [ 4, 5, 0], [ 4, 5, 0]]], [[[ 8, 28, 53], [ 14, 33, 58], [ 19, 35, 61], ..., [ 11, 16, 35], [ 10, 16, 35], [ 9, 14, 32]], [[ 15, 31, 57], [ 15, 32, 58], [ 18, 34, 60], ..., [ 13, 17, 35], [ 13, 17, 35], [ 13, 16, 35]], [[ 20, 35, 61], [ 19, 33, 59], [ 20, 33, 59], ..., [ 16, 17, 35], [ 16, 18, 35], [ 15, 17, 35]], ..., [[ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0], ..., [ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0]], [[ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0], ..., [ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0]], [[ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0], ..., [ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0]]]], dtype=uint8), array([[0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]]), array([[[[158, 104, 83], [161, 106, 85], [162, 107, 84], ..., [170, 120, 100], [167, 121, 103], [172, 127, 109]], [[158, 103, 82], [160, 104, 82], [162, 105, 83], ..., [169, 118, 99], [164, 117, 98], [168, 121, 104]], [[158, 104, 82], [161, 105, 82], [162, 105, 83], ..., [173, 123, 102], [169, 122, 104], [168, 122, 104]], ..., [[102, 68, 52], [ 44, 31, 23], [ 28, 23, 20], ..., [163, 118, 102], [163, 120, 103], [166, 121, 105]], [[ 99, 67, 51], [ 38, 28, 22], [ 30, 26, 23], ..., [161, 117, 100], [164, 121, 104], [168, 123, 106]], [[127, 95, 72], [ 39, 29, 22], [ 30, 25, 22], ..., [165, 122, 105], [169, 126, 109], [173, 128, 110]]], [[[115, 110, 111], [137, 129, 129], [155, 146, 145], ..., [159, 156, 157], [141, 141, 145], [121, 122, 127]], [[123, 118, 120], [143, 136, 136], [159, 153, 150], ..., [167, 164, 165], [151, 151, 154], [130, 133, 137]], [[135, 130, 130], [150, 145, 141], [164, 159, 153], ..., [173, 174, 172], [160, 162, 162], [141, 144, 148]], ..., [[197, 196, 205], [208, 209, 214], [215, 216, 220], ..., [222, 224, 229], [205, 208, 215], [185, 189, 197]], [[190, 192, 199], [203, 205, 210], [213, 214, 218], ..., [217, 220, 225], [198, 202, 209], [178, 182, 191]], [[183, 186, 193], [197, 199, 205], [208, 210, 214], ..., [212, 215, 220], [192, 196, 203], [171, 176, 186]]], [[[255, 253, 254], [255, 253, 254], [255, 253, 254], ..., [197, 178, 118], [195, 177, 116], [192, 176, 115]], [[255, 253, 254], [255, 253, 254], [255, 253, 254], ..., [197, 178, 120], [195, 176, 118], [193, 174, 118]], [[255, 253, 254], [255, 253, 254], [255, 253, 254], ..., [197, 177, 121], [194, 174, 121], [190, 171, 122]], ..., [[ 94, 95, 97], [102, 104, 108], [115, 113, 115], ..., [ 68, 67, 91], [ 71, 55, 80], [ 64, 51, 82]], [[ 68, 71, 69], [ 75, 79, 80], [ 82, 91, 91], ..., [122, 103, 109], [114, 84, 99], [ 97, 72, 100]], [[ 67, 86, 89], [ 62, 83, 90], [ 47, 73, 82], ..., [147, 120, 125], [142, 110, 118], [133, 101, 121]]], ..., [[[ 41, 47, 84], [ 72, 78, 112], [103, 102, 124], ..., [125, 112, 119], [107, 101, 117], [ 76, 79, 109]], [[ 93, 96, 119], [112, 109, 124], [130, 122, 136], ..., [141, 121, 118], [130, 112, 114], [122, 109, 117]], [[119, 114, 123], [133, 124, 128], [164, 149, 145], ..., [187, 160, 135], [157, 132, 120], [136, 115, 109]], ..., [[233, 223, 207], [235, 226, 218], [242, 236, 235], ..., [164, 170, 196], [189, 185, 196], [190, 182, 188]], [[247, 250, 248], [236, 238, 232], [219, 215, 219], ..., [118, 101, 117], [153, 134, 154], [174, 146, 156]], [[253, 254, 254], [246, 250, 249], [226, 233, 234], ..., [154, 111, 93], [176, 132, 104], [183, 141, 116]]], [[[ 18, 18, 16], [ 35, 36, 31], [ 54, 57, 52], ..., [138, 140, 118], [139, 141, 124], [130, 129, 114]], [[ 21, 21, 18], [ 44, 45, 39], [ 68, 74, 66], ..., [128, 131, 109], [116, 117, 98], [ 95, 93, 76]], [[ 30, 31, 26], [ 54, 58, 52], [ 99, 110, 104], ..., [127, 129, 108], [113, 114, 94], [ 99, 99, 82]], ..., [[178, 178, 163], [161, 163, 146], [170, 174, 163], ..., [112, 108, 85], [149, 150, 131], [180, 185, 172]], [[182, 183, 167], [172, 175, 162], [170, 173, 162], ..., [ 88, 90, 77], [122, 115, 89], [157, 154, 131]], [[188, 190, 179], [173, 175, 163], [160, 162, 146], ..., [ 48, 50, 44], [103, 103, 89], [144, 137, 108]]], [[[133, 163, 75], [ 98, 120, 44], [108, 132, 47], ..., [ 96, 138, 97], [ 96, 146, 109], [ 81, 132, 101]], [[ 79, 102, 39], [ 83, 112, 33], [ 76, 106, 26], ..., [ 99, 149, 76], [ 71, 119, 62], [ 58, 106, 72]], [[ 35, 53, 27], [ 54, 74, 46], [ 35, 55, 17], ..., [110, 157, 122], [ 72, 119, 89], [ 81, 131, 108]], ..., [[ 76, 108, 79], [ 70, 110, 83], [ 70, 105, 84], ..., [ 25, 49, 21], [ 26, 53, 24], [ 27, 54, 28]], [[109, 120, 97], [ 86, 116, 86], [ 71, 105, 79], ..., [ 17, 46, 20], [ 23, 49, 28], [ 19, 33, 18]], [[ 97, 117, 100], [ 70, 108, 75], [ 75, 99, 69], ..., [ 8, 33, 12], [ 13, 35, 18], [ 5, 22, 5]]]], dtype=uint8), array([[1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0]]), array([b'non-cat', b'cat'], dtype='|S7'))
In [3]:
Copied!
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
# Example of a picture
index = 19
plt.imshow(train_set_x_orig[index])
print ("y = " + str(train_set_y[:,index]) + ", it's a '" + classes[np.squeeze(train_set_y[:,index])].decode("utf-8") + "' picture.")
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
# Example of a picture
index = 19
plt.imshow(train_set_x_orig[index])
print ("y = " + str(train_set_y[:,index]) + ", it's a '" + classes[np.squeeze(train_set_y[:,index])].decode("utf-8") + "' picture.")
y = [1], it's a 'cat' picture.
In [4]:
Copied!
# Reshape the training and test examples
train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T
print ("train_set_x_flatten shape: " + str(train_set_x_flatten.shape))
print ("train_set_y shape: " + str(train_set_y.shape))
print ("test_set_x_flatten shape: " + str(test_set_x_flatten.shape))
print ("test_set_y shape: " + str(test_set_y.shape))
print ("sanity check after reshaping: " + str(train_set_x_flatten[0:5,0]))
# Reshape the training and test examples
train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T
print ("train_set_x_flatten shape: " + str(train_set_x_flatten.shape))
print ("train_set_y shape: " + str(train_set_y.shape))
print ("test_set_x_flatten shape: " + str(test_set_x_flatten.shape))
print ("test_set_y shape: " + str(test_set_y.shape))
print ("sanity check after reshaping: " + str(train_set_x_flatten[0:5,0]))
train_set_x_flatten shape: (12288, 209) train_set_y shape: (1, 209) test_set_x_flatten shape: (12288, 50) test_set_y shape: (1, 50) sanity check after reshaping: [17 31 56 22 33]
In [5]:
Copied!
train_set_x = train_set_x_flatten / 255.
test_set_x = test_set_x_flatten / 255.
train_set_x = train_set_x_flatten / 255.
test_set_x = test_set_x_flatten / 255.
In [6]:
Copied!
# SIGMOID FUNCTION
def sigmoid(z):
s = 1 / (1 + np.exp(-z))
return s
# SIGMOID FUNCTION
def sigmoid(z):
s = 1 / (1 + np.exp(-z))
return s
In [7]:
Copied!
# initialize_with_zeros
def initialize_with_zeros(dim):
w = np.zeros(shape=(dim, 1))
b = 0
assert(w.shape == (dim, 1))
assert(isinstance(b, float) or isinstance(b, int))
return w, b
# initialize_with_zeros
def initialize_with_zeros(dim):
w = np.zeros(shape=(dim, 1))
b = 0
assert(w.shape == (dim, 1))
assert(isinstance(b, float) or isinstance(b, int))
return w, b
In [8]:
Copied!
# GRADED FUNCTION: propagate
def propagate(w, b, X, Y):
m = X.shape[1]
# FORWARD PROPAGATION (FROM X TO COST)
A = sigmoid(np.dot(w.T, X) + b) # compute activation
cost = (- 1 / m) * np.sum(Y * np.log(A) + (1 - Y) * (np.log(1 - A))) # compute cost
# BACKWARD PROPAGATION (TO FIND GRAD)
dw = (1 / m) * np.dot(X, (A - Y).T)
db = (1 / m) * np.sum(A - Y)
assert(dw.shape == w.shape)
assert(db.dtype == float)
cost = np.squeeze(cost)
assert(cost.shape == ())
grads = {"dw": dw,
"db": db}
return grads, cost
# GRADED FUNCTION: propagate
def propagate(w, b, X, Y):
m = X.shape[1]
# FORWARD PROPAGATION (FROM X TO COST)
A = sigmoid(np.dot(w.T, X) + b) # compute activation
cost = (- 1 / m) * np.sum(Y * np.log(A) + (1 - Y) * (np.log(1 - A))) # compute cost
# BACKWARD PROPAGATION (TO FIND GRAD)
dw = (1 / m) * np.dot(X, (A - Y).T)
db = (1 / m) * np.sum(A - Y)
assert(dw.shape == w.shape)
assert(db.dtype == float)
cost = np.squeeze(cost)
assert(cost.shape == ())
grads = {"dw": dw,
"db": db}
return grads, cost
In [9]:
Copied!
# GRADED FUNCTION: optimize
def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False):
costs = []
for i in range(num_iterations):
# Cost and gradient calculation (≈ 1-4 lines of code)
grads, cost = propagate(w, b, X, Y)
# Retrieve derivatives from grads
dw = grads["dw"]
db = grads["db"]
# update rule (≈ 2 lines of code)
w = w - learning_rate * dw # need to broadcast
b = b - learning_rate * db
# Record the costs
if i % 100 == 0:
costs.append(cost)
# Print the cost every 100 training examples
if print_cost and i % 100 == 0:
print ("Cost after iteration %i: %f" % (i, cost))
params = {"w": w,
"b": b}
grads = {"dw": dw,
"db": db}
return params, grads, costs
# GRADED FUNCTION: optimize
def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False):
costs = []
for i in range(num_iterations):
# Cost and gradient calculation (≈ 1-4 lines of code)
grads, cost = propagate(w, b, X, Y)
# Retrieve derivatives from grads
dw = grads["dw"]
db = grads["db"]
# update rule (≈ 2 lines of code)
w = w - learning_rate * dw # need to broadcast
b = b - learning_rate * db
# Record the costs
if i % 100 == 0:
costs.append(cost)
# Print the cost every 100 training examples
if print_cost and i % 100 == 0:
print ("Cost after iteration %i: %f" % (i, cost))
params = {"w": w,
"b": b}
grads = {"dw": dw,
"db": db}
return params, grads, costs
In [10]:
Copied!
# GRADED FUNCTION: predict
def predict(w, b, X):
m = X.shape[1]
Y_prediction = np.zeros((1, m))
w = w.reshape(X.shape[0], 1)
# Compute vector "A" predicting the probabilities of a cat being present in the picture
### START CODE HERE ### (≈ 1 line of code)
A = sigmoid(np.dot(w.T, X) + b)
for i in range(A.shape[1]):
# Convert probabilities a[0,i] to actual predictions p[0,i]
### START CODE HERE ### (≈ 4 lines of code)
Y_prediction[0, i] = 1 if A[0, i] > 0.5 else 0
assert(Y_prediction.shape == (1, m))
return Y_prediction
# GRADED FUNCTION: predict
def predict(w, b, X):
m = X.shape[1]
Y_prediction = np.zeros((1, m))
w = w.reshape(X.shape[0], 1)
# Compute vector "A" predicting the probabilities of a cat being present in the picture
### START CODE HERE ### (≈ 1 line of code)
A = sigmoid(np.dot(w.T, X) + b)
for i in range(A.shape[1]):
# Convert probabilities a[0,i] to actual predictions p[0,i]
### START CODE HERE ### (≈ 4 lines of code)
Y_prediction[0, i] = 1 if A[0, i] > 0.5 else 0
assert(Y_prediction.shape == (1, m))
return Y_prediction
In [11]:
Copied!
#GRADED FUNCTION: model
def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost=False):
# initialize parameters with zeros (≈ 1 line of code)
w, b = initialize_with_zeros(X_train.shape[0])
# Gradient descent (≈ 1 line of code)
parameters, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)
# Retrieve parameters w and b from dictionary "parameters"
w = parameters["w"]
b = parameters["b"]
# Predict test/train set examples (≈ 2 lines of code)
Y_prediction_test = predict(w, b, X_test)
Y_prediction_train = predict(w, b, X_train)
# Print train/test Errors
print("train accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))
print("test accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))
d = {"costs": costs,
"Y_prediction_test": Y_prediction_test,
"Y_prediction_train" : Y_prediction_train,
"w" : w,
"b" : b,
"learning_rate" : learning_rate,
"num_iterations": num_iterations}
return d
#GRADED FUNCTION: model
def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost=False):
# initialize parameters with zeros (≈ 1 line of code)
w, b = initialize_with_zeros(X_train.shape[0])
# Gradient descent (≈ 1 line of code)
parameters, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)
# Retrieve parameters w and b from dictionary "parameters"
w = parameters["w"]
b = parameters["b"]
# Predict test/train set examples (≈ 2 lines of code)
Y_prediction_test = predict(w, b, X_test)
Y_prediction_train = predict(w, b, X_train)
# Print train/test Errors
print("train accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))
print("test accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))
d = {"costs": costs,
"Y_prediction_test": Y_prediction_test,
"Y_prediction_train" : Y_prediction_train,
"w" : w,
"b" : b,
"learning_rate" : learning_rate,
"num_iterations": num_iterations}
return d
In [12]:
Copied!
d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 2000, learning_rate = 0.005, print_cost = True)
d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 2000, learning_rate = 0.005, print_cost = True)
Cost after iteration 0: 0.693147 Cost after iteration 100: 0.584508 Cost after iteration 200: 0.466949 Cost after iteration 300: 0.376007 Cost after iteration 400: 0.331463 Cost after iteration 500: 0.303273 Cost after iteration 600: 0.279880 Cost after iteration 700: 0.260042 Cost after iteration 800: 0.242941 Cost after iteration 900: 0.228004 Cost after iteration 1000: 0.214820 Cost after iteration 1100: 0.203078 Cost after iteration 1200: 0.192544 Cost after iteration 1300: 0.183033 Cost after iteration 1400: 0.174399 Cost after iteration 1500: 0.166521 Cost after iteration 1600: 0.159305 Cost after iteration 1700: 0.152667 Cost after iteration 1800: 0.146542 Cost after iteration 1900: 0.140872 train accuracy: 99.04306220095694 % test accuracy: 70.0 %
In [13]:
Copied!
# Plot learning curve (with costs)
costs = np.squeeze(d['costs'])
plt.plot(costs)
plt.ylabel('cost')
plt.xlabel('iterations (per hundreds)')
plt.title("Learning rate =" + str(d["learning_rate"]))
plt.show()
# Plot learning curve (with costs)
costs = np.squeeze(d['costs'])
plt.plot(costs)
plt.ylabel('cost')
plt.xlabel('iterations (per hundreds)')
plt.title("Learning rate =" + str(d["learning_rate"]))
plt.show()
In [14]:
Copied!
learning_rates = [0.01, 0.001, 0.0001]
models = {}
for i in learning_rates:
print ("learning rate is: " + str(i))
models[str(i)] = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 1500, learning_rate = i, print_cost = False)
print ('\n' + "-------------------------------------------------------" + '\n')
for i in learning_rates:
plt.plot(np.squeeze(models[str(i)]["costs"]), label= str(models[str(i)]["learning_rate"]))
plt.ylabel('cost')
plt.xlabel('iterations')
legend = plt.legend(loc='upper center', shadow=True)
frame = legend.get_frame()
frame.set_facecolor('0.90')
plt.show()
learning_rates = [0.01, 0.001, 0.0001]
models = {}
for i in learning_rates:
print ("learning rate is: " + str(i))
models[str(i)] = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 1500, learning_rate = i, print_cost = False)
print ('\n' + "-------------------------------------------------------" + '\n')
for i in learning_rates:
plt.plot(np.squeeze(models[str(i)]["costs"]), label= str(models[str(i)]["learning_rate"]))
plt.ylabel('cost')
plt.xlabel('iterations')
legend = plt.legend(loc='upper center', shadow=True)
frame = legend.get_frame()
frame.set_facecolor('0.90')
plt.show()
learning rate is: 0.01 train accuracy: 99.52153110047847 % test accuracy: 68.0 % ------------------------------------------------------- learning rate is: 0.001 train accuracy: 88.99521531100478 % test accuracy: 64.0 % ------------------------------------------------------- learning rate is: 0.0001 train accuracy: 68.42105263157895 % test accuracy: 36.0 % -------------------------------------------------------
To test with your images :
In [15]:
Copied!
m_train = train_set_y.shape[1]
m_test = test_set_y.shape[1]
num_px = train_set_x_orig.shape[1]
m_train = train_set_y.shape[1]
m_test = test_set_y.shape[1]
num_px = train_set_x_orig.shape[1]
In [16]:
Copied!
pip install scipy==1.1.0
pip install scipy==1.1.0
Requirement already satisfied: scipy==1.1.0 in /usr/local/lib/python3.7/dist-packages (1.1.0) Requirement already satisfied: numpy>=1.8.2 in /usr/local/lib/python3.7/dist-packages (from scipy==1.1.0) (1.19.5)
In [19]:
Copied!
import imageio
# preprocessing the image to fit our algorithm.
fname = "/content/drive/MyDrive/Temporary/download.jpg"
image = np.array(imageio.imread(fname))
my_image = scipy.misc.imresize(image, size=(num_px, num_px)).reshape((1, num_px * num_px * 3)).T
my_predicted_image = predict(d["w"], d["b"], my_image)
plt.imshow(image)
print("y = " + str(np.squeeze(my_predicted_image)) + ", your algorithm predicts a \"" + classes[int(np.squeeze(my_predicted_image)),].decode("utf-8") + "\" picture.")
import imageio
# preprocessing the image to fit our algorithm.
fname = "/content/drive/MyDrive/Temporary/download.jpg"
image = np.array(imageio.imread(fname))
my_image = scipy.misc.imresize(image, size=(num_px, num_px)).reshape((1, num_px * num_px * 3)).T
my_predicted_image = predict(d["w"], d["b"], my_image)
plt.imshow(image)
print("y = " + str(np.squeeze(my_predicted_image)) + ", your algorithm predicts a \"" + classes[int(np.squeeze(my_predicted_image)),].decode("utf-8") + "\" picture.")
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:6: DeprecationWarning: `imresize` is deprecated! `imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use ``skimage.transform.resize`` instead.
y = 1.0, your algorithm predicts a "cat" picture.