【问题标题】:Layer concatenate was called with an input that isn't a symbolic tensor使用不是符号张量的输入调用层连接
【发布时间】:2020-05-07 15:16:16
【问题描述】:

我有两个结构相似的模型 (model_a,model_b)(VGG16 架构,顶部块被替换)。我需要连接两个模型最后一层的输出,以便作为输入发送到注意力机制。

我运行以下代码行进行连接:

merged = Concatenate()([model_a.layers[-1].layers[-1], model_b.layers[-1].layers[-1]])

model_a.layers[-1] 是顶层块,是一个 Sequential 对象,model_a.layers[-1].layers[-1] 是一个密集层。)

但是,当我尝试这样做时收到以下错误:

层 concatenate_8 被调用,输入不是符号 张量。接收类型:。全输入: [, ]。所有输入到 层应该是张量。

我注意到 similar issues 是通过指定输入层来重新定义最后一层来修复的,但我不确定该解决方案在这里有什么帮助,因为我使用的是预定义和预训练的模型。

【问题讨论】:

  • 试试model_a.layers[-1].layers[-1].outputoutput 属性是符号张量(该层的输出)
  • @jakub 非常感谢!如果您可以添加评论作为答案,我可以接受它作为正确的答案:)
  • @jakub 实际上,当我尝试使用 Model() 方法最终组合输入和输出时,我遇到了一个错误 - 'Graph disconnected: Cannot get value of tensor Tensor Flatten..' ,但是当我将 model_a.layers[-1].layers[-1].output 更改为 model_a.output,同样对于 model_b,问题已解决。知道为什么会这样吗?

标签: python tensorflow keras deep-learning


【解决方案1】:

使用模型的.output 属性访问符号张量。

merged = Concatenate()(
    [model_a.layers[-1].layers[-1].output, model_b.layers[-1].layers[-1].output])

tensorflow.keras.layers.Layer documentation 声明以下关于Layer.output

输出:检索层的输出张量。

仅当层只有一个输出时才适用,即如果它连接到一个传入层。

关于评论

实际上,当我尝试使用 Model() 方法最终组合输入和输出时,我遇到了一个错误 - Graph disconnected: Cannot get value of tensor Tensor Flatten..,但是当我将 model_a.layers[-1].layers[-1].output 更改为 model_a.output 并且对于 model_b 也是如此,问题已解决。知道为什么吗?

不看型号代码很难说,但你可以比较model_a.layers[-1].layers[-1]model_a.output的值。

【讨论】:

  • model_a.layers[-1].layers[-1] is ; model_a.output 是
猜你喜欢
  • 2018-05-29
  • 1970-01-01
  • 2019-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 2020-11-30
相关资源
最近更新 更多