神经网络手写数字识别学习


本文旨在神经网络手写数字识别学习笔记(一)的理解和基础上,一步一步操作神经网络基础模型的构建,完成手写数字识别的功能。

初始化Weight,bias

from mnist import MNIST
from scipy.linalg import expm, sinm, cosm
import numpy as np
import tensorflow as tf

mndata = MNIST('/home/warmtree/python-mnist/data')
images, labels = mndata.load_training()
images=np.array(images)/255 


INPUT_NODE = 2#784 #28*28
OUTPUT_NODE = 2#10 #输出10个结点,十种分类结果,对应0-9数字
HIDDEN_NODE = 3#30 #隐藏层有30个结点


weight_variable1=np.random.randn(HIDDEN_NODE,INPUT_NODE) 
weight_variable2=np.random.randn(OUTPUT_NODE, HIDDEN_NODE) 
bias1=np.zeros((HIDDEN_NODE,1)) #initial one
bias2=np.zeros((OUTPUT_NODE,1))

print(weight_variable1)
print(weight_variable2)
print(bias1)
print(bias2)

alpha = 0.05  #learning rate

我们首先测试以下,2输入层,3隐藏层,2输出层的初始化Weights 和Bias是否构建正确。

在终端进行测试,结果如下。

(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~/code/labs$ python text.py[[-0.34202146 -1.73997664]
 [ 1.20293535  0.06090624]
 [ 0.4435486   0.0234955 ]]
[[-0.39891768  0.28467452  0.61513217]
 [ 0.56107511  0.40020381 -0.8207778 ]]
[[0.]
 [0.]
 [0.]]
[[0.]
 [0.]]

在这里插入图片描述为了构造我们最终的矩阵(此处1暂且size为1*1) Z l = w l a l − 1 + b l ∗ 1 Z^l=w^la^{l-1}+b^l*1 Zl=wlal1+bl1
我们需要把Weights 和Bias结合起来。

x_input=[0.2,0.5]
# forward computation
z1 = np.dot(weight_variable1,np.transpose(x_input))+np.dot(bias1,one)
x2 = g(z1)
z2 = np.dot(weight_variable2,x2)+np.dot(bias2,one)
print(z1)
print(z2)

现在我们得到的矩阵就是整体的矩阵了,结果如图:

(base) warmtree@warmtree-HP-Pavilion-Laptop-15-cc5xx:~/code/labs$ python text.py[[-1.01828242  0.349962   -1.22051518]
 [-1.01828242  0.349962   -1.22051518]
 [-1.01828242  0.349962   -1.22051518]]
[[-0.54908179 -1.21379794 -0.47145384]
 [-0.67447777 -1.49099778 -0.57912161]]

接下来,我们开始编写反向传播部分。

y_output = g(z2)

#epochs and minibatches
epochs = 30
cont = 0
while(cont<epochs):
	#backpropagation
	error2 = y_output-y_lablel
	error1 = np.dot(np.transpose(weight_variable2),error2)*gd(z1)
	weight_variable2 = weight_variable2-(alpha/m)*np.dot(error2,np.transpose(x2))
	bias2 = bias2-(alpha/m)*np.dot(error2,np.transpose(x2))
	weight_variable1 = weight_variable1-(alpha/m)*np.dot(error1,np.transpose(x_input))
	bias1 = bias1-(alpha/m)*np.dot(error1,np.transpose(x_input))
	z1 = np.dot(weight_variable1,np.transpose(x_input))+np.dot(bias1,one)
	x2 = g(z1)
	z2 = np.dot(weight_variable2,x2)+np.dot(bias2,one)
	y_output = g(z2)
	cont++

现在呢,我们要把对我们训练的数据和标签进行处理,具体怎样处理呢?
这里,我们要把数据打乱顺序,所以呢,我们要用到numpy.random.shuffle打乱顺序函数

>>> list = [ i for i in range(10)]  
>>> list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list1 = np.random.shuffle(list)
>>> list1
>>> np.random.shuffle(list)
>>> list
[9, 7, 2, 5, 3, 8, 0, 6, 1, 4]
>>> list[1]
7

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐