TensorFlow实战学习笔记2

来源:互联网 发布:cassandra数据库下载 编辑:程序博客网 时间:2024/06/07 21:44

我直接跳过了第四章来到卷积神经网络这里,书中给出了一个简单的两层神经网络。

用的笔记本训练的,显卡GTX960m,内存四个g

但运行书中代码后会出现

ResourceExhaustedError (see above for traceback): OOM when allocating tensor with shape[10000,32,28,28]
[[Node: Conv2D = Conv2D[T=DT_FLOAT, data_format="NHWC", padding="SAME", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/gpu:0"](Reshape, Variable/read)]]

的报错

这是因为在训练时我们把训练集打散了,每次只训练batch=50

但测试时把测试集一下子都输入进去系统内存不足

解决的办法是把最后的测试代码换成下面的

accuracy_sum = tf.reduce_sum(tf.cast(correct_prediction, tf.float32))good = 0total = 0for i in range(10):    testSet = mnist.test.next_batch(50)    good += accuracy_sum.eval(feed_dict={ x: testSet[0], y_: testSet[1], keep_prob: 1.0})    total += testSet[0].shape[0])
print("test accuracy %g"%(good/total))
只迭代了501次的结果,迭代20000次可以达到92%的正确率
另外,为了能看到最后一个整数的迭代结果,建议迭代的次数+1,例如你想迭代2万次,就将参数设置成20001,
这样就可以看到第2万次的结果,不然最后一次是不显示出来的
OK!


0 0
原创粉丝点击