scatter函数_PyTorch数据发散(scatter/scatter_add)与聚集(Gather)操作和二者关系

本文详细介绍了PyTorch中scatter和scatter_add_的基础操作,包括函数原型、参数说明和使用示例。scatter是数据发散操作,scatter_add_在发散基础上增加了累加功能。此外,还讲解了数据聚集操作gather,它是scatter的逆操作。这些操作在图神经网络计算框架PyG中扮演关键角色。
摘要由CSDN通过智能技术生成

b43d426c26fa0b7c1eec41c55d9523aa.png

前言:scatter和gather这两个操作在图神经网络计算框架PyG中起着非常重要的作用,搞明白这两个函数那么接下来理解GNN发散和聚集操作就很ez了

一,最基础的发散操作Scatter

函数原型:

scatter_(dim,index,src)→ Tensor

参数:

  • dim (int) – the axis along which to index
  • index (LongTensor) – the indices of elements to scatter, can be either empty or the same size of src. When empty, the operation returns identity
  • src (Tensor) – the source element(s) to scatter, incase value is not specified
  • value (float) – the source element(s) to scatter, incase src is not specified

scatter_(注:scatter_是scatter的就地操作)将所有被index指出的src中的值写入到张量self(即调用scatter_的张量)中。具体来说,对于src中的每个值,其在张量self中的输出索引由其dimension != dim处的索引值和其对应在张量index中dimension= dim处的值所组成(这句话比较绕,看公式和下面的例子可以帮助理解)。

Writes all values from the tensor src into self at the indices specified in the index tensor. For each value in src, its output index is specified by its index in src for dimension != dim and by the corresponding value in index for dimension = dim.

对于一个三维的张量来说,张量self的更新公式如下所示:

self[index[i][j][k]][j][k] = src[i][j][k]  # if dim == 0
self[i][index[i][j][k]][k] = src[i][j][k]  # if dim == 1
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值