本文实例为大家分享了python实现手写数字识别的具体代码,供大家参考,具体内容如下
import numpy import scipy.special #import matplotlib.pyplot 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.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) 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_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)) pass def query(self,input_list): inputs=numpy.array(input_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 hidden_nodes=100 output_nodes=10 learning_rate=0.1 n=neuralNetwork(input_nodes,hidden_nodes,output_nodes,learning_rate) training_data_file=open(r"C:\Users\lsy\Desktop\nn\mnist_train.csv","r") training_data_list=training_data_file.readlines() training_data_file.close() #print(n.wih) #print("") epochs=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) #print(n.wih) #print(len(training_data_list)) #for i in training_data_list: # print(i) test_data_file=open(r"C:\Users\lsy\Desktop\nn\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_lable=int(all_values[0]) inputs=(numpy.asfarray(all_values[1:])/255.0*0.99)+0.01 outputs=n.query(inputs) label=numpy.argmax(outputs) if(label==correct_lable): scorecard.append(1) else: scorecard.append(0) scorecard_array=numpy.asarray(scorecard) print(scorecard_array) print("") print(scorecard_array.sum()/scorecard_array.size) #all_value=test_data_list[0].split(",") #input=(numpy.asfarray(all_value[1:])/255.0*0.99)+0.01 #print(all_value[0]) #image_array=numpy.asfarray(all_value[1:]).reshape((28,28)) #matplotlib.pyplot.imshow(image_array,cmap="Greys",interpolation="None") #matplotlib.pyplot.show() #nn=n.query((numpy.asfarray(all_value[1:])/255.0*0.99)+0.01) #for i in nn : # print(i)
《python神经网络编程》中代码,仅做记录,以备后用。
image_file_name=r"*.JPG" img_array=scipy.misc.imread(image_file_name,flatten=True) img_data=255.0-img_array.reshape(784) image_data=(img_data/255.0*0.99)+0.01
图片对应像素的读取。因训练集灰度值与实际相反,故用255减取反。
import numpy import scipy.special #import matplotlib.pyplot import scipy.misc from PIL import Image 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.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) 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_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)) pass def query(self,input_list): inputs=numpy.array(input_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 hidden_nodes=100 output_nodes=10 learning_rate=0.1 n=neuralNetwork(input_nodes,hidden_nodes,output_nodes,learning_rate) training_data_file=open(r"C:\Users\lsy\Desktop\nn\mnist_train.csv","r") training_data_list=training_data_file.readlines() training_data_file.close() #print(n.wih) #print("") #epochs=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) #image_file_name=r"C:\Users\lsy\Desktop\nn\1000-1.JPG" ''' img_array=scipy.misc.imread(image_file_name,flatten=True) img_data=255.0-img_array.reshape(784) image_data=(img_data/255.0*0.99)+0.01 #inputs=(numpy.asfarray(image_data)/255.0*0.99)+0.01 outputs=n.query(image_data) label=numpy.argmax(outputs) print(label) ''' #print(n.wih) #print(len(training_data_list)) #for i in training_data_list: # print(i) test_data_file=open(r"C:\Users\lsy\Desktop\nn\mnist_test.csv","r") test_data_list=test_data_file.readlines() test_data_file.close() scorecard=[] total=[0,0,0,0,0,0,0,0,0,0] rightsum=[0,0,0,0,0,0,0,0,0,0] for record in test_data_list: all_values=record.split(",") correct_lable=int(all_values[0]) inputs=(numpy.asfarray(all_values[1:])/255.0*0.99)+0.01 outputs=n.query(inputs) label=numpy.argmax(outputs) total[correct_lable]+=1 if(label==correct_lable): scorecard.append(1) rightsum[correct_lable]+=1 else: scorecard.append(0) scorecard_array=numpy.asarray(scorecard) print(scorecard_array) print("") print(scorecard_array.sum()/scorecard_array.size) print("") print(total) print(rightsum) for i in range(10): print((rightsum[i]*1.0)/total[i]) #all_value=test_data_list[0].split(",") #input=(numpy.asfarray(all_value[1:])/255.0*0.99)+0.01 #print(all_value[0]) #image_array=numpy.asfarray(all_value[1:]).reshape((28,28)) #matplotlib.pyplot.imshow(image_array,cmap="Greys",interpolation="None") #matplotlib.pyplot.show() #nn=n.query((numpy.asfarray(all_value[1:])/255.0*0.99)+0.01) #for i in nn : # print(i)
尝试统计了对于各个数据测试数量及正确率。
原本想验证书后向后查询中数字‘9'识别模糊是因为训练数量不足或错误率过高而产生,然最终结果并不支持此猜想。
另书中只能针对特定像素的图片进行学习,真正手写的图片并不能满足训练条件,实际用处仍需今后有时间改进。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月16日
2024年11月16日
- 群星.1995-坠入情网【宝丽金】【WAV+CUE】
- 群星《谁杀死了Hi-Fi音乐》涂鸦精品 [WAV+CUE][1G]
- 群星1998《宝丽金最精彩98》香港首版[WAV+CUE][1G]
- 汪峰《也许我可以无视死亡》星文[WAV+CUE][1G]
- 李嘉-1991《国语转调2》[天王唱片][WAV整轨]
- 蔡琴2008《金声回忆录101》6CD[环星唱片][WAV整轨]
- 群星《2024好听新歌36》AI调整音效【WAV分轨】
- 梁朝伟.1986-朦胧夜雨裡(华星40经典)【华星】【WAV+CUE】
- 方芳.1996-得意洋洋【中唱】【WAV+CUE】
- 辛欣.2001-放120个心【上海音像】【WAV+CUE】
- 柏菲·万山红《花开原野1》限量开盘母带ORMCD[低速原抓WAV+CUE]
- 柏菲·万山红《花开原野2》限量开盘母带ORMCD[低速原抓WAV+CUE]
- 潘安邦《思念精选集全纪录》5CD[WAV+CUE]
- 杨千嬅《千嬅新唱金牌金曲》金牌娱乐 [WAV+CUE][985M]
- 杨钰莹《依然情深》首版[WAV+CUE][1G]