学习tensorflow,自然是会想要先从mnist开始,简单看了官方教程之后,我选择了这篇博客文章follow一下:
https://my.oschina.net/u/876354/blog/1926060
训练不收敛问题
而在follow这篇文章代码的时候,遇到了训练不收敛的情况,并且在训练迭代的最后,会提示内存不够用了,大部分报错附在文末。网上很多提到内存申请错误的帖子,都说是batch设置太大了,不过这个明显不是这个问题,因为是在最后内存不够了,而且之前迭代过程中的training accuracy 明显没有向1收敛,很明显是死循环了(当然,期间排错的时候,我也尝试了,把batch size设置成更小,也不行的;而且迭代次数只设置到100次,还是会报相同的错误)。
所以,对于不收敛的问题,应该还是在于前面模型网络的定义里面。
最后,经过细心的review,我发现,问题出在熵函数定义里面,函数前面的负号被漏掉了(这意味着,如果你最小化这个错误的函数,本来就是在朝着training accuracy变小的方向走)。虽然是自己敲代码留的坑,但是这个排错过程中的痛苦,还是磨砺了我的耐心。
# define cross entropy function
# y_ is true label
cross_entropy = tf.reduce_mean(tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
修改为如下,training accuracy就可以正常收敛了:
cross_entropy = -tf.reduce_mean(tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
另外的问题和另外的尝试:
在可以收敛以后,问题并没有完全解决,在迭代的最后,内存问题仍然出现。
step 9300, training accuracy 0.96
step 9400, training accuracy 0.94
step 9500, training accuracy 1
step 9600, training accuracy 0.98
step 9700, training accuracy 0.94
step 9800, training accuracy 0.92
step 9900, training accuracy 0.94
Optimization Finished!
2020-07-05 20:48:15.217061: W tensorflow/core/common_runtime/bfc_allocator.cc:273] Allocator (GPU_0_bfc) ran out of memory trying to allocate 957.03MiB. Current allocation summary follows.
2020-07-05 20:48:15.217103: I tensorflow/core/common_runtime/bfc_allocator.cc:643] Bin (256): Total Chunks: 0, Chunks in use: 0 0B allocated for chunks. 0B client-requested for chunks. 0B in use in bin. 0B client-requested in use in bin.
另外,我使用官方教程follow使用intersession方式的代码出来,可以很快收敛,但是也会在迭代的最后,报出内存的问题(不太一样的是,calle