user3124007
user3124007

Reputation: 11

How to combine element-wise multiplication and matrix multiplication

I'm trying to combine element-wise multiplication and matrix multiplication for two matrix:

I would like to perform element-wise operation for the first two dimensions (N, N), and matrix multiplication for the last two dimensions. The goal is to get a (N, N, 3, 1) matrix.

I was not able to find a good operation in numpy, may I know if there is a proper operation for this? Thanks!

Upvotes: 0

Views: 624

Answers (1)

swag2198
swag2198

Reputation: 2696

Probably you want this. This is essentially matrix multiplication of the inner (3, 3) and (3, 1) shaped matrices.

import numpy as np
>>> a = np.random.rand(2, 2, 3, 3)
>>> b = np.random.rand(2, 2, 3, 1)
>>> c = np.matmul(a, b)
>>> c.shape
(2, 2, 3, 1)

Upvotes: 1

Related Questions