数字手写体的卷积神经网络实现

使用卷积神经网络实现手写体数字识别,在过程中使用了不同的卷积层个数及全连接层的长度,在组建模型的过程中发现卷积层中的图像深度可以调节,在全连接层中,全连接层的长度可以自由定义。例如:对于一个卷及神经网络模型,
| conv1-5x5x32 | max_pool1-2x2 |
|conv2-5x5x64|max_pool2-2x2|
| fc1-512 | fc2-10 |
对此模型,我们可以进行更改,将此模型改为

  • conv1-5x5x16
  • max_pool1-2x2
  • conv2-5x5x64
  • conv3-5x5x128
  • max-pool2-2x2
  • fc1-3000
  • fc2-1024
  • fc3-10
    模型更改后对模型进行训练,发现模型训练时间变长,且在增加模型卷积层的深度以及全连接层的节点数后模型训练的正确率反而降低。
    下面为修改后的模型代码:
#coding:utf-8
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("MNIST_data",one_hot=True)
x=tf.placeholder(tf.float32,shape=[None,784])
y_=tf.placeholder(tf.float32,shape=[None,10])
x_image=tf.reshape(x,[-1,28,28,1])
#定义权重
def weight_variable(shape):
    initializer=tf.truncated_normal(shape,stddev=0.1)
    return tf.Variable(initializer)
def biase_variable(shape):
    initializer=tf.constant(0.1,shape=shape)
    return tf.Variable(initializer)
def conv2d(x,w):
    return tf.nn.conv2d(x,w,strides=[1,1,1,1],padding="SAME")
def max_pool_2x2(x):
    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
#第一层卷积
w_conv1=weight_variable([5,5,1,16])
b_conv1=biase_variable([16])
h_conv1=tf.nn.relu(conv2d(x_image,w_conv1)+b_conv1)
#第一层池化
h_pool1=max_pool_2x2(h_conv1)
#第二层卷积
w_conv2=weight_variable([5,5,16,64])
b_conv2=weight_variable([64])
h_conv2=tf.nn.relu(conv2d(h_pool1,w_conv2)+b_conv2)
#第三层卷积
w_conv3=weight_variable([5,5,64,128])
b_conv3=weight_variable([128])
h_conv3=tf.nn.relu(conv2d(h_conv2,w_conv3)+b_conv3)
#第二层池化
h_pool2=max_pool_2x2(h_conv3)
#第一层全连接
w_fc1=weight_variable([7*7*128,3000])
b_fc1=biase_variable([3000])
h_pool2_flat=tf.reshape(h_pool2,[-1,7*7*128])
h_fc1=tf.nn.relu(tf.matmul(h_pool2_flat,w_fc1)+b_fc1)
keep_prob=tf.placeholder(tf.float32)
h_fc1_drop=tf.nn.dropout(h_fc1,keep_prob)
#第二层全连接层
w_fc2=weight_variable([3000,1024])
b_fc2=biase_variable([1024])
y_conv2=tf.matmul(h_fc1_drop,w_fc2)+b_fc2
#第三层全连接层
w_fc3=weight_variable([1024,10])
b_fc3=weight_variable([10])
y_conv=tf.matmul(y_conv2,w_fc3)+b_fc3
cross_entropy=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_,logits=y_conv))
train_step=tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
corrent_validation=tf.equal(tf.argmax(y_conv,1),tf.argmax(y_,1))
accuray=tf.reduce_mean(tf.cast(corrent_validation,dtype=tf.float32))
sess=tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
for i in range(2000):
    batch=mnist.train.next_batch(50)
    if i %100==0:
        train_accurcy=accuray.eval(feed_dict={x:batch[0],y_:batch[1],keep_prob:1.0})
        print("step %d,training accuracy is %g"%(i,train_accurcy))
    train_step.run(feed_dict={x:batch[0],y_:batch[1],keep_prob:0.5})
print("test accurcy %g"%accuray.eval(feed_dict={x:mnist.test.images,y_:mnist.test.labels,keep_prob:0.5}))

step 1400,training accuracy is 0.96
step 1500,training accuracy is 0.96
step 1600,training accuracy is 0.96
step 1700,training accuracy is 0.98
step 1800,training accuracy is 0.96
step 1900,training accuracy is 1
2018-10-16 17:00:04.703214:#########
2018-10-16 17:00:05.853831: ########
2018-10-16 17:00:07.134570: ########
test accurcy 0.957

可以看出在训练次数为2000次的情况下,最终正确率能够达到95.7%,而采用第一个模型正确率可以达到98%,。因此在模型的构建上应该采用合适的深度以及节点个数。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值