torch_scatter.scatter_add、Tensor.scatter_add_ 、Tensor.scatter_、Tensor.scatter_add 、Tensor.scatter

 torch_scatter.scatter_add

官方文档:torch_scatter.scatter_add(srcindexdim=-1out=Nonedim_size=Nonefill_value=0)

Sums all values from the src tensor into out at the indices specified in the index tensor along a given axis dim. For each value in src, its output index is specified by its index in input for dimensions outside of dim and by the corresponding value in index for dimension dim. If multiple indices reference the same location, their contributions add.

看着挺疑惑的,自己试了一把:

src = torch.tensor([10, 20, 30, 40, 1, 2, 2, 2, 9])
index = torch.tensor([2, 1, 1, 1, 1, 1, 1, 1, 0])
out=scatter_add(src, index)
print(out)

输出结果为:tensor([ 9, 97, 10])

说白了就是:index就是out的下标,将src所有和此下标对应的值加起来,就是out的值。

例如上面的例子:index中等于1的,对应于src是【20, 30, 40, 1, 2, 2, 2】,将这些值加起来是97,于是,out[1]=97

同理:out[0]=src[8]=9     out[2]=src[0]=10

 

另一个函数

Tensor.scatter_add_

官方文档:

scatter_add_(self, dim, index, other):
For a 3-D tensor, :attr:`self` is updated as::

    self[index[i][j][k]][j][k] += other[i][j][k]  # if dim == 0
    self[i][index[i][j][k]][k] += other[i][j][k]  # if dim == 1
    self[i][j][index[i][j][k]] += other[i][j][k]  # if dim == 2

官方例子:

            >>> x = torch.rand(2, 5)
            >>> x
            tensor([[0.7404, 0.0427, 0.6480, 0.3806, 0.8328],
                    [0.7953, 0.2009, 0.9154, 0.6782, 0.9620]])
            >>> torch.ones(3, 5).scatter_add_(0, torch.tensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]), x)
            tensor([[1.7404, 1.2009, 1.9154, 1.3806, 1.8328],
                    [1.0000, 1.0427, 1.0000, 1.6782, 1.0000],
                    [1.7953, 1.0000, 1.6480, 1.0000, 1.9620]])

以index来遍历,就比较容易看懂。self中并不是每个值都要改变的。

以上面为例 index[0][0]=0  self[index[0][0]][0]=self[0][0] =self[0][0]+ x[0][0]=1 +0.7404=1.7404

                   index[0][1]=1  self[index[0][1]][1]=self[1][1] =self[1][1]+ x[0][1] =1 +0.0427 =1.0427

                   。。。

                  以此类推,将index遍历一遍,就得到最终的结果

所以,self中需要改变的是index中列出的坐标,其他的是不动的。


Tensor.scatter_

scatter_(self, dim, index, src)

和Tensor.scatter_add_的区别是直接将src中的值填充到self中,不做相加

例子:

>>> x = torch.rand(2, 5)
            >>> x
            tensor([[ 0.3992,  0.2908,  0.9044,  0.4850,  0.6004],
                    [ 0.5735,  0.9006,  0.6797,  0.4152,  0.1732]])
            >>> torch.zeros(3, 5).scatter_(0, torch.tensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]), x)
            tensor([[ 0.3992,  0.9006,  0.6797,  0.4850,  0.6004],
                    [ 0.0000,  0.2908,  0.0000,  0.4152,  0.0000],
                    [ 0.5735,  0.0000,  0.9044,  0.0000,  0.1732]])
        
            >>> z = torch.zeros(2, 4).scatter_(1, torch.tensor([[2], [3]]), 1.23)
            >>> z
            tensor([[ 0.0000,  0.0000,  1.2300,  0.0000],
                    [ 0.0000,  0.0000,  0.0000,  1.2300]])

另外,pytorch中还有

scatter_add和scatter函数,和上面两个函数不同的是这个两个函数不改变self,会返回结果值;上面两个函数(scatter_add_和scatter_)是直接在原数据self上进行修改
`torch_scatter.scatter_max`函数是PyTorch中的一种scatter函数,用于将输入的Tensor按照指定的维度进行散射操作,并返回指定维度上的元素最大值和对应的索引位置。 该函数的输入包括三个参数:输入Tensor(即要进行散射操作的Tensor)、散射维度dim和索引Tensor(即指定维度上的索引位置)。输出包括两个Tensor:散射后的Tensor和对应的最大值和索引位置。 具体来说,`torch_scatter.scatter_max`函数的操作流程如下: 1. 根据索引Tensor将输入Tensor按照指定维度进行散射操作,得到一个散射后的Tensor。 2. 在指定维度上找到散射后的Tensor中的最大值和对应的索引位置。 3. 返回散射后的Tensor和最大值和索引位置对应的两个Tensor。 值得注意的是,如果输入Tensor中某些元素在指定维度上对应的索引位置相同,那么在散射操作时,这些元素的最大值和索引位置会被更新为最后一个被处理到的元素的最大值和索引位置。 下面是一个简单的示例代码,演示了如何使用`torch_scatter.scatter_max`函数: ```python import torch from torch_scatter import scatter_max # 定义一个输入Tensor x = torch.tensor([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]]) # 定义一个索引Tensor index = torch.tensor([0, 1, 0]) # 在第一维上进行散射操作,得到散射后的Tensor和最大值和索引位置对应的两个Tensor out, argmax = scatter_max(x, index, dim=0) # 输出结果 print(out) # tensor([[0.7000, 0.8000, 0.9000], [0.4000, 0.5000, 0.6000]]) print(argmax) # tensor([2, 1]) ``` 在上面的示例代码中,我们首先定义了一个3x3的输入Tensor `x`,然后定义了一个长度为3的索引Tensor `index`,表示在第一维上,第一个元素要被散射到第0个位置,第二个元素要被散射到第1个位置,第三个元素要被散射到第0个位置。 之后我们调用`torch_scatter.scatter_max`函数,在第一维上进行散射操作,得到了散射后的Tensor `out`和最大值和索引位置对应的两个Tensor `argmax`。最后我们输出了这两个Tensor的值,可以看到在第一维上,第一个位置对应的最大值为0.7,索引为2,第二个位置对应的最大值为0.5,索引为1。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值