2016-12-09 135 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的东西? –

相关问题