10

I have try run a code but I find a problem with merge layers of Keras. I'm using python 3 and keras 2.2.4

This is de code part of code


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!")

This is the message of error

from keras.layers import LSTM, Embedding, TimeDistributed, Dense, RepeatVector, Merge, Activation
ImportError: cannot import name 'Merge' from 'keras.layers'
Omkar
  • 3,253
  • 3
  • 20
  • 36
JSouza
  • 115
  • 1
  • 1
  • 7

1 Answers1

12

Merge is not supported in Keras +2. Instead, you need to use Concatenate layer:

merged = Concatenate()([x1, x2]) # NOTE: the layer is first constructed and then it's called on its input

or it's equivalent functional interface concatenate (starting with lowercase c):

merged = concatenate([x1,x2]) # NOTE: the input of layer is passed as an argument, hence named *functional interface*

If you are interested in other forms of merging, e.g. addition, subtration, etc., then you can use the relevant layers. See the documentation for a comprehensive list of merge layers.

today
  • 32,602
  • 8
  • 95
  • 115
  • Thanks for this answer. It almost worked for me except that when I applied it this way: ```model.add(Concatenate([word_model, context_model], mode="dot"))``` , I got the following error: ```TypeError: ('Keyword argument not understood:', 'mode')``` Any thoughts on a solution? – nigus21 Feb 05 '20 at 23:31
  • @nigus21 There are two problems with your code (probably only the second one is relevant for your needs): 1) You are passing the input of layer as its arguments (i.e. pay attention to using *concatenate* big "C" or small "c", and their difference as I explained in my answer). 2) `mode` no longer works in Keras 2+. Use the appropriate [merge layers](https://keras.io/layers/merge/) instead (in your case, it's probably the `Dot` layer). – today Feb 06 '20 at 00:02