mnist运行遇到的几个问题

参考博客:
https://blog.csdn.net/qq_38269418/article/details/78991649

这篇博客写的很详细,包括模型训练、测试与ps制作手写字体。这里主要写几个在运行mnist时遇到的问题点。

1.优化器(Optimizer)的选择

可以参考https://blog.csdn.net/weixin_41417982/article/details/81561210
在mnist_deep.py中可以使用三种optimizer:

train_step = tf.train.GradientDescentOptimizer(1e-3).minimize(cross_entropy)
#train_step = tf.train.AdagradOptimizer(1e-4).minimize(cross_entropy)
#train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

若训练时使用GradientDescent,那么在test.py(测试自己制作的手写数字)中也应使用GradientDescent或者直接注掉

#train_step = tf.train.GradientDescentOptimizer(1e-3).minimize(cross_entropy)

若测试时使用了AdamOptimizer将会报以下错误:

NotFoundError (see above for traceback): Key Variable/Adam not found in checkpoint

若训练使用Adam测试时使用GradientDescent或者注掉都不影响。
使用GradientDescent方法生成的模型较小为12M左右,Adam模型大小为39M左右。

2.cpu 与 gpu切换:

就是sess的写法区别

cpu版本写法为;

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(20000):
        batch = mnist.train.next_batch(50)
        if i % 100 == 0:
            train_accuracy = accuracy.eval(feed_dict={
                x: batch[0], y_: batch[1], keep_prob: 1.0})
            print('step %d, training accuracy %g' % (i, train_accuracy))
        train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
    saver.save(sess, '/home/dengjie/dengjie/project/Mnist/mnist/deepmodel/model.ckpt') #模型储存位置

gpu版本为:

sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
for i in range(20000):
    batch = mnist.train.next_batch(50)
    if i % 100 == 0:
        train_accuracy = accuracy.eval(feed_dict={
            x: batch[0], y_: batch[1], keep_prob: 1.0})
        print('step %d, training accuracy %g' % (i, train_accuracy))
    train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
saver.save(sess, '/home/dengjie/dengjie/project/Mnist/mnist/deepmodel/model.ckpt') #模型储存位置

gpu版本tensorflow使用cpu代码时会报以下错误;

could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR

3.打印手写体矩阵形式

tva = [(255-x)*1.0/255.0 for x in tv] 
print(tva)

在这里插入图片描述
识别结果:
在这里插入图片描述
在这里插入图片描述

4.报错(自己记录)

File "Test.py", line 103, in <module>
    saver.restore(sess, "/home/dengjie/dengjie/project/Mnist/mnist/model/model.ckpt")
...
InvalidArgumentError (see above for traceback): Assign requires shapes of both tensors to match. lhs shape= [5,5,32,64] rhs shape= [5,5,1,32]

需要注掉mnist_deep.py中的以下代码

#W = tf.Variable(tf.zeros([784,10]))
#b = tf.Variable(tf.zeros([10]))

#y = tf.nn.softmax(tf.matmul(x,W) + b)

其他参考:
三维可视化理解卷积:
https://cs.ryerson.ca/~aharley/vis/conv/
项目地址及源码:
https://cs.ryerson.ca/~aharley/vis/
使用opencv进行手写数字图片预处理参考博客:
https://blog.csdn.net/sparta_117/article/details/66965760
tensorflow中文社区:
http://www.tensorfly.cn/tfdoc/tutorials/mnist_pros.html
tensorflow函数:
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-hdfb2xtv.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值