zoukankan      html  css  js  c++  java
  • 【PyTorch】tensor.scatter

    【PyTorch】scatter

    参数:

    • 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

    官网例子:

    第三个参数为张量时:

    >>> 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]])
    

    又一个栗子:

    dim = 0

    >>> torch.zeros(3, 5).scatter_(0, torch.tensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]), 7)
    tensor([[7., 7., 7., 7., 7.],
            [0., 7., 0., 7., 0.],
            [7., 0., 7., 0., 7.]])
    

    dim = 1

    >>> torch.zeros(3, 5).scatter_(1, torch.tensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]), 7)
    tensor([[7., 7., 7., 0., 0.],
            [7., 7., 7., 0., 0.],
            [0., 0., 0., 0., 0.]])
    
    8DB47597EE6E611873901BD3CD2226B5
  • 相关阅读:
    网络兼职轻松赚
    微信H5支付demo
    支付宝H5支付demo
    笔记本安装SSD固态硬盘详细的优化设置
    JQuery的AJAX
    第二次面试
    存储过程的总结
    SQl数据操作和查询
    面对考试毫无畏惧的SSH
    掷骰子-IOS新手项目练习(抱歉,由于个人原因,图片没显示,要源码的项目私聊)
  • 原文地址:https://www.cnblogs.com/xxxxxxxxx/p/13546390.html
Copyright © 2011-2022 走看看