![](/img/trans.png)
[英]How to extract the shape value of a placeholder Tensor in Tensorflow?
[英]tensorflow: how create an const tensor the same shape as a placeholder
我有一个用于输入的占位符:
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)
在另一个帖子tensorflow-constant-with-variable-size 中找到了答案
zero = tf.fill(tf.shape(Y), 0.0)
如果你想用零或一填充你的张量,你可以使用tf.zeros_like
和tf.ones_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)>
为什么不只是用与占位符相同的形状来构建常量,这样的事情应该可以工作。
zero = tf.constant(0, dtype=tf.float32, shape(none, n_outputs))
我认为是因为占位符尚未提供任何数据,这就是您收到错误的原因
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.