Numpy multiply 3d matrix by 2d matrix

前端 未结 2 1261
忘掉有多难
忘掉有多难 2021-01-13 12:20

For example, I got matrix A of shape (3,2,2), e.g.

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

and matrix B of shape (2,2), e.g.

2条回答
  •  星月不相逢
    2021-01-13 12:57

    Use np.tensordot and then swap axes. So, use one of these -

    np.tensordot(B,A,axes=((1),(1))).swapaxes(0,1)
    np.tensordot(A,B,axes=((1),(1))).swapaxes(1,2)
    

    We can reshape A to 2D after swapping axes, use 2D matrix multiplication with np.dot and reshape and swap axes to maybe gain marginal performance boost.

    Timings -

    # Original approach
    def orgapp(A,B):
        m = A.shape[0]
        n = B.shape[0]
        r = A.shape[2]
        c = np.zeros((m,n,r))
        for i in range(len(A)):
            c[i] = np.dot(B, A[i,:,:])
        return c  
    
    In [91]: n = 10000
        ...: A = np.random.rand(n,2,2)
        ...: B = np.random.rand(2,2)
    
    In [92]: %timeit orgapp(A,B)
    100 loops, best of 3: 12.2 ms per loop
    
    In [93]: %timeit np.tensordot(B,A,axes=((1),(1))).swapaxes(0,1)
    1000 loops, best of 3: 191 µs per loop
    
    In [94]: %timeit np.tensordot(A,B,axes=((1),(1))).swapaxes(1,2)
    1000 loops, best of 3: 208 µs per loop
    
    # @Bitwise's solution
    In [95]: %timeit np.flip(np.dot(A,B).transpose((0,2,1)),1)
    1000 loops, best of 3: 697 µs per loop
    

提交回复
热议问题