python - tensorflow :如何创建一个与占位符形状相同的常量张量

标签 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)

最佳答案

在另一篇文章中找到了答案 tensorflow-constant-with-variable-size

zero = tf.fill(tf.shape(Y), 0.0)

关于python - tensorflow :如何创建一个与占位符形状相同的常量张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47674588/

相关文章:

python - 使用 Python 迭代时出现 StaleElementException

python - Flask - WTForm - 将表单数据获取为 NoneType 而不是 String

tensorflow - AttributeError : module 'tensorflow_core._api.v2.config' has no attribute 'list_physical_devices'

python - Torch 求和张量子集

python - ValueError : base_distribution needs to have shape with size at least 6, 但得到了 torch.Size([6])

python - 如何从新 Lego Mindstorms Robot Inventor 上的自定义 Python 模块导入

python - SciPy:从 PMF 生成自定义随机变量

python - 沿着张量的第二维收集元素

python - Keras、TorchVision 中的预训练模型

python - 选择数组中向量的分量