无法串联两个keras ValueError模型:层concatenate_16的输入不是符号张量

时间:2019-03-20 20:46:54

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

我正在尝试在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中使用merge,所以我使用了它。

   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.

1 个答案:

答案 0 :(得分:1)

正如我所说,keras concatenate不支持串联Sequential model类型。您应该将final_model更改为[Python 3]: sorted(iterable, *, key=None, reverse=False)。如下:

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])