• Pytorch - scatter_()


    1. torch_scatter.scatter(src: Tensor, index: Tensor, dim: int = - 1,
    2. out: Optional[Tensor] = None,
    3. dim_size: Optional[int] = None,
    4. reduce: str = 'sum')→ Tensor
    • src – The source tensor. ( 源张量)

    • index – The indices of elements to scatter.(要分散的元素的索引)

    • dim – The axis along which to index. (default: -1) (要索引的轴(默认值:-1))

    • out – The destination tensor. (目标张量)

    • dim_size – If out is not given, automatically create output with size dim_size at dimension dim. If dim_size is not given, a minimal sized output tensor according to index.max() + 1 is returned.(如果未给出out,则在 dim 处自动创建尺寸为 dim_size 的输出。如果没有给出 dim_size,则返回根据 index.max() + 1 的最小尺寸输出张量);

    • reduce – The reduce operation ("sum""mul""mean""min" or "max"). (default: "sum") (reduce 操作(“ sum”、“ mul”、“ mean”、“ min”或“ max”);

    直观的理解:

    对于三维矩阵:

    1. y = y.scatter(dim,index,src)
    2. #则结果为:
    3. y[ index[i][j][k] ] [j][k] = src[i][j][k] # if dim == 0
    4. y[i] [ index[i][j][k] ] [k] = src[i][j][k] # if dim == 1
    5. y[i][j] [ index[i][j][k] ] = src[i][j][k] # if dim == 2

    对于二维矩阵:

    1. y = y.scatter(dim,index,src)
    2. #则:
    3. y [ index[i][j] ] [j] = src[i][j] #if dim==0
    4. y[i] [ index[i][j] ] = src[i][j] #if dim==1

    ps: index的维度,必须和src维度相同;

    举例:

    1. >>> src = torch.randn(3, 3)
    2. >>> src
    3. tensor([[-1.8801, 0.9740, 1.2865],
    4. [ 0.3140, 1.2396, -1.3452],
    5. [-0.8937, 0.6916, -2.0134]])
    6. >>> y = y.scatter_(0,index,src)
    7. >>> index = torch.tensor([[0, 1, 0],[1,0,1],[2,1,0]])
    8. >>> index
    9. tensor([[0, 1, 0],
    10. [1, 0, 1],
    11. [2, 1, 0]])
    12. >>> y = y.scatter_(0,index,src)
    13. >>> y
    14. tensor([[-1.8801, 1.2396, -2.0134],
    15. [ 0.3140, 0.6916, -1.3452],
    16. [-0.8937, 0.0000, 0.0000],
    17. [ 0.0000, 0.0000, 0.0000]])

  • 相关阅读:
    SpringBoot集成Quartz实现定时任务
    负数如何取模?深度理解取模:取余运算
    labview编程笔记之条件结构
    【赠书第2期】嵌入式虚拟化技术与应用
    scrcpy macos 编译安装最新版 1.2.4
    狂团KtAdmin框架正式免费开源发布,助力独立版SAAS系统快速开发!
    护网2024-攻防对抗解决方案思路
    git 批量clone,pull 项目
    STM32CubeMX实战教程(九)——外部SRAM+内存管理
    对均匀采样信号进行重采样
  • 原文地址:https://blog.csdn.net/qq_40671063/article/details/126256756