2016-12-09 175 views
0

我試圖在Jupyter筆記本中運行以下代碼,但是我得到了InvalidArgumentErrorplaceholderTensorflow:佔位符InvalidArgumentError

但是,當我編寫一個Python腳本並在命令窗口中運行它時,它工作。我想知道如何成功地在筆記本中運行我的代碼,謝謝。

  • OS:Ubuntu的16.04 LTS
  • Tensorflow版本:0.12rc(從源安裝)

程序和輸出:

enter image description here

enter image description here

命令窗口:

enter image description here

實際活動代碼:

import tensorflow as tf 
import numpy as np 

raw_data = np.random.normal(10, 1, 100) 

# Define alpha as a constant 
alpha = tf.constant(0.05) 

# A placeholder is just like a variable, but the value is injected from the 
# session 
curr_value = tf.placeholder(tf.float32) 
# Initialize the previous average to some 

prev_avg = tf.Variable(0.) 
avg_hist = tf.summary.scalar("running_average", update_avg) 
value_hist = tf.summary.scalar("incoming_values", curr_value) 
merged = tf.summary.merge_all() 
writer = tf.summary.FileWriter("./logs") 
init = tf.global_variables_initializer() 

with tf.Session() as sess: 
    sess.run(init) 
    for i in range(len(raw_data)): 
    summary_str, curr_avg = sess.run([merged, update_avg], feed_dict={curr_value: raw_data[i]}) 
    sess.run(tf.assign(prev_avg, curr_avg)) 
    print(raw_data[i], curr_avg) 
    writer.add_summary(summary_str, i) 
+0

歡迎來到SO。請將實際的代碼文本放在您的問題中,而不是截圖。 http://stackoverflow.com/help/how-to-ask –

回答

1

raw_data是float64(默認NumPy的float型),而您的佔位符是FLOAT32(默認tensorflow float類型)。您應該明確地將您的數據轉換爲float32

+0

謝謝。我已根據您的答案解決了此問題。順便說一句,我想知道爲什麼我可以成功地在shell中運行我之前的程序?謝謝。 –

+0

也許theres在shell中設置numpy默認爲float32的東西? –

相關問題