手动搭建三层神经网络,反向传播,梯度下降
反向传播核心公式推导如下:
标题本次练习数据集采用mnist训练集有6000条数据进行训练,20000条测试机,最终准确率为97%。
链接:https://pan.baidu.com/s/1mg3V4PqKX1EzBpOtzXLD5Q
提取码:6666
–来自百度网盘超级会员V4的分享
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2023/7/21 14:59
# @Author : qian
# @FileName: 神经网络.py
# @Software: PyCharm
import numpy.random
import scipy.special
class neuralNetwork():
def __init__(self,inputnodes,hiddennodes,outputnodes,learningrate):
self.inodes=inputnodes
self.hnodes=hiddennodes
self.onodes=outputnodes
self.lr=learningrate
# 链接权重矩阵
self.wih=numpy.random.rand(self.hnodes,self.inodes)-0.5
self.who=numpy.random.rand(self.onodes,self.hnodes)-0.5
# 激活函数
self.activation_function=lambda x:scipy.special.expit(x)
pass
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_input=numpy.dot(self.who,hidden_outputs)
final_output=self.activation_function(final_input)
#计算误差
output_errors=targets-final_output
hidden_errors=numpy.dot(self.who.T,output_errors)
self.who+=self.lr*numpy.dot((output_errors*final_output*(1.0-final_output)),numpy.transpose(hidden_outputs))
self.wih+=self.lr*numpy.dot(hidden_errors*hidden_outputs*(1.0-hidden_outputs),numpy.transpose(inputs))
pass
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_intputs=numpy.dot(self.who,hidden_outputs)
final_outputs=self.activation_function(final_intputs)
return final_outputs
input_node=784
hidden_nodes=200
output_nodes=10
learning_rate=0.1
n=neuralNetwork(input_node,hidden_nodes,output_nodes,learning_rate)
data_file=open("./mnist_train.csv",'r')
data_list=data_file.readlines()
data_file.close()
test_data_file=open("./mnist_test.csv",'r')
test_data_list=test_data_file.readlines()
test_data_file.close()
for e in range(5):
for record in data_list:
all_values = record.split(',')
inputs = numpy.asfarray(all_values[1:]) / 255.0 * 0.99 + 0.01
targeys = numpy.zeros(output_nodes) + 0.01
targeys[int(all_values[0])] = 0.99
n.train(inputs,targeys)
score=[]
epochs=5
for test_data in test_data_list:
test_values=test_data.split(',')
correct_label=int(test_values[0])
print(correct_label,"正确答案")
output=n.query((numpy.asfarray(test_values[1:])/255.0*0.99)+0.01)
label=numpy.argmax(output)
print(label,"训练结果答案")
if label==correct_label:
score.append(1)
else:
score.append(0)
print(score)
score_array=numpy.asarray(score)
print("正确率为:",score_array.sum()/score_array.size)
# test_values=test_data_list[0].split(',')
# print(test_values[0])
# print(n.query(numpy.asfarray(test_values[1:])/255.0*0.99)+0.01)