Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Merge layer (concat function) on Keras 2.0.0?

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.

like image 809
Pengju Zhao Avatar asked Mar 16 '17 01:03

Pengju Zhao


2 Answers

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])
like image 146
Autonomous Avatar answered Sep 28 '22 20:09

Autonomous


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.

like image 45
泡泡糖Lee Avatar answered Sep 28 '22 22:09

泡泡糖Lee