![b43d426c26fa0b7c1eec41c55d9523aa.png](https://i-blog.csdnimg.cn/blog_migrate/fb7f601286dd8dd9e1be8d412a9b3ef9.jpeg)
前言: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