我如何汇总2D张量,而其中一些必须平均并且其中一些保留?
我想汇总2D张量。规则是,如果变量“ IDX”中的数字相同,则为相应的张量。以下是问题。
idx = torch.tensor([[0, 1, 2], [1, 2, 3], [3, 4, 5]])
x = torch.tensor([[10, 10, 10], [11, 11, 11], [12, 12, 12],
[13, 13, 13], [14, 14, 14], [15, 15, 15],
[16, 16, 16], [17, 17, 17], [18, 18, 18]])
例如,由于重复索引(1,2),因此将汇总相应的张量
[[11, 11, 11], [12, 12, 12]] and [[13, 13, 13], [14, 14, 14]]
我们会得到张量
[[12, 12, 12], [13, 13, 13]]
,因此,如果没有重复的数字, ,请保持张量。因此,对于这个问题,所需的答案是
torch.tensor([[10, 10, 10], [12, 12, 12], [13, 13, 13], [15.5, 15.5, 15.5], [17, 17, 17], [18, 18, 18]])
我该怎么做?预先感谢您的帮助。
I want to aggregate a 2D tensor. The rule is, if the number in the the variable "idx" is the same, average the corresponding tensor. Below is the problem.
idx = torch.tensor([[0, 1, 2], [1, 2, 3], [3, 4, 5]])
x = torch.tensor([[10, 10, 10], [11, 11, 11], [12, 12, 12],
[13, 13, 13], [14, 14, 14], [15, 15, 15],
[16, 16, 16], [17, 17, 17], [18, 18, 18]])
For example, since index (1, 2) is repeated, the corresponding tensors
[[11, 11, 11], [12, 12, 12]] and [[13, 13, 13], [14, 14, 14]]
will be aggregated and thus we get the tensor
[[12, 12, 12], [13, 13, 13]]
if there is no repeated number, keep the tensor. So for this problem the desired answer is
torch.tensor([[10, 10, 10], [12, 12, 12], [13, 13, 13], [15.5, 15.5, 15.5], [17, 17, 17], [18, 18, 18]])
How do I do this? Thank you in advance for helping.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您可以使用散点功能。
2D中的最小设置
让我们探索您的问题的简化版本,从而没有三胞胎,而是有单个值。在这种情况下,我们将拥有:
我们应该查看中间结果,而不是所需的结果本身。最终,我们知道结果将沿其一个维度之一求和或平均。我们可以首先定义其形状,它的行应该与
x
和idx
以及与IDX
中的唯一索引一样多。在此特定示例中,我们正在寻找的中间结果将具有(3,6)
的形状。由于#[| 0,5 |] = 6
:我们可以看到,一旦沿着第一个维度减少,我们就会得到:
这是1D中此最小示例的所需结果。
要进行这样的操作,我们可以从 TORCH.TENSOR.SCATTER 。当应用于二维张量并设置
dim = 1
时,调用此行:将对
out
>:如果我们尝试使用输入张量,我们会得到:
然后,我们只需要使用 /code> 和 :
原始问题
,但是,在您的原始用例中,每个元素有三个值:
因此,我们首先必须在
x
和idx
上进行以下操作:reshape
x
与Shape的3D张量
>使其具有与
x _
的形状相同的形状:然后,我们只需要使用
torch.scatter.scatter.scatter
应用相同的方法,但是 (dim = 1,index = idx_,src = x _)out.scatter_
在3D中, ie
dim = 1
,与以前相同:It looks like you could use a scatter function.
Minimal setup in 2D
Let us explore a reduced version of your problem whereby instead of having triplets, we have single values. In that scenario, we would have:
We should look at an intermediate result rather than the desired result itself. Ultimately, we know that the result will be summed or averaged along one of its dimensions. We can first define its shape, it should have as many rows as
x
andidx
and as many columns as there are unique indices inidx
. In this particular example, the intermediate result we're looking for will have a shape of(3, 6)
. Since#[|0,5|]=6
:We can see that once reduced along the first dimension, we get:
Which is the desired result for this minimal example in 1D.
To do such operation we can benefit from
torch.Tensor.scatter
. When applied on two-dimensional tensors, and settingdim=1
, calling this line:will have the following effect on
out
:If we try on our input tensors, we get:
Then we only have to average over nonzero values using
torch.sum
andtorch.count_nonzero
:Original problem
However, in your original use case, you have three values per element:
Therefore, we first have to do the following on
x
andidx
:reshape
x
to a 3D tensor with shape(*idx.shape, x.size(-1))
:Unsqueeze and expand
idx
such that it has the same shape asx_
:Then we simply have to apply the same approach with
torch.scatter
, but in 3D, i.e.out.scatter_(dim=1, index=idx_, src=x_)
which will result in:For our inputs:
To finish it off, reduce on
dim=1
, same way as before: