youngtackpark
youngtackpark

Reputation: 1635

Keras Dot Axes questions

I am trying to use Keras Dot and have the following errors. Could you explain what I am doing wrong?

x1 = Input(shape=(2,4))
x2 = Input(shape=(4,))
y1 = dot([x1,x2], axes = (2,1))
modelA = Model(inputs=[x1, x2], outputs=y1)

a1 = np.arange(16).reshape(2,2,4)
a2 = np.array( [1,2,3,4] )

modelA.predict([a1,a2])

---->
ValueError: Error when checking : expected input_40 to have shape (None, 4) but 
got array with shape (4, 1)

Upvotes: 0

Views: 2040

Answers (1)

handhand
handhand

Reputation: 756

I am new to Keras, too. And the following is what I figured out after playing around with the Dot operation.

Firstly, the shape parameter of Input layer is NOT including the batch size. In your code, x2 = Input(shape=(4,)), so x2 is expecting the input data to be (None, 4), (None refers to batch size), but a2 is np.array([1,2,3,4]), the shape is (1, 4), hence the error message. To get rid of the error you need to add the batch_size dimension to a2.

But then there is another problem, according to the doc of Dot, I think x1 and x2 should have the same batch size:

if applied to a list of two tensors a and b of shape (batch_size, n), the output will be a tensor of shape (batch_size, 1) where each entry i will be the dot product between a[i] and b[i].

So I manually match the batch size of a1 and a2, and the batch size of a1 is 2, so a2 needs to be np.array([[1,2,3,4],[1,2,3,4]])

Now you can have your desire result:

[[ 20. 60.] [100. 140.]]

A few more words for beginners like me, the shape of x1 is (batch_size, 2, 4), the shape of x2 is (batch_size, 4), it seems that they are not compatible. Now is when the 'axes' parameter comes into play. In OP's code, axes=(2,1) means to dot x1's 3rd axis (0-indexed, it's the axis with the length of 4), with x2's 2nd axis(also the length is 4). So it will be [0,1,2,3]dot[1,2,3,4]=20, [4,5,6,7]dot[1,2,3,4]=60 ...

Upvotes: 2

Related Questions