python - Tensorflow:用动态计算值张量填充张量

标签 python tensorflow

计算了两个张量值,我尝试创建动态形状的张量。 E是张量变量的切片,labelLen_l是占位符,tensorval1和tensorval2是维度1的张量。

num1 = tf.reduce_sum(tf.eye(labelLen_l, dtype=tf.float64)*E, 1) num2 = tf.fill(num1.shape, tensor_val1) num3 = tf.fill(num1.shape, tensor_val2)

上面写着ValueError: Tried to convert 'dims' to a tensor and failed. Error: Cannot convert a partially known TensorShape to a Tensor: <unknown>

我正在尝试计算 num1 + num2 + num3,因此它们的尺寸应该匹配。有什么建议来实现它吗?

最佳答案

您可以使用 tf.shape 来获取张量类型的张量形状。

num2 = tf.fill(num1.shape, tensor_val1)

num3 = tf.fill(num1.shape, tensor_val2)

应该是:

num2 = tf.fill(tf.shape(num1), tensor_val1)
num3 = tf.fill(tf.shape(num1), tensor_val2)

关于python - Tensorflow:用动态计算值张量填充张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46707421/

相关文章:

Python:如何编写看起来不错并且可以与 argparse 或 optparse 一起使用的代码?

python - Tensorflow basic_rnn_seq2seq TypeError : Expected int32, 取而代之的是 'float' 类型的 -0.1

找不到 Python Keras 在 Windows 10 _gru_ops.dll 上的安装

python - Keras 在多个输出的多个损失中寻找最佳损失以进行反向传播

python - 分发 Ruby/Python 桌面应用程序

python - 获取与 TensorFlow 中特定张量相关的所有 tf​​.Variables

tensorflow - pipenv - 由于 'Could not find a version that matches keras-nightly~=2.5.0.dev' 错误,未生成 Pipfile.lock

python - 获取张量数组中的对张量对象

python - 使用自定义函数迭代 Pandas 中系列中的每个项目

python - pyinstaller:使用 matplotlib 构建 python exe