Numpy - Dot Product of a Vector of Matrices with a Vector of Scalars

[亡魂溺海] 提交于 2020-01-24 04:45:09

问题


I have a 3 dimensional data set that I am trying to manipulate in the following way.

data.shape = (643, 2890, 10)
vector.shape = (643,)

I would like numpy to see data as a 643 length 1-D array of 2890x10 matrices and calculate a dot product (sum-product?) between data and vector. I can do this with a loop, but would really like to find a way to do this using a primitive (this will be run many times across parallel nodes).

The equivalent loop (I believe):

a = numpy.zeros ((2890, 10))
for i in range (643):
   a += vector[i]*data[i]

Thanks very much! Sorry if this is a repost, I've searched far and wide, and ended up making an account to ask you guys.

   a = numpy.array ([[[1,1,1,1],[2,2,2,2],[3,3,3,3]], [[3,3,3,3],[4,4,4,4],[5,5,5,5]]])
   b = numpy.array ([10,20])
# Thus, 
   a.shape = (2,3,4)
   b.shape = (2,)
# Want an operation . such that:
   a . b = [[10,10,10,10],[20,20,20,20],[30,30,30,30]] + [[60,60,60,60],[80,80,80,80],[100,100,100,100]]
         = [[70,70,70,70],[100,100,100,100],[130,130,130,130]]

回答1:


If your NumPy is new enough (1.6 or better), you could use numpy.einsum:

result = np.einsum('ijk,i -> jk', data, vector)

In [36]: data = np.array ([[[1,1,1,1],[2,2,2,2],[3,3,3,3]], [[3,3,3,3],[4,4,4,4],[5,5,5,5]]])

In [37]: vector = np.array ([10,20])

In [38]: np.einsum('ijk,i -> jk', data, vector)
Out[38]: 
array([[ 70,  70,  70,  70],
       [100, 100, 100, 100],
       [130, 130, 130, 130]])

Or, without np.einsum, you could add extra axes to vector and take advantage of broadcasting to perform the multiplication:

In [64]: (data * vector[:,None,None]).sum(axis=0)
Out[64]: 
array([[ 70,  70,  70,  70],
       [100, 100, 100, 100],
       [130, 130, 130, 130]])



回答2:


The previous answer uses the right numpy function, however it doesn't give the result asked.

It should be:

np.einsum('i, ijk -> ijk', vector, data)


来源:https://stackoverflow.com/questions/16704037/numpy-dot-product-of-a-vector-of-matrices-with-a-vector-of-scalars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!