-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnn.py
305 lines (239 loc) · 7.63 KB
/
nn.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import sys
import os
import numpy as np
import pandas as pd
# The seed will be fixed to 42 for this assigmnet.
np.random.seed(42)
NUM_FEATS = 90
class Net(object):
'''
'''
def __init__(self, num_layers, num_units):
'''
Initialize the neural network.
Create weights and biases.
Here, we have provided an example structure for the weights and biases.
It is a list of weight and bias matrices, in which, the
dimensions of weights and biases are (assuming 1 input layer, 2 hidden layers, and 1 output layer):
weights: [(NUM_FEATS, num_units), (num_units, num_units), (num_units, num_units), (num_units, 1)]
biases: [(num_units, 1), (num_units, 1), (num_units, 1), (num_units, 1)]
Please note that this is just an example.
You are free to modify or entirely ignore this initialization as per your need.
Also you can add more state-tracking variables that might be useful to compute
the gradients efficiently.
Parameters
----------
num_layers : Number of HIDDEN layers.
num_units : Number of units in each Hidden layer.
'''
self.num_layers = num_layers
self.num_units = num_units
self.biases = []
self.weights = []
for i in range(num_layers):
if i==0:
# Input layer
self.weights.append(np.random.uniform(-1, 1, size=(NUM_FEATS, self.num_units)))
else:
# Hidden layer
self.weights.append(np.random.uniform(-1, 1, size=(self.num_units, self.num_units)))
self.biases.append(np.random.uniform(-1, 1, size=(self.num_units, 1)))
# Output layer
self.biases.append(np.random.uniform(-1, 1, size=(1, 1)))
self.weights.append(np.random.uniform(-1, 1, size=(self.num_units, 1)))
def __call__(self, X):
'''
Forward propagate the input X through the network,
and return the output.
Note that for a classification task, the output layer should
be a softmax layer. So perform the computations accordingly
Parameters
----------
X : Input to the network, numpy array of shape m x d
Returns
----------
y : Output of the network, numpy array of shape m x 1
'''
raise NotImplementedError
def backward(self, X, y, lamda):
'''
Compute and return gradients loss with respect to weights and biases.
(dL/dW and dL/db)
Parameters
----------
X : Input to the network, numpy array of shape m x d
y : Output of the network, numpy array of shape m x 1
lamda : Regularization parameter.
Returns
----------
del_W : derivative of loss w.r.t. all weight values (a list of matrices).
del_b : derivative of loss w.r.t. all bias values (a list of vectors).
Hint: You need to do a forward pass before performing backward pass.
'''
raise NotImplementedError
class Optimizer(object):
'''
'''
def __init__(self, learning_rate):
'''
Create a Gradient Descent based optimizer with given
learning rate.
Other parameters can also be passed to create different types of
optimizers.
Hint: You can use the class members to track various states of the
optimizer.
'''
raise NotImplementedError
def step(self, weights, biases, delta_weights, delta_biases):
'''
Parameters
----------
weights: Current weights of the network.
biases: Current biases of the network.
delta_weights: Gradients of weights with respect to loss.
delta_biases: Gradients of biases with respect to loss.
'''
raise NotImplementedError
def loss_mse(y, y_hat):
'''
Compute Mean Squared Error (MSE) loss betwee ground-truth and predicted values.
Parameters
----------
y : targets, numpy array of shape m x 1
y_hat : predictions, numpy array of shape m x 1
Returns
----------
MSE loss between y and y_hat.
'''
raise NotImplementedError
def loss_regularization(weights, biases):
'''
Compute l2 regularization loss.
Parameters
----------
weights and biases of the network.
Returns
----------
l2 regularization loss
'''
raise NotImplementedError
def loss_fn(y, y_hat, weights, biases, lamda):
'''
Compute loss = loss_mse(..) + lamda * loss_regularization(..)
Parameters
----------
y : targets, numpy array of shape m x 1
y_hat : predictions, numpy array of shape m x 1
weights and biases of the network
lamda: Regularization parameter
Returns
----------
l2 regularization loss
'''
raise NotImplementedError
def rmse(y, y_hat):
'''
Compute Root Mean Squared Error (RMSE) loss betwee ground-truth and predicted values.
Parameters
----------
y : targets, numpy array of shape m x 1
y_hat : predictions, numpy array of shape m x 1
Returns
----------
RMSE between y and y_hat.
'''
raise NotImplementedError
def cross_entropy_loss(y, y_hat):
'''
Compute cross entropy loss
Parameters
----------
y : targets, numpy array of shape m x 1
y_hat : predictions, numpy array of shape m x 1
Returns
----------
cross entropy loss
'''
raise NotImplementedError
def train(
net, optimizer, lamda, batch_size, max_epochs,
train_input, train_target,
dev_input, dev_target
):
'''
In this function, you will perform following steps:
1. Run gradient descent algorithm for `max_epochs` epochs.
2. For each bach of the training data
1.1 Compute gradients
1.2 Update weights and biases using step() of optimizer.
3. Compute RMSE on dev data after running `max_epochs` epochs.
Here we have added the code to loop over batches and perform backward pass
for each batch in the loop.
For this code also, you are free to heavily modify it.
'''
m = train_input.shape[0]
for e in range(max_epochs):
epoch_loss = 0.
for i in range(0, m, batch_size):
batch_input = train_input[i:i+batch_size]
batch_target = train_target[i:i+batch_size]
pred = net(batch_input)
# Compute gradients of loss w.r.t. weights and biases
dW, db = net.backward(batch_input, batch_target, lamda)
# Get updated weights based on current weights and gradients
weights_updated, biases_updated = optimizer.step(net.weights, net.biases, dW, db)
# Update model's weights and biases
net.weights = weights_updated
net.biases = biases_updated
# Compute loss for the batch
batch_loss = loss_fn(batch_target, pred, net.weights, net.biases, lamda)
epoch_loss += batch_loss
#print(e, i, rmse(batch_target, pred), batch_loss)
#print(e, epoch_loss)
# Write any early stopping conditions required (only for Part 2)
# Hint: You can also compute dev_rmse here and use it in the early
# stopping condition.
# After running `max_epochs` (for Part 1) epochs OR early stopping (for Part 2), compute the RMSE on dev data.
dev_pred = net(dev_input)
dev_rmse = rmse(dev_target, dev_pred)
print('RMSE on dev data: {:.5f}'.format(dev_rmse))
def get_test_data_predictions(net, inputs):
'''
Perform forward pass on test data and get the final predictions that can
be submitted on Kaggle.
Write the final predictions to the part2.csv file.
Parameters
----------
net : trained neural network
inputs : test input, numpy array of shape m x d
Returns
----------
predictions (optional): Predictions obtained from forward pass
on test data, numpy array of shape m x 1
'''
raise NotImplementedError
def read_data():
'''
Read the train, dev, and test datasets
'''
raise NotImplementedError
return train_input, train_target, dev_input, dev_target, test_input
def main():
# Hyper-parameters
max_epochs = 50
batch_size = 256
learning_rate = 0.001
num_layers = 1
num_units = 64
lamda = 0.1 # Regularization Parameter
train_input, train_target, dev_input, dev_target, test_input = read_data()
net = Net(num_layers, num_units)
optimizer = Optimizer(learning_rate)
train(
net, optimizer, lamda, batch_size, max_epochs,
train_input, train_target,
dev_input, dev_target
)
get_test_data_predictions(net, test_input)
if __name__ == '__main__':
main()