Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does axes parameter do in dot layer in keras?

Which values are exactly being multipled when we specify axes = (2, 2)? Can anyone tell me what really is happening behind the scenes here?

match = dot([input_encoded_m, question_encoded], axes=(2, 2))
match = Activation('softmax')(match)

Data shapes:

print(input_encoded_m)
print(questions_encoded)

(<tf.Tensor 'dropout_41/Identity:0' shape=(None, 552, 64) dtype=float32>,
 <tf.Tensor 'dropout_42/Identity:0' shape=(None, 5, 64) dtype=float32>)

input_encoded_m, 552 is sentence length, 64 is embeddings length

questions_encoded, 5 is sentence length, 64 is embeddings length

If we specify axes = (2, 2) in dot layer which values are being multiplied? can anyone help me with it?

like image 881
user_12 Avatar asked Oct 19 '25 17:10

user_12


1 Answers

Axes mean the axis of your tensors.

For examples, in your case, you have a tensor of shape=(None,552,64) which is 3D(rank 3) tensor.

A scalar(e.g 3) is 0D tensor.

A vector ([1,2,3]) is 1D tensor:

A matrix ([ is 2D tensor. and so on.

       [1,2],
       [2,3],
        ]

the first axis( axis 0) is the one that has None.

The second axis( axis 1 ) is the one that has 522 rows.

The third axis ( axis 2) is the one that has 64 columns.

a = Input(batch_shape=(None,255,64))
b = Input(batch_shape=(None,5,64))
out = dot([a,b], axes =(2,2))
out.shape
TensorShape([Dimension(None), Dimension(255), Dimension(5)])

So basically, a.b = a1.b1 + a2.b2 +.... + a64.b64 will give one scalar. Since you have a row of 5, you will have a vector of 5 dimensions in the last axis of your tensor.(vector Dimension is different from tensor Dimension

like image 177
Eliethesaiyan Avatar answered Oct 21 '25 07:10

Eliethesaiyan