CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
y33-j3T

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: y33-j3T/Coursera-Deep-Learning
Path: blob/master/Improving Deep Neural Networks Hyperparameter tuning, Regularization and Optimization/week5/Initialization/init_utils.py
Views: 13376
1
import numpy as np
2
import matplotlib.pyplot as plt
3
import h5py
4
import sklearn
5
import sklearn.datasets
6
7
def sigmoid(x):
8
"""
9
Compute the sigmoid of x
10
11
Arguments:
12
x -- A scalar or numpy array of any size.
13
14
Return:
15
s -- sigmoid(x)
16
"""
17
s = 1/(1+np.exp(-x))
18
return s
19
20
def relu(x):
21
"""
22
Compute the relu of x
23
24
Arguments:
25
x -- A scalar or numpy array of any size.
26
27
Return:
28
s -- relu(x)
29
"""
30
s = np.maximum(0,x)
31
32
return s
33
34
def forward_propagation(X, parameters):
35
"""
36
Implements the forward propagation (and computes the loss) presented in Figure 2.
37
38
Arguments:
39
X -- input dataset, of shape (input size, number of examples)
40
Y -- true "label" vector (containing 0 if cat, 1 if non-cat)
41
parameters -- python dictionary containing your parameters "W1", "b1", "W2", "b2", "W3", "b3":
42
W1 -- weight matrix of shape ()
43
b1 -- bias vector of shape ()
44
W2 -- weight matrix of shape ()
45
b2 -- bias vector of shape ()
46
W3 -- weight matrix of shape ()
47
b3 -- bias vector of shape ()
48
49
Returns:
50
loss -- the loss function (vanilla logistic loss)
51
"""
52
53
# retrieve parameters
54
W1 = parameters["W1"]
55
b1 = parameters["b1"]
56
W2 = parameters["W2"]
57
b2 = parameters["b2"]
58
W3 = parameters["W3"]
59
b3 = parameters["b3"]
60
61
# LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID
62
z1 = np.dot(W1, X) + b1
63
a1 = relu(z1)
64
z2 = np.dot(W2, a1) + b2
65
a2 = relu(z2)
66
z3 = np.dot(W3, a2) + b3
67
a3 = sigmoid(z3)
68
69
cache = (z1, a1, W1, b1, z2, a2, W2, b2, z3, a3, W3, b3)
70
71
return a3, cache
72
73
def backward_propagation(X, Y, cache):
74
"""
75
Implement the backward propagation presented in figure 2.
76
77
Arguments:
78
X -- input dataset, of shape (input size, number of examples)
79
Y -- true "label" vector (containing 0 if cat, 1 if non-cat)
80
cache -- cache output from forward_propagation()
81
82
Returns:
83
gradients -- A dictionary with the gradients with respect to each parameter, activation and pre-activation variables
84
"""
85
m = X.shape[1]
86
(z1, a1, W1, b1, z2, a2, W2, b2, z3, a3, W3, b3) = cache
87
88
dz3 = 1./m * (a3 - Y)
89
dW3 = np.dot(dz3, a2.T)
90
db3 = np.sum(dz3, axis=1, keepdims = True)
91
92
da2 = np.dot(W3.T, dz3)
93
dz2 = np.multiply(da2, np.int64(a2 > 0))
94
dW2 = np.dot(dz2, a1.T)
95
db2 = np.sum(dz2, axis=1, keepdims = True)
96
97
da1 = np.dot(W2.T, dz2)
98
dz1 = np.multiply(da1, np.int64(a1 > 0))
99
dW1 = np.dot(dz1, X.T)
100
db1 = np.sum(dz1, axis=1, keepdims = True)
101
102
gradients = {"dz3": dz3, "dW3": dW3, "db3": db3,
103
"da2": da2, "dz2": dz2, "dW2": dW2, "db2": db2,
104
"da1": da1, "dz1": dz1, "dW1": dW1, "db1": db1}
105
106
return gradients
107
108
def update_parameters(parameters, grads, learning_rate):
109
"""
110
Update parameters using gradient descent
111
112
Arguments:
113
parameters -- python dictionary containing your parameters
114
grads -- python dictionary containing your gradients, output of n_model_backward
115
116
Returns:
117
parameters -- python dictionary containing your updated parameters
118
parameters['W' + str(i)] = ...
119
parameters['b' + str(i)] = ...
120
"""
121
122
L = len(parameters) // 2 # number of layers in the neural networks
123
124
# Update rule for each parameter
125
for k in range(L):
126
parameters["W" + str(k+1)] = parameters["W" + str(k+1)] - learning_rate * grads["dW" + str(k+1)]
127
parameters["b" + str(k+1)] = parameters["b" + str(k+1)] - learning_rate * grads["db" + str(k+1)]
128
129
return parameters
130
131
def compute_loss(a3, Y):
132
133
"""
134
Implement the loss function
135
136
Arguments:
137
a3 -- post-activation, output of forward propagation
138
Y -- "true" labels vector, same shape as a3
139
140
Returns:
141
loss - value of the loss function
142
"""
143
144
m = Y.shape[1]
145
logprobs = np.multiply(-np.log(a3),Y) + np.multiply(-np.log(1 - a3), 1 - Y)
146
loss = 1./m * np.nansum(logprobs)
147
148
return loss
149
150
def load_cat_dataset():
151
train_dataset = h5py.File('datasets/train_catvnoncat.h5', "r")
152
train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
153
train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels
154
155
test_dataset = h5py.File('datasets/test_catvnoncat.h5', "r")
156
test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
157
test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels
158
159
classes = np.array(test_dataset["list_classes"][:]) # the list of classes
160
161
train_set_y = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
162
test_set_y = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
163
164
train_set_x_orig = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
165
test_set_x_orig = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T
166
167
train_set_x = train_set_x_orig/255
168
test_set_x = test_set_x_orig/255
169
170
return train_set_x, train_set_y, test_set_x, test_set_y, classes
171
172
173
def predict(X, y, parameters):
174
"""
175
This function is used to predict the results of a n-layer neural network.
176
177
Arguments:
178
X -- data set of examples you would like to label
179
parameters -- parameters of the trained model
180
181
Returns:
182
p -- predictions for the given dataset X
183
"""
184
185
m = X.shape[1]
186
p = np.zeros((1,m), dtype = np.int)
187
188
# Forward propagation
189
a3, caches = forward_propagation(X, parameters)
190
191
# convert probas to 0/1 predictions
192
for i in range(0, a3.shape[1]):
193
if a3[0,i] > 0.5:
194
p[0,i] = 1
195
else:
196
p[0,i] = 0
197
198
# print results
199
print("Accuracy: " + str(np.mean((p[0,:] == y[0,:]))))
200
201
return p
202
203
def plot_decision_boundary(model, X, y):
204
# Set min and max values and give it some padding
205
x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1
206
y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1
207
h = 0.01
208
# Generate a grid of points with distance h between them
209
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
210
# Predict the function value for the whole grid
211
Z = model(np.c_[xx.ravel(), yy.ravel()])
212
Z = Z.reshape(xx.shape)
213
# Plot the contour and training examples
214
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
215
plt.ylabel('x2')
216
plt.xlabel('x1')
217
plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)
218
plt.show()
219
220
def predict_dec(parameters, X):
221
"""
222
Used for plotting decision boundary.
223
224
Arguments:
225
parameters -- python dictionary containing your parameters
226
X -- input data of size (m, K)
227
228
Returns
229
predictions -- vector of predictions of our model (red: 0 / blue: 1)
230
"""
231
232
# Predict using forward propagation and a classification threshold of 0.5
233
a3, cache = forward_propagation(X, parameters)
234
predictions = (a3>0.5)
235
return predictions
236
237
def load_dataset():
238
np.random.seed(1)
239
train_X, train_Y = sklearn.datasets.make_circles(n_samples=300, noise=.05)
240
np.random.seed(2)
241
test_X, test_Y = sklearn.datasets.make_circles(n_samples=100, noise=.05)
242
# Visualize the data
243
plt.scatter(train_X[:, 0], train_X[:, 1], c=train_Y, s=40, cmap=plt.cm.Spectral);
244
train_X = train_X.T
245
train_Y = train_Y.reshape((1, train_Y.shape[0]))
246
test_X = test_X.T
247
test_Y = test_Y.reshape((1, test_Y.shape[0]))
248
return train_X, train_Y, test_X, test_Y
249