TensorFlow如何进行预测

4
我想使用以下代码进行预测计算:

```

我是代码

```

import tensorflow as tf

x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])

pred = multilayer_perceptron(x, weights, biases)


cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# Initializing the variables

##trn.txt start

##tst.txt end
with tf.Session() as sess:
    sess.run(init)

    # Training cycle
    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(num_lines_trn/batch_size)
        # Loop over all batches
        for i in range(total_batch):
            batch_x, batch_y = bat_x[i*batch_size:(i+1)*batch_size],bat_y[i*batch_size:(i+1)*batch_size]#mnist.train.next_batch(batch_size)
            # Run optimization op (backprop) and cost op (to get loss value)
            _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
                                                          y: batch_y})
            # Compute average loss
            avg_cost += c / total_batch

    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))

    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

    print(sess.run(accuracy, feed_dict={x: tst_x, y: tst_y}))
    print(sess.run(accuracy, feed_dict={x: tst_x}))

这行代码

print(sess.run(accuracy, feed_dict={x: tst_x, y: tst_y}))

返回的是批次的准确率,为0.80353

然而我想要获取预测结果,因此我添加了:

print(sess.run(accuracy, feed_dict={x: tst_x}))

但是这一行返回了一个错误:

您必须使用数据类型为float的占位符张量'Placeholder_7'提供值

我该如何解决这个问题?

1个回答

13

如果你想获得你的模型的预测结果,你需要执行以下操作:

sess.run(pred, feed_dict={x: tst_x})

您遇到了错误,因为您尝试运行sess.run(accuracy, feed_dict={x: tst_x}),但是要计算给定批次的准确度,您需要使用占位符y中包含的真实标签,因此您会收到以下错误:

必须提供占位符张量“占位符名称y”的值


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接