torch.Tensor.index_fill_(dim, index, val)以及index_fill(dim, index, value)使用举例

参考链接: index_fill_(dim, index, val)
参考链接: index_fill(dim, index, value)

在这里插入图片描述
实验代码:

Microsoft Windows [版本 10.0.18363.1256]
(c) 2019 Microsoft Corporation。保留所有权利。

C:\Users\chenxuqi>conda activate ssd4pytorch1_2_0

(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May  6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x000002DB7ECBD330>
>>>
>>> x = torch.randn(3,4)
>>> x
tensor([[ 0.2824, -0.3715,  0.9088, -1.7601],
        [-0.1806,  2.0937,  1.0406, -1.7651],
        [ 1.1216,  0.8440,  0.1783,  0.6859]])
>>> index = torch.tensor([0,2])
>>> index
tensor([0, 2])
>>>
>>> x.index_fill(1, index, 2.0200910)
tensor([[ 2.0201, -0.3715,  2.0201, -1.7601],
        [ 2.0201,  2.0937,  2.0201, -1.7651],
        [ 2.0201,  0.8440,  2.0201,  0.6859]])
>>> x
tensor([[ 0.2824, -0.3715,  0.9088, -1.7601],
        [-0.1806,  2.0937,  1.0406, -1.7651],
        [ 1.1216,  0.8440,  0.1783,  0.6859]])
>>>
>>> x.index_fill_(1, index, 88)
tensor([[88.0000, -0.3715, 88.0000, -1.7601],
        [88.0000,  2.0937, 88.0000, -1.7651],
        [88.0000,  0.8440, 88.0000,  0.6859]])
>>> x
tensor([[88.0000, -0.3715, 88.0000, -1.7601],
        [88.0000,  2.0937, 88.0000, -1.7651],
        [88.0000,  0.8440, 88.0000,  0.6859]])
>>>
>>>
>>>
>>>
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x000002DB7ECBD330>
>>> x = torch.randn(2,3,4)
>>> x
tensor([[[ 0.5816,  2.0060,  1.6013, -0.6379],
         [-1.1943,  0.1426,  1.3612, -1.4171],
         [ 0.2797, -0.5316,  0.6480,  2.6538]],

        [[ 1.1088,  1.2914, -1.4494, -1.7273],
         [-0.2370, -1.7016, -0.2565,  1.4568],
         [ 0.7152,  1.1784,  0.0806,  0.7787]]])
>>> index = torch.tensor([0,1])
>>> index
tensor([0, 1])
>>> x.index_fill(0, index, 88)
tensor([[[88., 88., 88., 88.],
         [88., 88., 88., 88.],
         [88., 88., 88., 88.]],

        [[88., 88., 88., 88.],
         [88., 88., 88., 88.],
         [88., 88., 88., 88.]]])
>>> x
tensor([[[ 0.5816,  2.0060,  1.6013, -0.6379],
         [-1.1943,  0.1426,  1.3612, -1.4171],
         [ 0.2797, -0.5316,  0.6480,  2.6538]],

        [[ 1.1088,  1.2914, -1.4494, -1.7273],
         [-0.2370, -1.7016, -0.2565,  1.4568],
         [ 0.7152,  1.1784,  0.0806,  0.7787]]])
>>> x.index_fill(1, index, 88)
tensor([[[ 8.8000e+01,  8.8000e+01,  8.8000e+01,  8.8000e+01],
         [ 8.8000e+01,  8.8000e+01,  8.8000e+01,  8.8000e+01],
         [ 2.7972e-01, -5.3157e-01,  6.4804e-01,  2.6538e+00]],

        [[ 8.8000e+01,  8.8000e+01,  8.8000e+01,  8.8000e+01],
         [ 8.8000e+01,  8.8000e+01,  8.8000e+01,  8.8000e+01],
         [ 7.1516e-01,  1.1784e+00,  8.0582e-02,  7.7868e-01]]])
>>>
>>> x.index_fill(2, index, 88)
tensor([[[ 8.8000e+01,  8.8000e+01,  1.6013e+00, -6.3792e-01],
         [ 8.8000e+01,  8.8000e+01,  1.3612e+00, -1.4171e+00],
         [ 8.8000e+01,  8.8000e+01,  6.4804e-01,  2.6538e+00]],

        [[ 8.8000e+01,  8.8000e+01, -1.4494e+00, -1.7273e+00],
         [ 8.8000e+01,  8.8000e+01, -2.5646e-01,  1.4568e+00],
         [ 8.8000e+01,  8.8000e+01,  8.0582e-02,  7.7868e-01]]])
>>> x
tensor([[[ 0.5816,  2.0060,  1.6013, -0.6379],
         [-1.1943,  0.1426,  1.3612, -1.4171],
         [ 0.2797, -0.5316,  0.6480,  2.6538]],

        [[ 1.1088,  1.2914, -1.4494, -1.7273],
         [-0.2370, -1.7016, -0.2565,  1.4568],
         [ 0.7152,  1.1784,  0.0806,  0.7787]]])
>>>
>>>
>>>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值