0
点赞
收藏
分享

微信扫一扫

RuntimeError: Subtraction, the `-` operator, with a bool tensor is not supported. If you are trying

笙烛 2022-03-11 阅读 145

参考RuntimeError: Subtraction, the `-` operator - 云+社区 - 腾讯云

下面的程序会报错:RuntimeError: Subtraction, the `-` operator, with a bool tensor is not supported. If you are trying to invert a mask, use the `~` or `logical_not()` operator instead.

mask = torch.Tensor([True,True,False]).type(torch.bool)
a = torch.Tensor([3,2,1])
a[1-mask]=0
print(a)

原因是pytorch改版之后不允许对bool变量进行“-”操作,如果需要对bool变量进行反转,则使用“~”操作,正确的代码如下:

mask = torch.Tensor([True,True,False]).type(torch.bool)
a = torch.Tensor([3,2,1])
a[~mask]=0
print(a)

tensor([3., 2., 0.])
举报

相关推荐

0 条评论