【发布时间】:2017-08-01 21:14:54
【问题描述】:
我正在使用 tensorflow 对 MNIST 数据集进行图像识别。在每个训练 epoch 中,我随机选取 10,000 张图像,以 1 的批大小进行在线训练。前几个 epoch 的识别率提高了,但是在几个 epoch 之后,识别率开始大幅下降。 (在前 20 个 epoch 中,识别率上升到 ~94%。之后,识别率从 90->50->40->30->20 上升)。这是什么原因?
此外,批量大小为 1 时,性能比批量大小为 100 时更差(最大识别率 94% 对 96%)。我查看了几个参考资料,但对于小批量还是大批量是否能获得更好的性能似乎存在矛盾的结果。在这种情况下会是什么情况?
编辑:我还加了一张训练数据集和测试数据集的识别率图。Recognition rate vs. epoch
我附上了下面的代码副本。感谢您的帮助!
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot = True)
#parameters
n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500
n_classes = 10
batch_size = 1
x = tf.placeholder('float', [None, 784])
y = tf.placeholder('float')
#model of neural network
def neural_network_model(data):
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([784, n_nodes_hl1]) , name='l1_w'),
'biases': tf.Variable(tf.random_normal([n_nodes_hl1]) , name='l1_b')}
hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2]) , name='l2_w'),
'biases' :tf.Variable(tf.random_normal([n_nodes_hl2]) , name='l2_b')}
hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3]) , name='l3_w'),
'biases' :tf.Variable(tf.random_normal([n_nodes_hl3]) , name='l3_b')}
output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes]) , name='lo_w'),
'biases' :tf.Variable(tf.random_normal([n_classes]) , name='lo_b')}
l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']), hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']), hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2,hidden_3_layer['weights']), hidden_3_layer['biases'])
l3 = tf.nn.relu(l3)
output = tf.matmul(l3,output_layer['weights']) + output_layer['biases']
return output
#train neural network
def train_neural_network(x):
prediction = neural_network_model(x)
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
optimizer = tf.train.AdamOptimizer().minimize(cost)
hm_epoches = 100
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epoches):
epoch_loss=0
for batch in range (10000):
epoch_x, epoch_y=mnist.train.next_batch(batch_size)
_,c =sess.run([optimizer, cost], feed_dict = {x:epoch_x, y:epoch_y})
epoch_loss += c
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
print(epoch_loss)
print('Accuracy_test:', accuracy.eval({x:mnist.test.images, y:mnist.test.labels}))
print('Accuracy_train:', accuracy.eval({x:mnist.train.images, y:mnist.train.labels}))
train_neural_network(x)
【问题讨论】:
-
训练误差线和测试误差线如此紧密地相互跟踪,这很奇怪;你确定这些集合不重叠吗?
-
@Ray 是的,根据tensorflow函数(mnist.train.next_batch),只挑选训练集中的数据。我认为这种现象的主要原因(训练数据集识别率紧跟测试数据集)是对于每个时期,我不使用整个训练数据集进行训练,而是使用训练集的一个子集(10k 个随机数据点) .使用相同的数据集进行训练和测试会产生什么结果?会不会导致这种认可度下降?
-
不,不会导致识别率下降;它只会使结果变得毫无用处,因为如果您不需要泛化到看不见的数据,那么在训练集上获得 100% 的识别是微不足道的(对于某些微不足道的值)。
标签: python machine-learning tensorflow mnist