简体   繁体   English

Python张量矩阵乘法

[英]Python tensor matrix multiply

I have the tensor我有张量

A = 
[[[a,b],
  [c,d]],
 [[e,f],
  [g,h]]]

and the matrix和矩阵

B = 
[[1,2],
 [3,4]]

I need to get我需要得到

C = 
[[a*1+e*2,b*1+f*2],
 [c*3+g*4,d*3+h*4]]

How can I do this using numpy in matrix form?如何使用矩阵形式的 numpy 来做到这一点? I've looked into np.tensordot() but it doesn't seem to help in this case.我已经研究过np.tensordot()但在这种情况下似乎没有帮助。

OP's problem can be reformulated in a standard format using tensor notation and the so called Einstein summation convention OP 的问题可以使用张量表示法和所谓的爱因斯坦求和约定以标准格式重新表述

A kij B ik ⇒ C ij A kij BikCij

Numpy has a convenient utility function to perform the kind of tensor operations that can be descibed using the Einstein summation convention, unsurprisingly named numpy.einsum , that allows to straightforwardly map the tensor notation to optimized C-level loops by the means of an instruction string that exactly reflects the tensor notation, 'kij, ik -> ij' Numpy 有一个方便的效用函数来执行可以使用爱因斯坦求和约定描述的那种张量运算, numpy.einsum ,它被命名为numpy.einsum ,它允许通过指令字符串直接将张量符号映射到优化的 C 级循环这完全反映了张量符号, 'kij, ik -> ij'

import numpy as np
a = np.arange(8).reshape(2,2,2)+1
b = np.arange(4).reshape(2,2)+1
c = np.einsum('kij, ik -> ij', a, b)
print(c)
# [[11 14]
#  [37 44]]

Merits of numpy.einsum numpy.einsum优点

  1. The source code documents the details of the operation performed.源代码记录了所执行操作的详细信息。
  2. np.einsum typically is fast np.einsum通常很快

    In [12]: import numpy as np ...: ...: i, j, k = 100, 320, 140 # just three largish numbers ...: a = np.random.random((k,i,j)) ...: b = np.random.random((i,k)) In [13]: %timeit np.einsum('kij,ik->ij', a, b) 7.47 ms ± 82.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [14]: %timeit (a * b[None,:,:].T).sum(axis = 0) 49.3 ms ± 6.77 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

You can try this:你可以试试这个:

>>> import numpy as np
>>> a = np.arange(1,9).reshape(2,2,2)
>>> a
array([[[1, 2],
        [3, 4]],

       [[5, 6],
        [7, 8]]])
>>> b = np.arange(1,5).reshape(2,2)
>>> b
array([[1, 2],
       [3, 4]])
>>> (a * b[None,:,:].T).sum(axis = 0)
array([[11, 14],
       [37, 44]])

Intermediate steps look like this:中间步骤如下所示:

>>> b[None,:,:]
array([[[1, 2],
        [3, 4]]])
>>> b[None,:,:].T
array([[[1],
        [3]],

       [[2],
        [4]]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM