1

Keras 2.0 has removed keras.layers.merge and now we should use keras.layers.Concatenate,

I was wonder what is the equivalent to having the 'cos' and 'dot_axis=0' arg, for example

similarity = keras.layers.merge([target, context], mode='cos', dot_axes=0)

How would I write that in keras 2.0?

SantoshGupta7
  • 5,607
  • 14
  • 58
  • 116
  • What does the `mode='cos'` do? – today Jan 13 '19 at 01:38
  • it's supposed to make it a cos similarity function https://faroit.github.io/keras-docs/1.2.2/layers/core/#merge – SantoshGupta7 Jan 13 '19 at 01:48
  • Look at [this answer](https://stackoverflow.com/a/52021481/2099607). You can use `Dot` layer with `normalize` argument set to `True`. – today Jan 13 '19 at 02:15
  • Possible duplicate of [Computing cosine similarity between two tensors in Keras](https://stackoverflow.com/questions/51003027/computing-cosine-similarity-between-two-tensors-in-keras) – Amir Jan 13 '19 at 09:09

2 Answers2

3

I tried with:

similarity = dot([target, context], axes=1, normalize=True)

Stefs
  • 46
  • 2
  • Changes to your question should not go into an answer. Rather, edit your question directly. Answers are strictly for answering. – Mike Jun 19 '19 at 22:03
0

How Can i convert below old from version 1+ code to new keras version 2.3.0. you provided above solution but if i need to use outputshape the how can i do this.

from keras.layers import merge

merge(inputs=[question_enc_1, answer_enc_1], mode=similarity, output_shape=lambda _: (None, 1))

till i now i converted above to

from keras import backend as K

K.dot([question_pool, answer_pool], output_shape=lambda _: (None, 1))

Click To see Screenshot of code line that shows unexpected arguments are there i think here it doesn't accept output shape param.

Jaimil Patel
  • 1,301
  • 6
  • 13
Krishna
  • 1
  • 1
  • please don't ask question in an answer. use a different question for your question. you can add a link to this question – Ophir Carmi May 07 '20 at 06:46