-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralNetwork.py
More file actions
149 lines (99 loc) · 3.58 KB
/
NeuralNetwork.py
File metadata and controls
149 lines (99 loc) · 3.58 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import numpy as np
class NeuralNetwork:
def __init__(self, layer_dims, learning_rate, iteration=1000):
self.init_parameters(layer_dims)
self.iteration = iteration
self.learning_rate = learning_rate
def init_parameters(self, layer_dims):
self.parameters = {}
self.L = len(layer_dims)
for l in range(1, self.L):
self.parameters['W'+str(l)] = np.random.randn(layer_dims[l], layer_dims[l-1])*0.01
self.parameters['b'+str(l)] = np.zeros((layer_dims[l]))
def linear_forward(self, A, W, b):
Z = np.dot(W, A) + b
cache = (A, W, b)
return Z, cache
def sigmoid(self, Z):
A = 1.0 / (1.0 + np.exp(-Z))
return A, (Z)
def relu(self, Z):
A = np.max(0, Z)
return A, (Z)
def linear_activation_forward(self, A_prev, W, b, activation):
if activation == 'sigmoid':
Z, linear_cache = self.linear_forward(A_prev, W, b)
A, activation_cache = self.sigmoid(Z)
elif activation == 'relu':
Z, linear_cache = self.linear_cache(A_prev, W, b)
A, activation_cache = self.relu(Z)
cache = (linear_cache, activation_cache)
return A, cache
def L_model_forward(self, X):
caches = []
A = X
for l in xrange(1, self.L):
A, cache = self.linear_activation_forward(A_prev,
self.parameters['W'+str(l)],
self.parameters['b'+str(l)],
activation='relu')
caches.append(cache)
AL, cache = self.linear_activation_forward(A,
self.parameters['W'+str(self.L)],
self.parameters['b'+str(self.L)],
activation='sigmoid')
caches.append(cache)
return AL, caches
def compute_cost(self, AL, Y):
m = Y.shape[1]
cost = (-1/m) * np.sum(np.multiply(Y, np.log(AL)) + np.multiply(1-Y, np.log(1-AL)))
return cost
def linear_backward(self, dZ, cache):
A_prev, W, b = cache
m = A_prev.shape[1]
dW = np.dot(dZ, cache[0].T)/m
db = np.sum(dZ, axis = 1, keepdims=True)/m
dA_prev = np.dot(cache[1].T, dZ)
return dA_prev, dW, db
def sigmoid_backward(self, dA, activation_cache):
Z = activation_cache[0]
dZ = dA*self.sigmoid(Z)*(1-self.sigmoid(Z))
return dZ
def relu_backward(self, dA, activation_cache):
Z = activation_cache[0]
dZ = dA*(1.0 if Z > 0 else 0.0)
return dZ
def linear_activation_backward(self, dA, cache, activation):
linear_cache, activation_cache = cache
if activation=='sigmoid':
dZ = self.sigmoid_backward(dA, activation_cache)
elif activation=='relu':
dZ = self.relu_backward(dA, activation_cache)
dA_prev, dW, db = self.linear_backward(dZ, linear_cache)
return dA_prev, dW, db
def L_model_backward(self, AL, Y, caches):
self.grads = {}
m = AL.shape[1]
Y = Y.reshape(AL.shape)
dAL = -(np.divide(Y, AL) - np.divide(1 - Y, 1 - AL))
current_cache = caches[self.L-1]
self.grads['dA'+str(self.L-1)], self.grads['dW'+str(self.L)], self.grads['db'+str(self.L)] = self.linear_activation_backward(dAL, current_cache, activation='sigmoid')
for l in reversed(range(self.L-1)):
current_cache = caches[l]
dA_prev_temp, dW_temp, db_temp = self.linear_activation_backward(self.grads['dA'+str(l+1)], current_cache, activation='relu')
self.grads['dA'+str(l)] = dA_prev_temp
self.grads['dW'+str(l+1)] = dW_temp
self.grads['db'+str(l+1)] = db_temp
def update_parameters(self):
for l in xrange(self.L):
self.parameters['W'+str(l)] -= self.learning_rate*self.grads['dW'+str(l)]
self.parameters['b'+str(l)] -= self.learning_rate*self.grads['db'+str(l)]
def fit(self, X, Y):
for _ in range(self.iteration):
AL, caches = self.L_model_forward(X)
self.L_model_backward(AL, Y, caches)
self.update_parameters(self.learning_rate)
def predict(self, X):
AL, _ = self.L_model_forward(X)
return AL
)