简体   繁体   English

3D 数组乘法

[英]3D array multiplication

I have arrays A and B both of dimension MxNxH.我有维度 MxNxH 的数组AB

I would like to define a binary operator, to "multiply", such that the result is MxN dimensions.我想定义一个二元运算符来“相乘”,结果是 MxN 维度。

The equivalent operation would be:等效的操作是:

C = A[:,:,0] * B[:,:,0] + A[:,:,1] * B[:,:,1] + .... + A[:,:,H] * B[:,:,H]

Is there a way to do this operation in a more efficient way?有没有办法以更有效的方式执行此操作?
For example, using a built in function in numpy?例如,在 numpy 中使用内置函数?

I have tried tensordot , but this gives a different result.我试过tensordot ,但这给出了不同的结果。

The easiest is:最简单的是:

C = numpy.sum(A * B, -1)

I think this might work too:我认为这也可能有效:

C = numpy.einsum("...i,...i->...", A, B)

try this: numpy.sum( A*B, axis=2 )试试这个: numpy.sum( A*B, axis=2 )

this is similar to the other suggestion but perhaps clearer (axes are numbered from 0, so axis=2 is the 3rd axis or H out of MxNxH)这与其他建议类似,但可能更清晰(轴从 0 开始编号,因此轴 = 2 是 MxNxH 中的第 3 个轴或 H)

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

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