-
Notifications
You must be signed in to change notification settings - Fork 0
/
vary_learning_rate.py
60 lines (49 loc) · 1.72 KB
/
vary_learning_rate.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
from neural_network import NeuralNetwork
import matplotlib.pyplot as plt
import numpy
# Setting up the value for the neural network
input_nodes = 784
hidden_nodes = 100
output_nodes = 10
scorecard = []
def scorecard_generation(record):
test_value = record.split(',')
network_lable = neural_network_object.query(
(numpy.asfarray(test_value[1:])/255.0*0.99)+0.01)
if (int(test_value[0]) == numpy.argmax(network_lable)):
return 1
else:
return 0
training_file = open('mnist_train.csv', 'r')
training_list = training_file.readlines()
training_file.close()
score = []
learning_rate = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
for i in range(1, 10):
# Initializing the neural network
neural_network_object = NeuralNetwork(input_nodes, hidden_nodes,
output_nodes, learning_rate[i-1])
for record in training_list:
values = record.split(',')
input_array = (numpy.asfarray(values[1:])/255.0*0.99)+0.01
target = numpy.zeros(output_nodes)+0.01
target[int(values[0])] = 0.99
neural_network_object.train(input_array, target)
test_file = open('mnist_test.csv', 'r')
test_list = test_file.readlines()
test_file.close()
scorecard = map(lambda record: scorecard_generation(record), test_list)
score.append(sum(scorecard)/10000.0)
print score
print learning_rate
# The graph is Accuracy against the learning rate which actually gives total
# justification as the accuracy decreases with learning rate increase
y = score
x = learning_rate
plt.plot(x, y)
plt.yscale('linear')
plt.title('Varying Learning Rate')
plt.xlabel("Learning Rate")
plt.ylabel("Accuracy")
plt.grid(True)
plt.show()