无法从“ keras.layers”导入名称“ Merge”

时间:2019-05-26 17:12:55

标签: python keras keras-layer

我尝试运行代码,但是发现Keras的合并层存在问题。我正在使用python 3和keras 2.2.4

这是代码的解码部分


import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import LSTM, Embedding, TimeDistributed, Dense, RepeatVector, Merge, Activation
from keras.preprocessing import image, sequence
import cPickle as pickle


    def create_model(self, ret_model = False):

        image_model = Sequential()
        image_model.add(Dense(EMBEDDING_DIM, input_dim = 4096, activation='relu'))
        image_model.add(RepeatVector(self.max_length))

        lang_model = Sequential()
        lang_model.add(Embedding(self.vocab_size, 256, input_length=self.max_length))
        lang_model.add(LSTM(256,return_sequences=True))
        lang_model.add(TimeDistributed(Dense(EMBEDDING_DIM)))

        model = Sequential()
        model.add(Merge([image_model, lang_model], mode='concat'))
        model.add(LSTM(1000,return_sequences=False))
        model.add(Dense(self.vocab_size))
        model.add(Activation('softmax'))

        print ("Model created!")

这是错误消息

from keras.layers import LSTM, Embedding, TimeDistributed, Dense, RepeatVector, Merge, Activation
ImportError: cannot import name 'Merge' from 'keras.layers'

1 个答案:

答案 0 :(得分:0)

Keras +2不支持

Merge。相反,您需要使用Concatenate层:

merged = Concatenate()([x1, x2])

或等效的功能接口concatenate(以小写的c开头):

merged = concatenate([x1,x2])

如果您对其他合并形式感兴趣,例如加法,减法等,则可以使用相关图层。有关合并图层的详细列表,请参见documentation