2

I'm implementing a neural network in python, as a part of backpropagation I need to multiply a 3D matrix,call it A, dimension (200, 100, 1) , by a 2D matrix, call it W,dimension (100, 200) the result should have dimensions (200, 200, 1).

A is an error vector, W is a weight matrix, the product is to be used to calculate the updates for the previous layer.

I tried solving it using matrix_multiply(from numpy.core.umath_tests), I tried reshaping W to (100,200,1) and then multiplying, but that throws

ValueError: matrix_multiply: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (m,n),(n,p)->(m,p) (size 100 is different from 1).

How can I solve this?

2
  • Do you have a compelling reason for keeping the shape of A as (200, 100, 1), instead of dropping the trivial dimension and making its shape (200, 100)? Commented Feb 11, 2017 at 15:21
  • @WarrenWeckesser I didn't consider that option. Thanks for the suggestion, I'll explore it. Commented Feb 11, 2017 at 15:59

1 Answer 1

4

You could use np.tensordot and then permute axes with swapaxes or simply reshape -

np.tensordot(A,W,axes=((1),(0))).swapaxes(1,2)
np.tensordot(A,W,axes=((1),(0))).reshape(A.shape[0],W.shape[1],1)

Alternatively, we can use np.dot using the only slice along the last axis of A and then after matrix-multiplication extend into 3D -

A[:,:,0].dot(W)[...,None]

Or we can use np.einsum -

np.einsum('ijk,jl->ilk',A,W)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.