Keras: Would someone explain the usage of concat_axis, dot_axis and output_shape in merge layer?

Created on 5 May 2016  路  7Comments  路  Source: keras-team/keras

For example: Text1 [A,B,C] Text2 [V,D,Y]

  1. In merge layer, if concat_axis = -1, it means [A,B,C] and [V,D,Y] transform to [A,B,C,V,D,Y] or [V,D,Y,A,B,C] ? What about dot_axis?
  2. How to set the output_shape in merge layer when using lambda or function?
    merged_vector2 = merge([encoded_a, encoded_b], mode=lambda x: 1 / (1 + l2_norm(x[0], x[1])),output_shape=lambda x: x[0])means that the output shape of merged_vector2 is as same as encoded_a?
  3. The output of mode = 'cos' merge layer is (1,1) matrix?

Any opinions would be appreciated!

stale

Most helpful comment

The best way to learn how Keras works is diving into code and write some codes to convince yourself.

from keras.layers import Input, merge
from keras.models import Model
import numpy as np

input_a = np.reshape([1, 2, 3], (1, 1, 3))
input_b = np.reshape([4, 5, 6], (1, 1, 3))

a = Input(shape=(1, 3))
b = Input(shape=(1, 3))

concat = merge([a, b], mode='concat', concat_axis=-1)
dot = merge([a, b], mode='dot', dot_axes=2)
cos = merge([a, b], mode='cos', dot_axes=2)

model_concat = Model(input=[a, b], output=concat)
model_dot = Model(input=[a, b], output=dot)
model_cos = Model(input=[a, b], output=cos)

print(model_concat.predict([input_a, input_b]))
print(model_dot.predict([input_a, input_b]))
print(model_cos.predict([input_a, input_b]))

Then you will get the output

[[[ 1.  2.  3.  4.  5.  6.]]]
[[[ 32.]]]
[[[[ 0.97463191]]]]

For question 2: yes.
For question 3: In the code, cos mode actually uses dot_axes. So, it depends on how you specify your argument.

All 7 comments

@nouiz @joelthchao @braingineer Would you give me an actual example to the usage above?

The best way to learn how Keras works is diving into code and write some codes to convince yourself.

from keras.layers import Input, merge
from keras.models import Model
import numpy as np

input_a = np.reshape([1, 2, 3], (1, 1, 3))
input_b = np.reshape([4, 5, 6], (1, 1, 3))

a = Input(shape=(1, 3))
b = Input(shape=(1, 3))

concat = merge([a, b], mode='concat', concat_axis=-1)
dot = merge([a, b], mode='dot', dot_axes=2)
cos = merge([a, b], mode='cos', dot_axes=2)

model_concat = Model(input=[a, b], output=concat)
model_dot = Model(input=[a, b], output=dot)
model_cos = Model(input=[a, b], output=cos)

print(model_concat.predict([input_a, input_b]))
print(model_dot.predict([input_a, input_b]))
print(model_cos.predict([input_a, input_b]))

Then you will get the output

[[[ 1.  2.  3.  4.  5.  6.]]]
[[[ 32.]]]
[[[[ 0.97463191]]]]

For question 2: yes.
For question 3: In the code, cos mode actually uses dot_axes. So, it depends on how you specify your argument.

Thanks very much for your reply!

@joelthchao I also feel little confused.
a is (none ,64,64,64) and b is (none,64,64,64)
the format is (none,nb_filter,weight,hight) in the image.

concat = merge([a, b], mode='concat', concat_axis=-1)

if concat_axis = -1 , a'hight plus b'hight.
we can get the output_shape which is (none ,64,64,64+64).
Does it right?
Thanks.

@joelthchao

a = Input(shape=(1, 3))
b = Input(shape=(1, 3))
a is (1,3),b is (1,3) , ndim is 2
dot = merge([a, b], mode='dot', dot_axes=2)
when the mode is dot and dot_axes = 2, i don't know why dot_axes is set 2 because a and b totally have the 2 ndim. [0,1]

Note that this example does not work with 2D arrays with shape: (batch_size, n_features). See: #5131.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LuCeHe picture LuCeHe  路  3Comments

NancyZxll picture NancyZxll  路  3Comments

vinayakumarr picture vinayakumarr  路  3Comments

zygmuntz picture zygmuntz  路  3Comments

harishkrishnav picture harishkrishnav  路  3Comments