首页 > 解决方案 > 您必须使用 dtype float 和 shape [2,2] 为占位符张量“Placeholder”提供一个值

问题描述

我有错误。我You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [2,2]已经喂了占位符aa

import numpy as np
import tensorflow as tf

aa = tf.placeholder(dtype=tf.float32, shape=(2, 2))
bb = tf.Variable(aa)
init = tf.global_variables_initializer()
with tf.Session() as sess:
  sess.run(init)
  print(sess.run(bb,feed_dict={aa:np.random.rand(2,2)}))

标签: pythontensorflow

解决方案


问题出在sess.run(init);你需要一个值aa来初始化bb. aa但是,以后不需要检索bb,因为它已经被分配了一个值。

import numpy as np
import tensorflow as tf

aa = tf.placeholder(dtype=tf.float32, shape=(2, 2))
bb = tf.Variable(aa)
init = tf.global_variables_initializer()
with tf.Session() as sess:
  sess.run(init, feed_dict={aa: np.random.rand(2, 2)})
  print(sess.run(bb))

推荐阅读