我想使用以下代码计算预测:
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}))
Run Code Online (Sandbox Code Playgroud)
这条线
print(sess.run(accuracy, feed_dict={x: tst_x, y: tst_y}))
Run Code Online (Sandbox Code Playgroud)
返回0.80353哪个是批次的准确性.
但是我想得到预测结果.所以我补充说:
print(sess.run(accuracy, feed_dict={x: tst_x}))
Run Code Online (Sandbox Code Playgroud)
但是这一行返回一个错误:
您必须使用dtype float为占位符张量'Placeholder_7'提供值
我怎么解决这个问题?
Oli*_*rot 11
如果您想获得模型的预测,您应该:
sess.run(pred, feed_dict={x: tst_x})
Run Code Online (Sandbox Code Playgroud)
您有一个错误,因为您尝试运行sess.run(accuracy, feed_dict={x: tst_x}),但要计算给定批次的准确性,您需要占位符中包含的真实标签y,因此您会收到以下错误:
您必须为占位符张量"占位符名称
y"提供值
| 归档时间: |
|
| 查看次数: |
8882 次 |
| 最近记录: |