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

下面的程序会报错: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.])

猜你喜欢

转载自blog.csdn.net/weixin_38314865/article/details/105949932