好的,我通过一个样本来预测特定产品质量在Tensorflow上是好还是坏。我的最后一段代码如下所示:
# Merge summaries for TensorBoard
merged_summaries = tf.summary.merge_all()
with tf.Session() as sess:
log_directory = create_log_directory()
summary_writer = tf.summary.FileWriter(log_directory, sess.graph)
tf.global_variables_initializer().run()
for i in range(epochs):
average_cost = 0
number_of_batches = int(len(X_train) / batch_size)
for start, end in zip(range(0, len(X_train), batch_size), range(batch_size, len(X_train), batch_size)):
feed = {X: X_train[start:end], y: y_train[start:end]}
sess.run(training_step, feed_dict=feed)
# Compute average loss
average_cost += sess.run(cost, feed_dict=feed) / number_of_batches
if i % epochs_to_print == 0:
feed = {X: X_test, y: y_test}
result = sess.run([merged_summaries, accuracy], feed_dict=feed)
summary = result[0]
current_accuracy = result[1]
summary_writer.add_summary(summary, i)
print("Epoch: {:4d}, average cost = {:.3f}, accuracy = {:.3f}".format(i+1, average_cost, current_accuracy))
print("Final accuracy = {:.3f}".format(sess.run(accuracy, feed_dict={X: X_test, y: y_test})))
它推出了一套很好的10个时期,最高精度为0.527,我认为准确率为52.7%。
Saving summaries to tmp/logs/run_32/
Epoch: 1, average cost = 3.300, accuracy = 0.174
Epoch: 101, average cost = 0.685, accuracy = 0.528
Epoch: 201, average cost = 0.682, accuracy = 0.527
Epoch: 301, average cost = 0.680, accuracy = 0.527
Epoch: 401, average cost = 0.680, accuracy = 0.527
Epoch: 501, average cost = 0.679, accuracy = 0.527
Epoch: 601, average cost = 0.679, accuracy = 0.527
Epoch: 701, average cost = 0.679, accuracy = 0.527
Epoch: 801, average cost = 0.679, accuracy = 0.527
Epoch: 901, average cost = 0.679, accuracy = 0.527
Final accuracy = 0.527
问题在于,现在,我想从(可能)一个numpy数组中仅将一行数据反馈到Tensorflow中以获得预测。我该怎么做呢?我假设它遵循这样的模式:
input =[1.939501945438227,-1.8459679631200792,1.9134581818982566,-0.6741964131111666,-0.5720868389043996,0.3926397708073837,-2.0777995164924112,0.03405362776450469,0.33621509508483066]
output = <<some function call here>>
print(output)
答案 0 :(得分:0)
这取决于您的图表。如果您检查accuracy
节点,它可能看起来像
tf.reduce_mean(tf.equal(my_prediction, correct_label))
其中correct_label
与y_train
有关,
并且my_prediction
将是某个节点,可能看起来像tf.round(...)
取决于您的实现。
你在这里要做的是找到my_prediction
,然后你可以使用
output = sess.run([my_prediction], feed_dict={X: [input]})