-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterface.py
More file actions
128 lines (89 loc) · 3.4 KB
/
Interface.py
File metadata and controls
128 lines (89 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import numpy as np
def initialize_parameters_random(layers_dims):
np.random.seed(3)
params = {}
L = len(layers_dims)
for l in range(1, L):
params[f'W{l}'] = np.random.randn(layers_dims[l], layers_dims[l - 1]) * 0.01
params[f'b{l}'] = np.zeros((layers_dims[l], 1))
return params
def linear_forward(A, W, b):
Z = np.dot(W, A) + b
return Z, (A, W, b)
def sigmoid(Z):
return 1 / (1 + np.exp(-Z)), Z
def relu(Z):
return np.maximum(0, Z), Z
def linear_activation_forward(A_prev, W, b, activation):
Z, linear_cache = linear_forward(A_prev, W, b)
if activation == "relu":
A, activation_cache = relu(Z)
elif activation == "sigmoid":
A, activation_cache = sigmoid(Z)
assert(A.shape == (W.shape[0], A_prev.shape[1]))
return A, (linear_cache, activation_cache)
def L_model_forward(X, params):
L = len(params) // 2
A = X
caches = []
for l in range(1, L):
A_prev = A
A, cache = linear_activation_forward(A_prev, params[f'W{l}'], params[f'b{l}'], "relu")
caches.append(cache)
AL, cache = linear_activation_forward(A, params[f'W{L}'], params[f'b{L}'], "sigmoid")
caches.append(cache)
assert (AL.shape == (1, X.shape[1]))
return AL, caches
def predict(X, params):
Y_, _ = L_model_forward(X, params)
return np.vectorize(lambda x: 1 if x > 0.5 else 0)(Y_)
def accuracy(X, Y, params):
predictions = predict(X, params)
return (np.dot(Y, predictions.T) + np.dot(1 - Y, (1 - predictions).T)) * 100 / Y.size
def compute_cost(AL, Y):
m = Y.shape[1] # number of samples
cost = -(np.dot(np.log(AL), Y.T)) + np.dot(np.log(1 - AL), (1 - Y).T)
cost /= m
cost = np.squeeze(cost) # To make sure the cost's shape is what we expected
return cost
def linear_backward(dZ, cache):
A_prev, W, b = cache
m = A_prev.shape[1]
dW = np.dot(dZ, A_prev.T) / m
db = np.sum(dZ, axis=1, keepdims=True) / m
dA_prev = np.dot(W.T, dZ)
assert (dA_prev.shape == A_prev.shape)
assert (dW.shape == W.shape)
assert (db.shape == b.shape)
return dA_prev, dW, db
def linear_activation_backward(dA, cache, activation):
linear_cache, Z = cache
if activation == "relu":
dZ = dA * np.vectorize(lambda x: 1 if x > 0 else 0)(Z)
dA_prev, dW, db = linear_backward(dZ, linear_cache)
elif activation == "sigmoid":
sigm, _ = sigmoid(Z)
dZ = dA * (sigm * (1 - sigm))
dA_prev, dW, db = linear_backward(dZ, linear_cache)
return dA_prev, dW, db
def L_model_backward(AL, Y, caches):
grads = {}
m = AL.shape[1]
# Y = Y.reshape(AL.shape) # Y is the same shape as AL
L = len(caches)
dAL = -(np.divide(Y, AL) - np.divide((1 - Y), (1 - AL)))
current_cache = caches[L - 1]
grads[f'dA{L}'], grads[f'dW{L}'], grads[f'db{L}'] = linear_activation_backward(dAL, current_cache, "sigmoid")
for l in reversed(range(L - 1)):
current_cache = caches[l];
dA_prev, dW, db = linear_activation_backward(grads[f'dA{l+2}'], current_cache, "relu")
grads[f'dA{l + 1}'] = dA_prev
grads[f'dW{l + 1}'] = dW
grads[f'db{l + 1}'] = db
return grads
def update_parameters(params, grads, learning_rate):
L = len(params) // 2
for l in range(L):
params[f'W{l + 1}'] -= learning_rate * grads[f'dW{l + 1}']
params[f'b{l + 1}'] -= learning_rate * grads[f'db{l + 1}']
return params