Python tensorflow:如何创建与占位符形状相同的常量张量

Python tensorflow:如何创建与占位符形状相同的常量张量,python,tensorflow,tensor,Python,Tensorflow,Tensor,我有一个占位符输入: Y = tf.placeholder(dtype=tf.float32, shape=(None, n_outputs)) 现在,我想创建一个与Y形状相同的常数: w = Y.get_shape() zero = tf.constant(np.zeros(w), dtype=tf.float32) 错误返回: __index__ returned non-int (type NoneType) 为什么不用与占位符相同的形状来构建常量呢,类似的东西应该可以工作 zer

我有一个占位符输入:

Y = tf.placeholder(dtype=tf.float32, shape=(None, n_outputs))
现在,我想创建一个与Y形状相同的常数:

w = Y.get_shape()
zero = tf.constant(np.zeros(w), dtype=tf.float32)
错误返回:

__index__ returned non-int (type NoneType)

为什么不用与占位符相同的形状来构建常量呢,类似的东西应该可以工作

zero = tf.constant(0, dtype=tf.float32, shape(none, n_outputs))

我认为因为占位符没有被输入任何数据,所以你会得到一个错误

在另一篇文章中找到了答案


如果你想用0或1填充张量,你可以使用
tf.zeros\u like
tf.ones\u like
方法作为
tf.fill
的简写

a = tf.constant([0, 1, 2, 3, 4])

a_zeros = tf.zeros_like(a)
a_zeros
>>> <tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 0, 0, 0, 0], dtype=int32)>
a=tf.常数([0,1,2,3,4])
a_zeros=tf.zeros_like(a)
a_零
>>> 

我尝试了你的想法:zero=tf.constant(0,dtype=tf.float32,shape=(None,n_输出)),这里有一个错误“int()参数必须是字符串、类似字节的对象或数字,而不是‘NoneType’。去掉None可能只有shape(n_输出)
a = tf.constant([0, 1, 2, 3, 4])

a_zeros = tf.zeros_like(a)
a_zeros
>>> <tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 0, 0, 0, 0], dtype=int32)>