-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultilayerperceptron.py
222 lines (175 loc) · 6.14 KB
/
multilayerperceptron.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# coding: utf-8
import numpy as np
from softmax import softmax_crossentropy
from softmax import grad_softmax_crossentropy
def train(network, X, y):
"""
Train our network on a test set X and Y.
Activate all layers
Backward prop from last to first so that Dense layers have already one gradient step
:param network: list of network
:param X: set of data
:param y: result matching X
:return:
"""
layer_activations = forward(network, X)
layer_inputs = [X] + layer_activations # layer_input[i] is an input for network[i]
logits = layer_activations[-1]
# Compute the loss and the initial gradient
loss = softmax_crossentropy(logits, y)
loss_grad = grad_softmax_crossentropy(logits, y)
# Propagate gradients through the network & Reverse propogation as this is backprop
for layer_index in range(len(network))[::-1]:
layer = network[layer_index]
loss_grad = layer.backward(layer_inputs[layer_index], loss_grad) # grad w.r.t. input, also weight updates
return np.mean(loss)
def predict(network, X):
"""
Make predictions
:param network: list of network
:param X: set of data
:return: largest prob
"""
logits = forward(network,X)[-1]
return logits.argmax(axis=-1)
def predict_probas(network, X):
"""
Make predictions
:param network: list of network
:param X: set of data
:return: largest prob
"""
logits = forward(network, X)[-1]
return logits
def forward(network, X):
"""
Activate all networks by applying them sequentially
:param network: list of network
:param X: set of data
:return:
"""
activations = []
input = X
for l in network:
activations.append(l.forward(input)) # Update to next
input = activations[-1]
assert len(activations) == len(network)
return activations
class Layer:
"""
Each Layer has be able to perform a pass forward and a pass backward.
So we have here our main class doing either backward or forward
"""
def __init__(self):
pass
def forward(self, input):
"""
Takes input of data and returns output data
:param input: data of shape [batch, input_units]
:return: input
"""
return input
def backward(self, input, grad_output):
"""
Backpropagation on the given input using less gradient
Because we already received d loss / d layer we only need to multiply it by d layer / d x
:param input: data of shape [batch, input_units]
:param grad_output:
:return: input
"""
num_units = input.shape[1]
d_layer_d_input = np.eye(num_units)
return np.dot(grad_output, d_layer_d_input)
class tanh(Layer):
"""
Applies non linearity to every element of the network
"""
def __init__(self):
pass
def forward(self, input):
"""
Apply elementwise Hyperbolic tangent function to [batch, input_units] matrix
:param input: data of shape [batch, input_units]
:return: input
"""
relu_forward = np.tanh(input)
return relu_forward
def backward(self, input, grad_output):
# Compute tahn gradient of loss on input
A = np.tanh(input)
return grad_output * (1 - np.square(A))
class ReLU(Layer):
"""
Applies non linearity to every element of the network
"""
def __init__(self):
pass
def forward(self, input):
"""
Apply elementwise ReLU to [batch, input_units] matrix
:param input: data of shape [batch, input_units]
:return: input
"""
relu_forward = np.maximum(0, input)
return relu_forward
def backward(self, input, grad_output):
# Compute ReLU gradient of loss on input
relu_grad = input > 0
return grad_output * relu_grad
class Sigmoid(Layer):
"""
Applies non linearity to every element of the network
"""
def __init__(self):
pass
def forward(self, input):
"""
Apply elementwise Sigmoid to [batch, input_units] matrix
:param input: data of shape [batch, input_units]
:return: input
"""
relu_forward = 1 / (1 + np.exp(-input))
return relu_forward
def backward(self, input, grad_output):
# Compute sigmoid gradient of loss on input
A = 1 / (1 + np.exp(-input))
return grad_output * A * (1 - A)
class Dense(Layer):
"""
This Layer is an Hidden one. It applies an affine transformation
"""
def __init__(self, input_units, output_units, learning_rate=0.1):
"""
Layer performing a learned affine transformation (f(x) = <W*x> + b)
:param input_units:
:param output_units:
:param learning_rate:
"""
self.learning_rate = learning_rate
self.weights = np.random.normal(loc=0.0,
scale=np.sqrt(2 / (input_units + output_units)),
size=(input_units, output_units))
self.biases = np.zeros(output_units)
def forward(self, input):
"""
Perform an affine transformation (f(x) = <W*x> + b)
:param input: data of shape [batch, input_units]
:return: input
"""
return np.dot(input, self.weights) + self.biases
def backward(self, input, grad_output):
"""
Perform (d f / d x = d f / d dense * d dense / d x)
Then compute gradient w.r.t. weights and biases
Finally compute gradient weights and biases
:param input:
:param grad_output:
:return:
"""
grad_input = np.dot(grad_output, self.weights.T) # d dense/ d x = weights transposed
grad_weights = np.dot(input.T, grad_output) # gradient on weights and biases
grad_biases = grad_output.mean(axis=0) * input.shape[0]
assert grad_weights.shape == self.weights.shape and grad_biases.shape == self.biases.shape
self.weights = self.weights - self.learning_rate * grad_weights # stochastic gradient descent
self.biases = self.biases - self.learning_rate * grad_biases
return grad_input