12

I am trying to reproduce the entity embedding models using Keras. Here is the github link and use the kaggle branch. There is one python file models.py and the Merge layer is used.

from keras.layers.core import Dense, Dropout, Activation, Merge, Reshape ......
self.model.add(Merge(models, mode='concat'))

This code should be fine for old-version of Keras, but using Keras 2.0.0 using tensorflow 1.0.0 as the backend (python 2.7), there will be wrong information: Using TensorFlow backend. Traceback (most recent call last): File "/Users/pengjuzhao/Udacity/MLND/entity-embedding-rossmann/test_model.py", line 2, in <module> from models import NN_with_EntityEmbedding File "/Users/pengjuzhao/Udacity/MLND/entity-embedding-rossmann/models.py", line 8, in <module> from keras.layers.core import Dense, Dropout, Activation, Merge, Reshape ImportError: cannot import name Merge [Finished in 1.8s with exit code 1] [shell_cmd: python -u "/Users/pengjuzhao/Udacity/MLND/entity-embedding-rossmann/test_model.py"] [dir: /Users/pengjuzhao/Udacity/MLND/entity-embedding-rossmann] [path: /usr/bin:/bin:/usr/sbin:/sbin]

Is there anyone who knows how to reach the same target(self.model.add(Merge(models, mode='concat')))or how to use the merge/Merge layer using Keras 2.0.0 ? Thank you in advance.

Pengju Zhao
  • 1,439
  • 3
  • 14
  • 17

2 Answers2

19

I think you are importing from the wrong location. You should do:

from keras.layers import Merge

See this Github post for more details on merge/Merge and how to use them.

From the same Github post, following two snippets of code are equivalent.

Keras 1.2.2 code:

from keras.engine import merge
m = merge([init, x], mode='sum')

Equivalent Keras 2.0.2 code:

from keras.layers import add
m = add([init, x])
Autonomous
  • 8,935
  • 1
  • 38
  • 77
  • 3
    Thank you for your answer, but there is something wrong with it. Here is the bug. /Users/pengjuzhao/anaconda/lib/python2.7/site-packages/keras/legacy/layers.py:66: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc. warnings.warn('The `Merge` layer is deprecated '`. It seems that the Merge layer has been removed from Keras 2.0. But the method `keras.layers.merge.concat` doesn't exist. – Pengju Zhao Mar 25 '17 at 14:10
  • have you tried using 'keras.layers.merge.Concatenate' ? – anakin Jul 17 '17 at 04:57
2

In Keras 2.0.4, "Merge" is not work, but "merge" is ok, the usage is as following:

from keras.layers import merge
m = merge([x1, x2], mod="cos", dot_axes=1)

the function code is not to implement concat function but to get the cosine value, the concat function is similar.

But this code in Keras 2.2.4 is not work, it will throw an error of " 'module' object is not callable", this is a problem caused by the Keras version.

  • 3
    So what is the solution for Keras 2.2.4 then? – quant Nov 15 '18 at 08:15
  • 1
    @quant, with keras 2.2, you have to use "concatenate", but the new version of Keras doesn't support the concatenation using the Sequential API, you have to use the Functional API. see: https://stackoverflow.com/questions/51075666/how-to-implement-merge-from-keras-layers – Minions Nov 22 '18 at 13:14
  • I'm new to Keras, how would I recreate the code below in keras 2.2.4? It isn't clear to me from the link provided above? `merge([target, context], mode='cos', dot_axes=0)` – Geoff Dec 17 '21 at 02:57