没有循环的子数组的点积

时间:2022-01-08 00:56:44

when we have:

当我们有:

array 1: A, shape (49998,3,3) 
array 2: B, shape (3, 49998)

and i want to multiply their subarrays to get

我想把它们的子数组相乘

array 3: C, shape(3,49998)

for which im using generator:

im使用生成器:

def genC(A,B):
    for a,b in itertools.izip(A,B.T):
        c=np.dot(a,b)
        yield c.T[0]

C=np.array([c for c in genC()]).T

so how could i do array multiplication insides of A,B without for loop to get array C?

那么,如果没有for循环,我怎么能在A B的内部进行数组乘法运算来得到C?

i was trying to use np.tensordot, but i cant get it

我想用np。tensordot,但我不能理解

NOTE:

注意:

this is just basic example, for some test cos in orginal data i had

这只是一个基本的例子,对于一些测试cos在原始数据中

4*3*37 arrays A(500 000,3,3) B(3,500 000)

4*3*37阵列A(500000,3,3) B(3,500 000)

to do, and for loop sems for me not pythonic way xD

要做的是,对于循环sems,我用的不是勾股定理xD

1 个解决方案

#1


4  

If I am getting your code right, you want to perform 49998 dot products of a 3x3 matrix with a 3 vector, right? That is very easy to do with np.einsum:

如果我把你的代码写对了,你想用3维矩阵的49998点乘3维向量,对吧?这与np.einsum很容易:

np.einsum('ijk,ki->ij', A, B)

#1


4  

If I am getting your code right, you want to perform 49998 dot products of a 3x3 matrix with a 3 vector, right? That is very easy to do with np.einsum:

如果我把你的代码写对了,你想用3维矩阵的49998点乘3维向量,对吧?这与np.einsum很容易:

np.einsum('ijk,ki->ij', A, B)