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
|
import numpy
import matplotlib.pyplot
import scipy.special
%matplotlib inline
class neuralNetwork:
def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate): # 初始化神经网络
self.inodes = inputnodes
self.hnodes = hiddennodes
self.onodes = outputnodes
self.lr = learningrate # 学习率
# from node i to node j in the next layer
self.wih = numpy.random.normal(0.0, pow(self.hnodes, -0.5), (self.hnodes, self.inodes)) # 权重矩阵
self.who = numpy.random.normal(0.0, pow(self.onodes, -0.5), (self.onodes, self.hnodes))
self.activation_function = lambda x: scipy.special.expit(x) # 应用激活函数
def train(self, inputs_list, targets_list):
inputs = numpy.array(inputs_list, ndmin=2).T
targets = numpy.array(targets_list, ndmin=2).T
hidden_inputs = numpy.dot(self.wih, inputs)
hidden_outputs = self.activation_function(hidden_inputs)
final_inputs = numpy.dot(self.who, hidden_outputs)
final_outputs = self.activation_function(final_inputs)
output_errors = targets - final_outputs
hidden_errors = numpy.dot(self.who.T, output_errors)
self.who += self.lr*numpy.dot((output_errors*final_outputs*(1.0-final_outputs)), numpy.transpose(hidden_outputs)) # 调整隐藏层与输出层之间的权重
self.wih += self.lr*numpy.dot((hidden_errors*hidden_outputs*(1.0-hidden_outputs)), numpy.transpose(inputs)) # 调整输入层与隐藏层之间的权重
def query(self, inputs_list):
inputs = numpy.array(inputs_list, ndmin=2).T # 输入矩阵的转置矩阵
hidden_inputs = numpy.dot(self.wih, inputs)
hidden_outputs = self.activation_function(hidden_inputs)
final_inputs = numpy.dot(self.who, hidden_outputs)
final_outputs = self.activation_function(final_inputs)
return final_outputs
input_nodes = 784 # 28*28,输入层节点数
hidden_nodes = 100 # 隐藏层节点数
output_nodes = 10 # 输出层节点
learning_rate = 0.2 # 学习率
# 生成一个神经网络实例
n = neuralNetwork(input_nodes,hidden_nodes,output_nodes,learning_rate)
# 加载训练数据
training_data_file = open("mnist_dataset/mnist_train.csv", 'r')
training_data_list = training_data_file.readlines()
training_data_file.close()
# 训练神经网络
epochs = 5 # 训练5个世代(本篇的学习率为0.2,训练数据为六万组,训练五个世代,测试数据为一万组)
for e in range(epochs):
for record in training_data_list:
all_values = record.split(',')
inputs = (numpy.asfarray(all_values[1:])/255.0*0.99)+0.01
targets = numpy.zeros(output_nodes) + 0.01
targets[int(all_values[0])] = 0.99
n.train(inputs,targets)
# 加载测试数据(本篇使用了一万组测试数据,正确率为95.11%,结果可能不大一致,但大概都在94%~95%)
test_data_file = open("mnist_dataset/mnist_test.csv", 'r')
test_data_list = test_data_file.readlines()
test_data_file.close()
# 测试神经网络
scorecard = []
for record in test_data_list:
all_values = record.split(',')
correct_label = int(all_values[0])
# print(correct_label, "correct lable")
inputs = (numpy.asfarray(all_values[1:])/255.0*0.99)+0.01
outputs = n.query(inputs)
label = numpy.argmax(outputs)
# print(label, "networks's answer")
if(label == correct_label):
scorecard.append(1)
else:
scorecard.append(0)
pass
pass
# print(scorecard) # 输出训练成绩
scorecard_array = numpy.asarray(scorecard)
print("performances = ", scorecard_array.sum()*1.0/scorecard_array.size)
|