torch.masked_select和torch.masked_scatter

torch.masked_select

import torch

x = torch.randn(3,4)
print(x)
mask = torch.randn(3,1) >0
print(mask)
s=torch.masked_select(x,mask)
print(s)

输出

tensor([[-2.7601, -0.9957, -0.2239, -1.2674],
        [ 1.2190,  1.6782, -0.3635,  0.6803],
        [-1.4541, -2.6175,  0.5655,  1.8493]])
tensor([[ True],
        [False],
        [ True]])
tensor([-2.7601, -0.9957, -0.2239, -1.2674, -1.4541, -2.6175,  0.5655,  1.8493])

import torch

x = torch.randn(3,4)
print(x)
mask = torch.randn(3,1) >0
print(mask)
re=torch.randn(3,4)+100
print(re)
x.masked_scatter_(mask,re)
print(x)

输出

tensor([[-0.2986, -2.1390, -1.3152,  0.1101],
        [-0.3291, -0.1943,  0.5282, -0.9538],
        [-0.1157,  1.1725, -1.1309,  1.1864]])
tensor([[ True],
        [False],
        [ True]])
tensor([[100.2276,  99.5916, 100.3636,  98.6109],
        [ 98.6181,  98.8684, 100.3451, 100.8166],
        [100.1912, 100.7507, 100.6337, 100.2147]])
tensor([[100.2276,  99.5916, 100.3636,  98.6109],
        [ -0.3291,  -0.1943,   0.5282,  -0.9538],
        [ 98.6181,  98.8684, 100.3451, 100.8166]])

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值