For example: Text1 [A,B,C] Text2 [V,D,Y]
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?mode = 'cos' merge layer is (1,1) matrix?Any opinions would be appreciated!
@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.
https://stackoverflow.com/questions/45108826/only-layers-of-same-output-shape-can-be-merged-using-sum-mode-layer-shapes?noredirect=1#comment77190517_45108826
I am trying to implement U-Net CNN with keras.
Most helpful comment
The best way to learn how Keras works is diving into code and write some codes to convince yourself.
Then you will get the output
For question 2: yes.
For question 3: In the code,
cosmode actually usesdot_axes. So, it depends on how you specify your argument.