【问题标题】:can't concatenate two model of keras ValueError: Layer concatenate_16 was called with an input that isn't a symbolic tensor无法连接两个 keras ValueError 模型:使用不是符号张量的输入调用层 concatenate_16
【发布时间】:2019-03-20 20:46:53
【问题描述】:

我试图在 keras 中连接两个模型,但出现错误

这是两个模型

1. `image_model = Sequential([
    Dense(embedding_size, input_shape=(2048,), activation='relu'),
    RepeatVector(max_len)
])`
 2.` caption_model = Sequential([
    Embedding(vocab_size, embedding_size, input_length=max_len),
    LSTM(256, return_sequences=True),
    TimeDistributed(Dense(300))
])`

我的连接函数本身就是“连接”,因为我们不能在 keras-2.0 中使用合并,所以我使用了它。

   3. `final_model = Sequential([
    concatenate([image_model, caption_model],axis=-1),
    Bidirectional(LSTM(256, return_sequences=False)),
    Dense(vocab_size),
    Activation('softmax')
]) `

但这是我得到的错误解决方法,我已经用谷歌搜索了它,但没有一个解决方案对我有用。请帮忙

~/anaconda3/lib/python3.6/site-packages/keras/engine/base_layer.py in 
assert_input_compatibility(self, inputs)
278             try:
279                 K.is_keras_tensor(x)
280             except ValueError:
~/anaconda3/lib/python3.6/site- 
packages/keras/backend/tensorflow_backend.py in is_keras_tensor(x)
471         raise ValueError('Unexpectedly found an instance 
ofstr(type(x)) + '`. '
473                          'Expected a symbolic tensor instance.')

ValueError: Unexpectedly found an instance of type `<class 
'keras.engine.sequential.Sequential'>`. Expected a symbolic tensor 
 instance.
 During handling of the above exception, another exception occurred:
 ValueError                                Traceback (most recent call 
  last)
 <ipython-input-107-04c9412f6b6d> in <module>
  5 
  6 final_model = Sequential([
  7         concatenate([image_model, caption_model],axis=-1),
  8         Bidirectional(LSTM(256, return_sequences=False)),
  9         Dense(vocab_size),

  ~/anaconda3/lib/python3.6/site-packages/keras/layers/merge.py in 
  concatenate(inputs, axis, **kwargs)
  639         A tensor, the concatenation of the inputs alongside axis 
  `axis`.
   640     """
   --> 641     return Concatenate(axis=axis, **kwargs)(inputs)
   642 
    643 

   ~/anaconda3/lib/python3.6/site-packages/keras/engine/base_layer.py 
     in __call__(self, inputs, **kwargs)
    412                 # Raise exceptions in case the input is not 
    compatible
    413                 # with the input_spec specified in the layer 
    constructor.
    --> 414                 self.assert_input_compatibility(inputs)
    415 
    416                 # Collect input shapes to build layer.

    ~/anaconda3/lib/python3.6/site-packages/keras/engine/base_layer.py 
    in assert_input_compatibility(self, inputs)
    283                                  'Received type: ' +
   284                                  str(type(x)) + '. Full input: 
   ' +
  --> 285                                  str(inputs) + '. All inputs 
   to the layer '
286                                  'should be tensors.')
287 

   ValueError: Layer concatenate_16 was called with an input that 
  isn't a symbolic tensor. Received type: <class  
       keras.engine.sequential.Sequential'>. Full input: 
    [<keras.engine.sequential.Sequential object at 0x7f63ae5b8240>, 
    <keras.engine.sequential.Sequential object at 0x7f63ae592320>]. 
      All inputs to the layer should be tensors.

【问题讨论】:

  • @giser_yugang 但仍然收到错误 ValueError: Layer concatenate_21 was called with an input that is not a symbolic tensor。收到的类型:。完整输入:[, ]。该层的所有输入都应该是张量
  • Keras concatenate 层不支持该答案中所述的串联`顺序模型`类型。您应该按照答案并将输入更改为图层类型。
  • @giser_yugang 感谢您的回复。请帮忙,因为我一直在解决这个问题已经有一段时间了对我来说很多。
  • @giser_yugang 我正在处理 Flicker8k 数据集上的图像字幕问题,火车花了大约 1 小时 45 分钟进行编码,而在投入了这么长的时间后,测试编码花了大约 35 分钟,令人沮丧地重新开始所以请帮忙。

标签: python tensorflow keras deep-learning ubuntu-18.04


【解决方案1】:

正如我所说,keras concatenate 不支持 concate Sequential model 类型。您应该将您的 final_model 更改为 Keras functional Model。如下:

concat_layers = concatenate([image_model.output, caption_model.output])
layer = Bidirectional(LSTM(256, return_sequences=False))(concat_layers)
layer = Dense(vocab_size)(layer)
outlayer = Activation('softmax')(layer)
final_model = Model([image_model.input, caption_model.input], [outlayer])

【讨论】:

  • @AyushKushwaha 如果能解决您的问题,请接受答案。
猜你喜欢
  • 2019-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-29
  • 1970-01-01
  • 2018-06-21
  • 2020-11-30
  • 1970-01-01
相关资源
最近更新 更多