python - 单维 tensorflow 占位符

标签 python numpy tensorflow

我在启动这个非常简单的 tensorflow 代码时遇到了困难。我正在尝试进行 y = theta1 * x + theta2 形式的简单直线拟合

我将 x 和 y 的数据创建为形状 [10] 的 numpy float32 数组,我创建了它们相应的占位符,如下所示:

tf_x = tf.placeholder(tf.float32, [10])
tf_y = tf.placeholder(tf.float32, [10])

我按如下方式喂养它们:

sess.run(train, feed_dict={tf_x: x_data, tf_y: y_data})

完整的代码有点长,所以我创建了一个要点: https://gist.github.com/meowmiau/369393f41b679dd95f4ac4e2e16b0782

我遇到的问题是这样的:

tensorflow.python.framework.errors.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [10]
 [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[10], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

但是,据我所知,没有不匹配的情况。

最佳答案

在您的代码中尝试进行此修改:

for i in range(1000):
  x_data, y_data = gen_data()
  _, e = sess.run([train, err], feed_dict={tf_x: x_data, tf_y: y_data})
  print e

@孙萌:

我认为将列表 [train, err] 传递给 sess.run() 是一个可能的解决方案。 以下代码片段的工作方式相同:

for i in range(1000):
  x_data, y_data = gen_data()
  feed_dict={tf_x: x_data, tf_y: y_data}
  print(sess.run(err, feed_dict=feed_dict))
  sess.run(train, feed_dict=feed_dict)

在您的代码中,两个占位符抛出错误“您必须为占位符张量提供一个值” 因为 sess.run(err) 是在没有 feed 的情况下执行的。

关于python - 单维 tensorflow 占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41406518/

相关文章:

python - 由于最新的python版本保留了dict的插入顺序,相等(==)的含义会改变吗?

python - django 在 View 或模型中获取媒体目录

python - 使用索引对更新 2D numpy 数组而不用迭代

machine-learning - SVM tensorflow 实现

tensorflow - "Flex Op"在 Tensorflow 中是什么意思?

python - 如何让多个处理程序使用日志?

python - 如何使用 Popen 同时写入标准输出和记录文件?

python - 值错误: setting an array element with a sequence when using feed_dict in TensorFlow

python - 一个函数需要什么才能使用堆叠的 NumPy 参数?

python - 为 TensorFlow reshape Gym 数组