Python 使用 TensorFlow 进行验证和测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35387109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:23:13  来源:igfitidea点击:

Validation and Test with TensorFlow

pythonneural-networktensorflow

提问by FiReTiTi

I have created a one hidden layer neural network with a pyramidal architecture using TensorFlow. Here is the code:

我使用 TensorFlow 创建了一个具有金字塔结构的单隐藏层神经网络。这是代码:

num_classes = 10
image_size = 28

#Read the data
train_dataset, train_labels, valid_dataset, valid_labels, test_dataset, test_labels = OpenDataSets("...")
#Create and convert what is needed.
tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size))
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
tf_valid_dataset = tf.constant(valid_dataset)
tf_test_dataset = tf.constant(test_dataset)

#Then I create the NN.
Wh = tf.Variable(tf.truncated_normal([image_size * image_size, image_size * image_size / 2]))
bh = tf.Variable(tf.truncated_normal([image_size * image_size / 2]))
hidden = tf.nn.relu(tf.matmul(tf_train_dataset, Wh) + bh)

Wout = tf.Variable(tf.truncated_normal([image_size * image_size / 2, num_labels]))
bout = tf.Variable(tf.truncated_normal([num_labels]))
logits = tf.nn.relu(tf.matmul(hidden, Wout) + bout)

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels))
optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
train_prediction = tf.nn.softmax(logits)

And now I train my NN:

现在我训练我的神经网络:

with tf.Session(graph=graph) as session:
    tf.initialize_all_variables().run()
    for step in range(1000):
        offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
        batch_data = train_dataset[offset:(offset + batch_size), :]
        batch_labels = train_labels[offset:(offset + batch_size), :]
        feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}
        _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict)

Now I would like to validate and test my NN after training. But I don't know how to create the new feed_dict and use session.run in order to validate/test.

现在我想在训练后验证和测试我的神经网络。但我不知道如何创建新的 feed_dict 并使用 session.run 来验证/测试。

Thanks for your help!

谢谢你的帮助!

采纳答案by Sudeep Juvekar

You will first have to create appropriate validation/test tensor functions. For one-layer MPL, it involves nested multiply with weights and addition of biases (and also Relu's since you have them in your original model). Define these right below your train predictions

您首先必须创建适当的验证/测试张量函数。对于一层 MPL,它涉及与权重和偏差相加的嵌套乘法(还有 Relu,因为您的原始模型中有它们)。在您的火车预测下方定义这些

valid_prediction = tf.nn.softmax(
                      tf.nn.relu(tf.matmul(
                         tf.nn.relu(tf.matmul(tf_valid_dataset, Wh) + bh)), Wout) + bout)))
test_prediction = tf.nn.softmax(
                      tf.nn.relu(tf.matmul(
                         tf.nn.relu(tf.matmul(tf_test_dataset, Wh) + bh)), Wout) + bout)))

These expressions are in fact quite identical to logitvariable defined in your code, only using tf_valid_datasetand tf_test_datasetrespectively. You can create intermediate variables to simplify them.

这些表达式实际上与logit代码中定义的变量完全相同,仅分别使用tf_valid_datasettf_test_dataset。您可以创建中间变量来简化它们。

You will then have to create some validation/test function to test accuracy. Simplest would be to test most likely predicted class (Misclassification error roughly). Define this outside your graph/session.

然后,您必须创建一些验证/测试函数来测试准确性。最简单的方法是测试最可能的预测类(粗略地误分类错误)。在您的图表/会话之外定义它。

def accuracy(predictions, labels):
      pred_class = np.argmax(predictions, 1)
      true_class = np.argmax(labels, 1)
      return (100.0 * np.sum(pred_class == true_class) / predictions.shape[0])

After that, you can simply pass this accuracy function inside same session/feed_dict to compute validation/test score.

之后,您可以简单地在同一个 session/feed_dict 中传递这个准确度函数来计算验证/测试分数。

print 'Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels)
print 'Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels)