【问题标题】:Tensorflow: InvalidArgumentError for placeholderTensorflow:占位符的 InvalidArgumentError
【发布时间】:2016-12-09 19:56:55
【问题描述】:

我尝试在 Jupyter Notebook 中运行以下代码,但是我得到了 InvalidArgumentErrorplaceholder

但是当我编写了一个 Python 脚本并在命令窗口中运行它时,它就起作用了。我想知道如何在 Notebook 中成功运行我的代码,谢谢。

  • 操作系统:Ubuntu 16.04 LTS
  • Tensorflow 版本:0.12rc(从源安装)

程序和输出:

命令窗口:

实际代码:

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)

【问题讨论】:

标签: tensorflow


【解决方案1】:

您的 raw_data 是 float64 (默认 numpy 浮点类型),而您的占位符是 float32 (默认 tensorflow 浮点类型)。您应该将数据显式转换为 float32

【讨论】:

  • 谢谢。我已经根据您的回答解决了这个问题。对了,我想知道为什么我可以在shell中成功运行我之前的程序?谢谢。
  • 也许shell中有一些东西将numpy默认设置为float32?
猜你喜欢
  • 2018-02-02
  • 2018-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
相关资源
最近更新 更多