import numpy as np
import matplotlib.pyplot as plt
import scipy.special
import datetime
class neuralNetwork:
def __init__(self, inputnodes, hiddennodes, outputnodes,learningrate):
self.inodes = inputnodes
self.hnodes = hiddennodes
self.onodes = outputnodes
self.with1 = np.random.normal(0.0,pow(self.hnodes,-0.5),(self.hnodes,self.inodes))
self.who = np.random.normal(0.0,pow(self.onodes,-0.5),(self.onodes,self.hnodes))
self.lr = learningrate
self.activation_function = lambda x: scipy.special.expit(x)
pass
def train(self,inputs_list,targets_list):
inputs = np.array(inputs_list,ndmin=2).T
targets = np.array(targets_list,ndmin=2).T
hidden_inputs = np.dot(self.with1,inputs)
hidden_outputs = self.activation_function(hidden_inputs)
final_inputs = np.dot(self.who,hidden_outputs)
final_outputs = self.activation_function(final_inputs)
output_errors = targets - final_outputs
hidden_errors = np.dot(self.who.T,output_errors)
self.who += self.lr*np.dot((output_errors*final_outputs*(1-final_outputs)),np.transpose(hidden_outputs))
self.with1 += self.lr*np.dot((hidden_errors*hidden_outputs*(1-hidden_outputs)),np.transpose(inputs))
pass
def query(self,inputs_list):
inputs = np.array(inputs_list,ndmin=2).T
hidden_inputs = np.dot(self.with1,inputs)
hidden_outputs = self.activation_function(hidden_inputs)
final_inputs = np.dot(self.who,hidden_outputs)
final_outputs = self.activation_function(final_inputs)
return final_outputs
start1 = datetime.datetime.now()
input_nodes = 784
hidden_nodes = 200 #200
output_nodes = 10
learning_rate = 0.1
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 # 权重更新循环的次数
for e in range(epochs):
for record in training_data_list:
all_values = record.split(',')
inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
targets = np.zeros(output_nodes) +0.01
targets[int(all_values[0])] = 0.99
n.train(inputs,targets)
pass
pass
end1 = datetime.datetime.now()
print("程序训练所需时间为:" + str((end1 - start1).seconds) + "秒")
test_data_file = open("mnist_dataset/mnist_test.csv",'r')
test_data_list = test_data_file.readlines()
test_data_file.close()
start2 = datetime.datetime.now()
scorecared = []
for record in test_data_list:
all_values = record.split(',')
correct_lable = int(all_values[0])
inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
outputs = n.query(inputs)
label = np.argmax(outputs)
if (label == correct_lable):
scorecared.append(1)
else:
scorecared.append(0)
pass
pass
scorecared_array = np.asarray(scorecared)
print ("performance = ", scorecared_array.sum() / scorecared_array.size)
end2 = datetime.datetime.now()
print("程序测试所需时间为:" + str((end2 - start2).seconds) + "秒")
# print("程序所需时间为:" + str((end2 - start1).seconds) + "秒")
此代码来自于《Python圣经网络编程》作者塔里克.拉希德著 林赐 译
此程序中使用的数据来自,手写数字的数据集MNIST,
根据作者所述进行试验,对权重更新的次数(作者建议为5)、学习率(作者建议为0.1)、隐藏层节点的数目(作者建议为200)进行试验分析,试验结果统计如下:
这些试验数据只供参考,由于能力有限,有不足之处,还望多提建议。谢谢,,,,,,