评估预测极其缓慢

时间:2017-11-01 21:33:55

标签: python-3.x numpy machine-learning tensorflow conv-neural-network

我正在使用以下占位符培训CNN ......

reloadData()

我在一个会话中训练它,因为我必须在每次将数据提供给占位符之前重新整形数据,这里大致是我正在做的...

# mnist data image of shape [batch_size, 28, 28, 1].
x = tf.placeholder(tf.float32, [None, 28, 28, 1])

# 0-9 digits recognition => 10 classes.
y = tf.placeholder(tf.float32, [None, 10])

正如您所看到的,我在将数据提供给占位符之前正在重新整理数据。 CNN似乎工作正常,但是当我尝试计算预测的准确性时出现问题。我使用softmax张量和我的with tf.Session() as sess: for epoch in range(25): total_batch = int(features.train.num_examples/500) avg_cost = 0 for i in range(total_batch): batch_xs, batch_ys = features.train.next_batch(10) _, c = sess.run([train_op, loss], feed_dict={x:batch_xs.reshape([-1, 28, 28, 1]), y:batch_ys}) ... ... 图层预测它...

logits

...所以在完成所有训练之后,这就是我计算预测的方法......

# Logits Layer.
# Create a dense layer with 10 neurons => 10 classes
# Output has a shape of [batch_size, 10]
logits = tf.layers.dense(inputs=dropout, units=10)

# Softmax layer for deriving probabilities.
pred = tf.nn.softmax(logits, name="softmax_tensor")

不幸的是,我必须将每个from tensorflow.examples.tutorials.mnist import input_data ... ... features = input_data.read_data_sets("/tmp/data/", one_hot=True) ... ... # list of booleans to determine the correct predictions correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) print(correct_prediction.eval({x:features.test.images.reshape([10000, 28, 28, 1]), y:features.test.labels})) 数据转换为正确的格式,因为占位符features只接受格式x。有10,000个图像和标签。当我这样做时,我的计算机速度非常慢,并且使用了大量计算,我每次都需要重新启动计算机。显然将它们转换为这种格式并不是非常友好。

有什么更好的方法可以避免我的计算机崩溃?我已经将以下代码用于其他神经网络...

[batch_size, 28, 28, 1]

...但我从来没有遇到这个问题让我相信原因源于print(correct_prediction.eval({x:features.test.images, y:features.test.labels})) 功能。

1 个答案:

答案 0 :(得分:1)

我可能需要查看整个脚本,但是从提供的代码片段来看,我认为问题在于您正在推动整个测试集进行评估。

请注意,它不仅为输入张量及其池化变换分配10000*28*28*4(30Mb),卷积层在整个批次(仅第一层)中执行大约10000*25*25*1*filter_size个卷积。它的计算成本非常高,而这很可能是整个时间,而不是reshape

您应该批量执行测试评估。这是简化的代码,需要更加谨慎地使用任意batch_size

正确计算它
total_accuracy = 0.0
for batch_xs, batch_ys in iterate_batches(features.test, batch_size):
  accuracy = correct_prediction.eval({x: batch_xs.reshape([-1, 28, 28, 1]), y: batch_ys})
  total_accuracy += np.mean(accuracy)
total_batches = features.test.num_examples / batch_size
total_accuracy /= total_batches

但它适用于batch_size=10batch_size=100

相关问题