-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1_nn_lib.py
763 lines (619 loc) · 28.7 KB
/
part1_nn_lib.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
import numpy as np
import pickle
def xavier_init(size, gain = 1.0):
"""
Xavier initialization of network weights.
Arguments:
- size {tuple} -- size of the network to initialise.
- gain {float} -- gain for the Xavier initialisation.
Returns:
{np.ndarray} -- values of the weights.
"""
low = -gain * np.sqrt(6.0 / np.sum(size))
high = gain * np.sqrt(6.0 / np.sum(size))
return np.random.uniform(low=low, high=high, size=size)
class Layer:
"""
Abstract layer class.
"""
def __init__(self, *args, **kwargs):
raise NotImplementedError()
def forward(self, *args, **kwargs):
raise NotImplementedError()
def __call__(self, *args, **kwargs):
return self.forward(*args, **kwargs)
def backward(self, *args, **kwargs):
raise NotImplementedError()
def update_params(self, *args, **kwargs):
pass
class MSELossLayer(Layer):
"""
MSELossLayer: Computes mean-squared error between y_pred and y_target.
"""
def __init__(self):
self._cache_current = None
@staticmethod
def _mse(y_pred, y_target):
return np.mean((y_pred - y_target) ** 2)
@staticmethod
def _mse_grad(y_pred, y_target):
return 2 * (y_pred - y_target) / len(y_pred)
def forward(self, y_pred, y_target):
self._cache_current = y_pred, y_target
return self._mse(y_pred, y_target)
def backward(self):
return self._mse_grad(*self._cache_current)
class CrossEntropyLossLayer(Layer):
"""
CrossEntropyLossLayer: Computes the softmax followed by the negative
log-likelihood loss.
"""
def __init__(self):
self._cache_current = None
@staticmethod
def softmax(x):
numer = np.exp(x - x.max(axis=1, keepdims=True))
denom = numer.sum(axis=1, keepdims=True)
return numer / denom
def forward(self, inputs, y_target):
assert len(inputs) == len(y_target)
n_obs = len(y_target)
probs = self.softmax(inputs)
self._cache_current = y_target, probs
out = -1 / n_obs * np.sum(y_target * np.log(probs))
return out
def backward(self):
y_target, probs = self._cache_current
n_obs = len(y_target)
return -1 / n_obs * (y_target - probs)
class SigmoidLayer(Layer):
"""
SigmoidLayer: Applies sigmoid function elementwise.
"""
def __init__(self):
"""
Constructor of the Sigmoid layer.
"""
self._cache_current = None
def forward(self, x):
"""
Performs forward pass through the Sigmoid layer.
Logs information needed to compute gradient at a later stage in
`_cache_current`.
Arguments:
x {np.ndarray} -- Input array of shape (batch_size, n_in).
Returns:
{np.ndarray} -- Output array of shape (batch_size, n_out)
"""
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
# each element in x the operation is done to and return array of same shape
self._cache_current = 1 / (1 + np.exp(-x))
return self._cache_current
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
def backward(self, grad_z):
"""
Given `grad_z`, the gradient of some scalar (e.g. loss) with respect to
the output of this layer, performs back pass through the layer (i.e.
computes gradients of loss with respect to parameters of layer and
inputs of layer).
Arguments:
grad_z {np.ndarray} -- Gradient array of shape (batch_size, n_out).
Returns:
{np.ndarray} -- Array containing gradient with respect to layer
input, of shape (batch_size, n_in).
"""
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
# forwar_val = output of sigmoid function for each input in the batch
# grad_z = gradient of loss with respect to output of this layer
# cache_current -> (batch, in)
# grad_z -> (batch, out)
forward_val = self._cache_current
return grad_z * forward_val * (1 - forward_val)
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
class ReluLayer(Layer):
"""
ReluLayer: Applies Relu function elementwise.
"""
def __init__(self):
"""
Constructor of the Relu layer.
"""
self._cache_current = None
def forward(self, x):
"""
Performs forward pass through the Relu layer.
Logs information needed to compute gradient at a later stage in
`_cache_current`.
Arguments:
x {np.ndarray} -- Input array of shape (batch_size, n_in).
Returns:
{np.ndarray} -- Output array of shape (batch_size, n_out)
"""
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
# if value is greater than 0 return the value
# return array that is the max between 0 and the value
self._cache_current = np.maximum(0, x)
return self._cache_current
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
def backward(self, grad_z):
"""
Given `grad_z`, the gradient of some scalar (e.g. loss) with respect to
the output of this layer, performs back pass through the layer (i.e.
computes gradients of loss with respect to parameters of layer and
inputs of layer).
Arguments:
grad_z {np.ndarray} -- Gradient array of shape (batch_size, n_out).
Returns:
{np.ndarray} -- Array containing gradient with respect to layer
input, of shape (batch_size, n_in).
"""
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
# The derivative of the ReLU activation function is 1 for positive values,
# and 0 for negative values. Therefore, during backpropagation, we multiply
# the gradient 'grad_z' by a binary mask (0 for negative, 1 for positive).
return grad_z * (self._cache_current > 0)
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
class LinearLayer(Layer):
"""
LinearLayer: Performs affine transformation of input.
"""
def __init__(self, n_in, n_out):
"""
Constructor of the linear layer.
Arguments:
- n_in {int} -- Number (or dimension) of inputs.
- n_out {int} -- Number (or dimension) of outputs.
"""
self.n_in = n_in
self.n_out = n_out
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
# Weights --> (output, input)
self._W = xavier_init(size=(self.n_out, self.n_in))
# Biases --> bias (1, output)
self._b = np.zeros((1, self.n_out))
# Leave as None for the back propagation
self._cache_current = None
self._grad_W_current = None
self._grad_b_current = None
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
def forward(self, x):
"""
Performs forward pass through the layer (i.e. returns Wx + b).
Logs information needed to compute gradient at a later stage in
`_cache_current`.
Arguments:
x {np.ndarray} -- Input array of shape (batch_size, n_in).
Returns:
{np.ndarray} -- Output array of shape (batch_size, n_out)
"""
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
# store the input to the layer for backpropagation
self._cache_current = x
# print statement for debugging purposes
# print("shape of x, W.T, b for linear layer forward: ", x.shape, self._W.T.shape, self._b.shape, sep="\n")
# (out, in)T --> (in, out) by (batch, in) --> (batch out)
z = np.matmul(x, self._W.T) + self._b
return z
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
def backward(self, grad_z):
"""
Given `grad_z`, the gradient of some scalar (e.g. loss) with respect to
the output of this layer, performs back pass through the layer (i.e.
computes gradients of loss with respect to parameters of layer and
inputs of layer).
Arguments:
grad_z {np.ndarray} -- Gradient array of shape (batch_size, n_out).
Returns:
{np.ndarray} -- Array containing gradient with respect to layer
input, of shape (batch_size, n_in).
"""
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
# Gradient w.r.t. to input x
# print(f"the shape for grad_z and W is: {grad_z.shape}, {self._W.shape}")
grad_x = np.matmul(grad_z, self._W)
# Gradient w.r.t.to weight matrix W
self._grad_W_current = np.matmul(self._cache_current.T, grad_z)
# Resulting shape is (1, n_out) to match the bias term dimensions.
self._grad_b_current = np.sum(grad_z, axis=0, keepdims=True)
return grad_x
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
def update_params(self, learning_rate):
"""
Performs one step of gradient descent with given learning rate on the
layer's parameters using currently stored gradients.
Arguments:
learning_rate {float} -- Learning rate of update step.
"""
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
#self._W -> (out, in)
#self._grad_W_current -> (in, out)
#self._b -> (1, out)
#self._grad_b_current -> (1, out)
self._W = self._W - learning_rate * self._grad_W_current.T
self._b = self._b - learning_rate * self._grad_b_current
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
class MultiLayerNetwork(object):
"""
MultiLayerNetwork: A network consisting of stacked linear layers and
activation functions.
"""
def __init__(self, input_dim, neurons, activations):
"""
Constructor of the multi layer network.
Arguments:
- input_dim {int} -- Number of features in the input (excluding
the batch dimension).
- neurons {list} -- Number of neurons in each linear layer
represented as a list. The length of the list determines the
number of linear layers.
- activations {list} -- List of the activation functions to apply
to the output of each linear layer.
"""
self.input_dim = input_dim
self.neurons = neurons
self.activations = activations
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
# Initialize the list to store layers
self._layers = []
current_input_dim = input_dim
# Iterate over pairs of neurons and activation functions
for n_neurons, activation in zip(neurons, activations):
# Add linear layer to layers list
linear_layer = LinearLayer(n_in=current_input_dim, n_out=n_neurons)
self._layers.append(linear_layer)
# Add activation layer based on the specified activation function
if activation == 'relu':
activation_layer = ReluLayer() # Instantiate ReluLayer
elif activation == 'sigmoid':
activation_layer = SigmoidLayer() # Instantiate SigmoidLayer
elif activation == 'identity':
# No additional activation layer for 'identity'
activation_layer = None
else:
# Handle function does not have an implementation
raise ValueError(f"Unsupported activation function: {activation}")
# Add the activation layer to layers list
if activation_layer is not None:
self._layers.append(activation_layer)
# Update input_dim for the next layer
current_input_dim = n_neurons
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
def forward(self, x):
"""
Performs forward pass through the network.
Arguments:
x {np.ndarray} -- Input array of shape (batch_size, input_dim).
Returns:
{np.ndarray} -- Output array of shape (batch_size,
#_neurons_in_final_layer)
"""
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
# Initialize current_input with the input data
current_input = x
# Iterate through each layer in the neural network
for layer in self._layers:
# Perform the forward pass for the current layer, updating current_input
output = layer.forward(current_input)
# Update current_input with the output for the next layer
current_input = output
# Return the final output after passing through all layers
return output
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
def __call__(self, x):
return self.forward(x)
def backward(self, grad_z):
"""
Performs backward pass through the network.
Arguments:
grad_z {np.ndarray} -- Gradient array of shape (batch_size,
#_neurons_in_final_layer).
Returns:
{np.ndarray} -- Array containing gradient with respect to layer
input, of shape (batch_size, input_dim).
"""
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
# Initialize the gradient to the initial gradient passed (grad_z)
current_grad = grad_z
# Iterate through layers in reverse order
for layer in reversed(self._layers):
# Backward pass through the current layer, updating current_grad
current_grad = layer.backward(current_grad)
# The final current_grad represents the gradient with respect to the input of the entire network
return current_grad
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
def update_params(self, learning_rate):
"""
Performs one step of gradient descent with given learning rate on the
parameters of all layers using currently stored gradients.
Arguments:
learning_rate {float} -- Learning rate of update step.
"""
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
for layer in self._layers:
if(isinstance(layer, LinearLayer)):
layer.update_params(learning_rate)
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
def save_network(network, fpath):
"""
Utility function to pickle `network` at file path `fpath`.
"""
with open(fpath, "wb") as f:
pickle.dump(network, f)
def load_network(fpath):
"""
Utility function to load network found at file path `fpath`.
"""
with open(fpath, "rb") as f:
network = pickle.load(f)
return network
class Trainer(object):
"""
Trainer: Object that manages the training of a neural network.
"""
def __init__(
self,
network,
batch_size,
nb_epoch,
learning_rate,
loss_fun,
shuffle_flag,
):
"""
Constructor of the Trainer.
Arguments:
- network {MultiLayerNetwork} -- MultiLayerNetwork to be trained.
- batch_size {int} -- Training batch size.
- nb_epoch {int} -- Number of training epochs.
- learning_rate {float} -- SGD learning rate to be used in training.
- loss_fun {str} -- Loss function to be used. Possible values: mse,
cross_entropy.
- shuffle_flag {bool} -- If True, training data is shuffled before
training.
"""
self.network = network
self.batch_size = batch_size
self.nb_epoch = nb_epoch
self.learning_rate = learning_rate
self.loss_fun = loss_fun
self.shuffle_flag = shuffle_flag
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
if loss_fun == 'mse':
self._loss_layer = MSELossLayer()
elif loss_fun == 'cross_entropy':
self._loss_layer = CrossEntropyLossLayer()
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
@staticmethod
def shuffle(input_dataset, target_dataset):
"""
Returns shuffled versions of the inputs.
Arguments:
- input_dataset {np.ndarray} -- Array of input features, of shape
(#_data_points, n_features) or (#_data_points,).
- target_dataset {np.ndarray} -- Array of corresponding targets, of
shape (#_data_points, #output_neurons).
Returns:
- {np.ndarray} -- shuffled inputs.
- {np.ndarray} -- shuffled_targets.
"""
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
# Generate random indices to achieve consistent shuffling
indices = np.arange(len(input_dataset))
np.random.shuffle(indices)
# Shuffle input dataset and target dataset
input_dataset_shuffled = input_dataset[indices]
target_dataset_shuffled = target_dataset[indices]
return input_dataset_shuffled, target_dataset_shuffled
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
def train(self, input_dataset, target_dataset):
"""
Main training loop. Performs the following steps `nb_epoch` times:
- Shuffles the input data (if `shuffle` is True)
- Splits the dataset into batches of size `batch_size`.
- For each batch:
- Performs forward pass through the network given the current
batch of inputs.
- Computes loss.
- Performs backward pass to compute gradients of loss with
respect to parameters of network.
- Performs one step of gradient descent on the network
parameters.
Arguments:
- input_dataset {np.ndarray} -- Array of input features, of shape
(#_training_data_points, n_features).
- target_dataset {np.ndarray} -- Array of corresponding targets, of
shape (#_training_data_points, #output_neurons).
"""
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
num_samples = len(input_dataset)
# Loop through epochs
for epoch in range(self.nb_epoch):
# Shuffle the dataset if the flag is True
if self.shuffle_flag:
input_dataset, target_dataset = self.shuffle(input_dataset, target_dataset)
# Loop through the dataset in mini-batches
for i in range(0, num_samples, self.batch_size):
# Extract a mini-batch
input_batch = input_dataset[i:i+self.batch_size]
target_batch = target_dataset[i:i+self.batch_size]
# Forward pass through loss layer
error = self.eval_loss(input_batch, target_batch)
# Backward pass to create grad_z
grad_z = self._loss_layer.backward()
# Update the parameters of the network using gradients and learning rate
self.network.backward(grad_z=grad_z)
self.network.update_params(self.learning_rate)
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
def eval_loss(self, input_dataset, target_dataset):
"""
Function that evaluate the loss function for given data. Returns
scalar value.
Arguments:
- input_dataset {np.ndarray} -- Array of input features, of shape
(#_evaluation_data_points, n_features).
- target_dataset {np.ndarray} -- Array of corresponding targets, of
shape (#_evaluation_data_points, #output_neurons).
Returns:
a scalar value -- the loss
"""
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
output = self.network.forward(input_dataset)
loss = 0
if isinstance(self._loss_layer, MSELossLayer):
loss = self._loss_layer.forward(output, target_dataset)
elif isinstance(self._loss_layer, CrossEntropyLossLayer):
loss = self._loss_layer.forward(output, target_dataset)
else:
raise ValueError(f"Unsupported loss function: {self.loss_fun}")
return loss
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
class Preprocessor(object):
"""
Preprocessor: Object used to apply "preprocessing" operation to datasets.
The object can also be used to revert the changes.
"""
def __init__(self, data):
"""
Initializes the Preprocessor according to the provided dataset.
(Does not modify the dataset.)
Arguments:
data {np.ndarray} dataset used to determine the parameters for
the normalization.
"""
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
# Compute and store normalization parameters
self.min_vals = np.min(data, axis=0)
self.max_vals = np.max(data, axis=0)
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
def apply(self, data):
"""
Apply the pre-processing operations to the provided dataset.
Arguments:
data {np.ndarray} dataset to be normalized.
Returns:
{np.ndarray} normalized dataset.
"""
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
return (data - self.min_vals) / (self.max_vals - self.min_vals)
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
def revert(self, data):
"""
Revert the pre-processing operations to retrieve the original dataset.
Arguments:
data {np.ndarray} dataset for which to revert normalization.
Returns:
{np.ndarray} reverted dataset.
"""
#######################################################################
# ** START OF YOUR CODE **
#######################################################################
return data * (self.max_vals - self.min_vals) + self.min_vals
#######################################################################
# ** END OF YOUR CODE **
#######################################################################
def example_main():
input_dim = 4
neurons = [16, 3]
activations = ["relu", "identity"]
net = MultiLayerNetwork(input_dim, neurons, activations)
dat = np.loadtxt("iris.dat")
np.random.shuffle(dat)
x = dat[:, :4]
y = dat[:, 4:]
split_idx = int(0.8 * len(x))
x_train = x[:split_idx]
y_train = y[:split_idx]
x_val = x[split_idx:]
y_val = y[split_idx:]
prep_input = Preprocessor(x_train)
x_train_pre = prep_input.apply(x_train)
x_val_pre = prep_input.apply(x_val)
trainer = Trainer(
network=net,
batch_size=8,
nb_epoch=1000,
learning_rate=0.01,
loss_fun="cross_entropy",
shuffle_flag=True,
)
trainer.train(x_train_pre, y_train)
print(f"Train loss = {trainer.eval_loss(x_train_pre, y_train)}")
print(f"Validation loss = {trainer.eval_loss(x_val_pre, y_val)}")
preds = net(x_val_pre).argmax(axis=1).squeeze()
targets = y_val.argmax(axis=1).squeeze()
accuracy = (preds == targets).mean()
print(f"Validation accuracy: {accuracy}")
if __name__ == "__main__":
example_main()