tensorflow - "Could not compute output"在 Tensorflow 2 中使用 tf.keras 合并层时出错

标签 tensorflow keras tensorflow2.0 tf.keras

我正在尝试在 tf.keras 中使用合并层,但得到 AssertionError: Could not compute output Tensor("concatenate_3/Identity:0", shape=(None, 10, 8), dtype=float32) .最小(非)工作示例:

import tensorflow as tf
import numpy as np

context_length = 10 

input_a = tf.keras.layers.Input((context_length, 4))
input_b = tf.keras.layers.Input((context_length, 4))

#output = tf.keras.layers.concatenate([input_a, input_b]) # same error
output = tf.keras.layers.Concatenate()([input_a, input_b])

model = tf.keras.Model(inputs = (input_a, input_b), outputs = output)

a = np.random.rand(3, context_length, 4).astype(np.float32)
b = np.random.rand(3, context_length, 4).astype(np.float32)

pred = model(a, b)

我在其他合并层(例如 add )中遇到相同的错误。我在 TF2.0.0-alpha0 上,但在 colab 上与 2.0.0-beta1 相同。

最佳答案

好吧,错误消息没有帮助,但我最终偶然发现了解决方案:输入到 model需要是一个可迭代的张量,即

pred = model((a, b))

工作得很好。

关于tensorflow - "Could not compute output"在 Tensorflow 2 中使用 tf.keras 合并层时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57434435/

相关文章:

python-3.x - 类型错误 : ('Keyword argument not understood:' , 'input' )

keras - keras.tokenize.text_to_sequences 和词嵌入有什么区别

tensorflow - torch 与tensorflow 的unsqueeze 等价物是什么?

python - 具有映射的稀疏张量的指数

tensorflow - 两个平行的 conv2d 层 (keras)

tensorflow 自定义操作梯度

python - 如何对张量的每一行/列使用 tf.unique_with_counts

python - 使用自定义 RNN 层说明自定义模型中缺少参数

python - Keras:屏蔽非 RNN 的零填充输入

python - Tensorflow 图像分类 Python 总是说相同的答案