简体   繁体   English

如何在Theano中对选定的尺寸进行逐元素乘法?

[英]How to do the element-wise multiplication over selected dimensions in Theano?

I have two tensors and I need to do the following: 我有两个张量,需要执行以下操作:

C[i, j, k, l, a, b] = A[i, j, k, l] * B[a, b, k, l]

So, as you can see there is not summation over k and l . 因此,正如您所看到的, kl没有求和。 So, I cannot apply tensordot . 因此,我无法应用tensordot

Here is the solution: 解决方法如下:

C = a.dimshuffle(0, 1, 2, 3, 'x', 'x') * b.dimshuffle('x', 'x', 2, 3, 0, 1)

And here is the explanation: 这里是解释:

You try to archive two goals: 您尝试存档两个目标:

  1. Pair only some indices of the first tensor with some indices of the second tensor. 仅将第一个张量的一些索引与第二个张量的一些索引配对。
  2. Do not sum up over the paired indices. 不要总结成对的索引。

If you use pairwise multiplication, then there will be no summation over the paired indices (unlike in tensordot , which sums up over the paired indices / axes). 如果您使用成对乘法,那么将不会在成对索引上求和(与tensordot不同,它在成对索引/轴上求和)。 So, the second goal is achieved by using pairwise multiplication. 因此,通过使用成对乘法来实现第二个目标。

Now you need to achieve the first goal. 现在您需要实现第一个目标。 Per default pairwise multiplication pairs all the indices of the first tensor with all the corresponding indices of the second tensor (first with first, second with second and so on). 默认情况下,成对乘法将第一个张量的所有索引与第二个张量的所有对应索引配对(第一个与第一个,第二个与第二个,依此类推)。 To avoid that use dimshuffle to align indexes that have to be paired and use 'x' to introduce "dummy" axes for the indices that should not be paired. 为了避免这种情况,请使用dimshuffle对齐必须配对的索引,并使用“ x”为不应配对的索引引入“虚拟”轴。 In your particular example it should like like that: 在您的特定示例中,它应该像这样:

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

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