无法连接两个 keras 模型 值错误:使用 不是符号张量的输入调用了第 concatenate_16 层



我试图在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.

正如我所说的keras concatenate不支持Sequential model类型。您应该将final_model更改为KERAS功能模型。如下:

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

相关内容

最新更新