-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemD.py
More file actions
53 lines (38 loc) · 1.63 KB
/
Copy pathProblemD.py
File metadata and controls
53 lines (38 loc) · 1.63 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
import neuralnet
import numpy as np
import pickle
import matplotlib as mpl
mpl.use('TkAgg')
import matplotlib.pyplot as plt
def main():
train_data_fname = 'MNIST_train.pkl'
valid_data_fname = 'MNIST_valid.pkl'
test_data_fname = 'MNIST_test.pkl'
X_train, y_train = neuralnet.load_data(train_data_fname)
X_valid, y_valid = neuralnet.load_data(valid_data_fname)
X_test, y_test = neuralnet.load_data(test_data_fname)
# found this as the optimal number of epochs from Part C
# optimal value is about 26 epochs
# To test regularization, going to check for a few more epochs
neuralnet.config['epochs'] = 30
regularization_constant_testers = [0.0001, 0.001]
for regFactor in regularization_constant_testers:
neuralnet.config['L2_penalty'] = regFactor
network = neuralnet.Neuralnetwork(neuralnet.config)
training_errors, validation_errors, best_model, numEpochs = neuralnet.trainer(network, X_train, y_train, X_valid, y_valid, network.config)
network.layers = best_model
accuracy = neuralnet.test(network, X_test, y_test, network.config)
print("Regularization Constant: ", regFactor)
print("Accuracy on Test Set: ", accuracy)
print()
plt.plot(range(len(training_errors)), training_errors,"ro", color = "blue", label= 'Training Set Accuracy')
plt.plot(range(len(validation_errors)), validation_errors,"ro", color = "red", label= 'Validation Set Accuracy')
plt.legend(loc='upper left')
plt.xlabel("Epochs")
plt.ylabel("Percentage Correct")
plt.title("Training with regularization factor: " + str(regFactor))
name = "partD_" + str(regFactor) + ".png"
plt.savefig(name)
plt.close()
if __name__ == '__main__':
main()