深度学习在手写数字识别上的使用方法

深度学习在手写数字识别上的使用方法

本节描述如何给手写的数字图片进行数字识别,我们知道,数字识别的公式是:z=x*w+b。其中x是指图片数据,w是权重值,b是偏置值。程序的实现方法如下:
x = tf.placeholder(tf.float32, [None,784],name=’picture’)
w = tf.placeholder(tf.float32, [784,10],name=’weight’)
b = tf.placeholder(tf.float32, [10],name=’bias’)
z = tf.matmul(x, w) + b
上述程序中,我们所采用的手写数字图是28*28尺寸,进行一纬展后,得到784个数据
w是二维数组,分别记录了每个象素点在数字0-9上的权重值
b是一维数字,记录了每张图片在数字0-9上的偏置。
在计算出z的结果后,通过y = tf.nn.softmax(z)进行归一化运算,就可以得到图片对应0-9数字的概率。
程序的具体运行过程如下:
首先定义shibie_num_2d_array=bs_np.zeros((100,10))来记录100张图片的计算结果
然后定义shibie_num=bs_np.zeros((100),dtype=int)来记录100张图片的识别结果

with tf.Session() as sess:
通过bias_data_1d_array=_read_bias_file_(‘bias.bin’)读入偏置数据
通过weight_2d_array=_read_weight_file_(‘weight.bin’)读入权重数据
通过label_byte_array=_read_txt_label_file_(“label/label.txt”)读入图片的正确数据用于精确度分析

下面程序读入100张图片,并进行识别,识别后把结果存入数组
for i in range(100):
image_filename= ‘pic/’ + str(i) + ‘.png’
bs_image_1d_float_array=_read_image_file_(image_filename)
num_array=sess.run(y,feed_dict={x:bs_image_1d_float_array,w:weight_2d_array,b:bias_data_1d_array})
shibie_num[i]=_get_the_maximun_value_index_(num_array.flat)
shibie_num_2d_array[i]=num_array.flat

接下来我们看看程序运行的情况:
用于识别的图片显示如下:
00000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000
0000000000000000000000000D1D1D421C00000AB3F22F0000000000
00000000000000000000001390FDFCFCD7AA521CD1FD540000000000
00000000000000000000007BFCFDFCFCFCFDF048D2FD540000000000
0000000000000000000097F6FCB21C1C1CFD975BFCFD540000000000
00000000000000000010B3FDB200000000A65BE5FDC9000000000000
0000000000000000002FC4FC670000000010D7FCFC00000000000000
0000000000000000000083FCE426000000CCFCFC7F00000000000000
0000000000000000000020E4FCE22626D5FDFC7F0300000000000000
000000000000000000000055FDFFCBFDFDD626000000000000000000
00000000000000000000001CB8FDFCFCCA0000000000000000000000
00000000000000000000001DB8FDFCFC1C0000000000000000000000
000000000000000000001A9FFCFDFCFCB20000000000000000000000
0000000000000000000078FDFD72C2FDFD3F00000000000000000000
000000000000000000AAE1E9600083FCFC2600000000000000000000
000000000000000059FDFC50000DCEFCCA0000000000000000000000
0000000000000026E1FD6606000DCEFC660000000000000000000000
0000000000000056FDFB4B000068FDCE0D0000000000000000000000
000000000000006EFCF4905FA9FDFC8E000000000000000000000000
000000000000006EFCFDFCFCFCF45D0D000000000000000000000000
000000000000000A80FDFCCA66190000000000000000000000000000
00000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000

权重数据如下:
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
-6.21575339e-07 -1.86172690e-06 -5.57173621e-07 -2.32155664e-08
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
。。。
-1.54554844e-04 -3.04110348e-04 -3.00577842e-04 -5.12265367e-04
-3.28946696e-03 -4.95949062e-03 -5.91816520e-03 -6.42772997e-03
-3.28246248e-03 -2.04786961e-03 -1.98838650e-03 -2.18997570e-03
-4.78268741e-03 -4.11115354e-03 -9.97261051e-03 -2.30694585e-03
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00

偏置数据如下:
-0.38391989 0.36819908 0.14742972 -0.2485853 0.00678487
1.50934291 -0.1230232 0.66861355 -1.6545428 -0.29029697

所获得的计算结果是:
。。。
19:[ 0.00 0.01 0.00 0.06 0.01 0.01 0.00 0.16 0.40 0.34 ]=0.400554 num=8/9 N
20:[ 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 ]=0.996658 num=4/4 Y
21:[ 0.99 0.00 0.00 0.00 0.00 0.01 0.00 0.00 0.00 0.00 ]=0.987394 num=0/0 Y
22:[ 0.00 0.00 0.01 0.01 0.17 0.01 0.01 0.10 0.16 0.54 ]=0.538625 num=9/9 Y
23:[ 0.00 0.90 0.02 0.01 0.00 0.00 0.00 0.00 0.07 0.00 ]=0.901153 num=1/1 Y
24:[ 0.00 0.09 0.02 0.24 0.00 0.38 0.03 0.03 0.18 0.02 ]=0.384953 num=5/1 N
25:[ 0.00 0.00 0.18 0.16 0.00 0.00 0.00 0.00 0.65 0.00 ]=0.654692 num=8/2 N
26:[ 0.00 0.00 0.00 0.01 0.92 0.01 0.00 0.01 0.01 0.05 ]=0.920881 num=4/4 Y
27:[ 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 ]=0.999994 num=3/3 Y
28:[ 0.00 0.00 0.06 0.01 0.00 0.00 0.00 0.93 0.00 0.00 ]=0.930135 num=7/2 N

其中,:前面的数字是图片序号,[。。。]内的数据为图片为0-9中数字的概率,=后面是最大的概率,/前面的数字是识别出来的数据,/后面是实际的数据,如果识别正确,则为Y,识别错误,则为N。