TensorF验证和测试

2024-09-21 03:21:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我用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)

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

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)

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

谢谢你的帮助!


Tags: testimagesizelabelstfbatchtrainnn
1条回答
网友
1楼 · 发布于 2024-09-21 03:21:03

首先必须创建适当的验证/测试张量函数。对于一层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)))

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

然后必须创建一些验证/测试函数来测试准确性。最简单的方法是测试最有可能预测的类(大致是误分类错误)。在图形/会话之外定义此项。

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])

之后,您只需在同一个session/feed_dict中传递这个精度函数,就可以计算验证/测试分数。

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

相关问题 更多 >

    热门问题