vega
vega

Reputation: 2189

How do I multiply a tensor by a matrix

So I have array A with shape [32,60,60] and array B with shape [32,60]. The first dimension is the batch size, so the first dimension is independent. What I want to do is a simple matrix by vector multiplication. So for each sample in A I want to multiply matrix of shape [60,60] with vector of shape [60]. Multiplying across the batch A*B should give me an array of shape [32,60].

This should be simple but I'm doing something wrong:

>>> v = np.matmul(A,B)
ValueError: shapes (32,60,60) and (32,60) not aligned: 60 (dim 2) != 32 (dim 0)

This is for tensorflow, but a numpy answer may suffice if I can convert the notation.

Upvotes: 2

Views: 2836

Answers (1)

Divakar
Divakar

Reputation: 221554

It seems you are trying to sum-reduce the last axes from the two input arrays with that matrix-multiplication. So, with np.einsum, it would be -

np.einsum('ijk,ik->ij',A,B)

For tensorflow, we can use tf.einsum.


With np.matmul, we need to extend B to 3D by introducing a new axis at the last one. Thus, using np.matmul would get the second axis of B's extended version sum-reduced against the third one of A. The result would be 3D. So, get the last singleton axis squeezed out with slicing or np.squeeze. Hence, the implementation would be -

np.matmul(A,B[...,None])[...,0]

We already have an existing tf.matmul function there.

Upvotes: 2

Related Questions