-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathchapter3-regularization.py
More file actions
44 lines (37 loc) · 1.27 KB
/
chapter3-regularization.py
File metadata and controls
44 lines (37 loc) · 1.27 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
import sys
sys.path.append('..')
from elements import network2
from elements import mnist_loader
import matplotlib.pyplot as plt
training_data, validation_data, test_data = mnist_loader.load_data_wrapper('../mnist.pkl.gz')
training_data = list(training_data)
test_data = list(test_data)
net = network2.Network([784, 30, 10], cost=network2.CrossEntropyCost)
net.large_weight_initializer()
evaluation_cost, evaluation_accuracy, training_cost, training_accuracy = \
net.SGD(training_data[:1000], 400, 10, 0.5,
evaluation_data=test_data,
lmbda = 0.1, # this is a regularization parameter
monitor_evaluation_cost=True,
monitor_evaluation_accuracy=True,
monitor_training_cost=True,
monitor_training_accuracy=True)
plt.figure(figsize=(6,5))
plt.plot(np.array(training_cost[200:])/1000)
plt.grid()
plt.title('Cost on the training data')
plt.xlabel('Epoch')
plt.show()
plt.figure(figsize=(6,5))
plt.plot(np.array(evaluation_accuracy[200:])/1000)
plt.grid()
plt.title('Accuracy (%) on the test data')
plt.xlabel('Epoch')
plt.show()
plt.figure(figsize=(6,5))
plt.plot(np.array(training_accuracy[:30])/1000)
plt.plot(np.array(evaluation_accuracy[:30])/10000)
plt.grid()
plt.xlabel('Epoch')
plt.legend(['Accuracy on the training data','Accuracy on the test data'])
plt.show()