• python_神经网络识别手写数字0-9


      1 import numpy as np
      2 import matplotlib.pyplot as plt

    5 import struct 6 i=0 7 #读取训练数据 8 file_list=['train-images.idx3-ubyte','train-labels.idx1-ubyte','t10k-images.idx3-ubyte','t10k-labels.idx1-ubyte'] 9 with open('./sjwl/train-images.idx3-ubyte','rb') as f: 10 header_data=f.read(16) 11 image_data=[] 12 image=[] 13 while i<1000: 14 im=f.read(28*28) 15 image_data.append(im) 16 tp=struct.unpack('784B',image_data[i]) 17 image.append(tp) 18 i=i+1 19 with open('./sjwl/train-labels.idx1-ubyte','rb') as f: 20 header_label=f.read(8) 21 labels_data=[] 22 labels=[] 23 i=0 24 while i<1000: 25 la=f.read(1) 26 labels_data.append(la) 27 label=struct.unpack('>B',labels_data[i]) 28 labels.append(label) 29 i=i+1 30 #sigmoid函数 31 def sigmoid(x): 32 y=1/(1+np.exp(-x)) 33 return y 34 #学习率eta 35 eta=0.2 36 #创建隐层 37 b=np.zeros((1,100))#隐层输出矩阵,隐层神经元个数 38 alpha=np.zeros((1,100))#隐层神经元输入 39 eh=np.zeros((1,100))#隐层梯度项 40 #创建输出层 41 y=np.zeros((1,10))#输出层输出 42 beta=np.zeros((1,10))#输出层输入 43 gj=np.zeros((1,10))#输出层梯度项 44 #定义连接权 45 v=np.random.rand(784,100)#输入层-隐层-连接权 46 delt_v=np.zeros((784,100))#输入层-隐层-连接权-误差 47 w=np.random.rand(100,10)#隐层-输出层-连接权 48 delt_w=np.zeros((100,10))#隐层-输出层-连接权-误差 49 #定义阈值 50 thita=np.random.rand(1,10)#输出层神经元阈值 51 delt_thita=np.zeros((1,10))#输出层神经元阈值-误差 52 gamma=np.random.rand(1,100)#隐层神经元阈值 53 delt_gamma=np.zeros((1,100))#隐层神经元阈值-误差 54 #循环 55 for daishu in range(1000): 56 for n in range(100): 57 image_=np.asarray(image[n]) 58 shuru=np.asarray(image_) 59 shuru=shuru/255 60 mubiao=np.asarray(labels[n]) 61 if mubiao[0]==0: 62 mubiaojz=np.asarray([1,0,0,0,0,0,0,0,0,0]) 63 elif mubiao[0]==1: 64 mubiaojz=np.asarray([0,1,0,0,0,0,0,0,0,0]) 65 elif mubiao[0]==2: 66 mubiaojz=np.asarray([0,0,1,0,0,0,0,0,0,0]) 67 elif mubiao[0]==3: 68 mubiaojz=np.asarray([0,0,0,1,0,0,0,0,0,0]) 69 elif mubiao[0]==4: 70 mubiaojz=np.asarray([0,0,0,0,1,0,0,0,0,0]) 71 elif mubiao[0]==5: 72 mubiaojz=np.asarray([0,0,0,0,0,1,0,0,0,0]) 73 elif mubiao[0]==6: 74 mubiaojz=np.asarray([0,0,0,0,0,0,1,0,0,0]) 75 elif mubiao[0]==7: 76 mubiaojz=np.asarray([0,0,0,0,0,0,0,1,0,0]) 77 elif mubiao[0]==8: 78 mubiaojz=np.asarray([0,0,0,0,0,0,0,0,1,0]) 79 elif mubiao[0]==9: 80 mubiaojz=np.asarray([0,0,0,0,0,0,0,0,0,1]) 81 #计算输出 82 alpha=shuru@v 83 alpha=alpha/100 84 b=sigmoid(alpha-gamma) 85 beta=b@w 86 beta=beta/100 87 y=sigmoid(beta-thita) 88 #计算输出层神经元梯度项 89 gj=y*(1-y)*(mubiaojz-y) 90 #计算隐层神经元梯度项 91 eh=b*(1-b)*(gj@w.T) 92 #更新链接权 93 delt_w=eta*b.T@gj 94 delt_thita=-eta*gj 95 shuru=shuru.reshape(1,784) 96 delt_v=eta*shuru.T@eh 97 delt_gamma=-eta*eh 98 w=w+delt_w 99 thita=thita+delt_thita 100 v=v+delt_v 101 gamma=gamma+delt_gamma

    运行完测试一下

    数字0

    输出结果矩阵y

    代表数字0的y[0]=0.957864064,代表1-9的y[1:9]的结果接近于0,识别结果是数字0。 

  • 相关阅读:
    HashTable、HashSet和Dictionary的区别
    CCF_ 201312-3_最大的矩形
    CCF_ 201312-2_ISBN号码
    CCF_201312-1_出现次数最多的数
    CCF_ 201509-2_日期计算
    CCF_ 201512-3_画图
    CCF_ 201512-2_消除类游戏
    CCF_ 201409-2_画图
    CCF_201409-1_相邻数对
    CCF_ 201412-1_门禁系统
  • 原文地址:https://www.cnblogs.com/winterbear/p/12627090.html
Copyright © 2020-2023  润新知