-
Notifications
You must be signed in to change notification settings - Fork 0
/
backprop_main.py
71 lines (51 loc) · 2.41 KB
/
backprop_main.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
import numpy as np
from matplotlib import pyplot as plt
import backprop_data
import backprop_network
training_data, test_data = backprop_data.load(train_size=10000,test_size=5000)
net = backprop_network.Network([784, 40, 10])
net.SGD(training_data, epochs=30, mini_batch_size=10, learning_rate=0.1, test_data=test_data)
def question_a():
print("-----(a)------")
net = backprop_network.Network([784, 40, 10])
net.SGD(training_data, epochs=30, mini_batch_size=10, learning_rate=0.1, test_data=test_data)
def question_b():
print("-----(b)------")
rates = [0.001, 0.01, 0.1, 1, 10, 100]
num_rates = len(rates)
train_accuracy = np.empty(num_rates, dtype=object)
train_loss = np.empty(num_rates, dtype=object)
test_accuracy = np.empty(num_rates, dtype=object)
for rate_index, learning_rate in enumerate(rates):
print("(b) Rate " + str(learning_rate))
net = backprop_network.Network([784, 40, 10])
train_accuracy[rate_index], train_loss[rate_index], test_accuracy[rate_index] = net.SGD(training_data, epochs=30, mini_batch_size=10,
learning_rate=learning_rate, test_data=test_data)
plt.plot(np.arange(30), train_accuracy[rate_index], label=r"rate = {}".format(learning_rate))
plt.xlabel(r"Epochs", fontsize=13)
plt.ylabel(r"Accuracy", fontsize=13)
plt.title(r"(b) Training Accuracy", fontsize=19)
plt.legend()
plt.show()
for rate_index, learning_rate in enumerate(rates):
plt.plot(np.arange(30), train_loss[rate_index], label="rate = {}".format(learning_rate))
plt.xlabel(r"Epochs", fontsize=13)
plt.ylabel(r"$\ell (\mathcal{W})$", fontsize=13)
plt.title(r"(b) Training loss $\ell (\mathcal{W})$", fontsize=19)
plt.legend()
plt.show()
for rate_index, learning_rate in enumerate(rates):
plt.plot(np.arange(30), test_accuracy[rate_index], label="rate = {}".format(learning_rate))
plt.xlabel(r"Epochs", fontsize=13)
plt.ylabel(r"Accuracy", fontsize=13)
plt.title(r"(b) Test Accuracy", fontsize=19)
plt.legend()
plt.show()
def question_c():
print("-----(c)------")
training_data, test_data = backprop_data.load(train_size=50000,test_size=10000)
net = backprop_network.Network([784, 40, 10])
net.SGD(training_data, epochs=30, mini_batch_size=10, learning_rate=0.1,test_data=test_data)
question_a()
question_b()
question_c()