2017-02-25 55 views
3

好吧,我通過預測Tensorflow中特定產品質量是好還是壞的樣本。我的代碼最後一段是這樣的:如何打印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 

的問題是,現在,我想從(大概)在短短的1行數據的反饋一個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

這取決於你的圖形。如果你檢查你的accuracy節點,它可能看起來像

tf.reduce_mean(tf.equal(my_prediction, correct_label)) 

其中correct_label將一些涉及到y_train, 和my_prediction將一些TF節點,可能看起來像tf.round(...)取決於您的實現。

你在這裏做什麼是要找到my_prediction出來,那麼你可以使用得到的預測

output = sess.run([my_prediction], feed_dict={X: [input]})