[英]How to convert a pytorch tensor of ints to a tensor of booleans?
I would like to cast a tensor of ints to a tensor of booleans.我想将整数张量转换为布尔值张量。
Specifically I would like to be able to have a function which transforms tensor([0,10,0,16])
to tensor([0,1,0,1])
具体来说,我希望能够有一个函数将
tensor([0,10,0,16])
为tensor([0,1,0,1])
This is trivial in Tensorflow by just using tf.cast(x,tf.bool)
.这在 Tensorflow 中只需使用
tf.cast(x,tf.bool)
。
I want the cast to change all ints greater than 0 to a 1 and all ints equal to 0 to a 0. This is the equivalent of !!
我希望强制转换将所有大于 0 的整数更改为 1,将所有等于 0 的整数更改为 0。这相当于
!!
in most languages.在大多数语言中。
Since pytorch does not seem to have a dedicated boolean type to cast to, what is the best approach here?由于 pytorch 似乎没有专用的布尔类型可以转换,这里最好的方法是什么?
Edit: I am looking for a vectorized solution opposed to looping through each element.编辑:我正在寻找一个矢量化的解决方案,而不是遍历每个元素。
What you're looking for is to generate a boolean mask for the given integer tensor.您正在寻找的是为给定的整数张量生成一个布尔掩码。 For this, you can simply check for the condition: "whether the values in the tensor are greater than 0" using simple comparison operator (
>
) or usingtorch.gt()
, which would then give us the desired result.为此,您可以简单地检查条件:“张量中的值是否大于 0”,使用简单的比较运算符 (
>
) 或使用torch.gt()
,然后会给我们所需的结果。
# input tensor
In [76]: t
Out[76]: tensor([ 0, 10, 0, 16])
# generate the needed boolean mask
In [78]: t > 0
Out[78]: tensor([0, 1, 0, 1], dtype=torch.uint8)
# sanity check
In [93]: mask = t > 0
In [94]: mask.type()
Out[94]: 'torch.ByteTensor'
Note : In PyTorch version 1.4+, the above operation would return 'torch.BoolTensor'
注意:在 PyTorch 1.4+ 版本中,上述操作将返回
'torch.BoolTensor'
In [9]: t > 0
Out[9]: tensor([False, True, False, True])
# alternatively, use `torch.gt()` API
In [11]: torch.gt(t, 0)
Out[11]: tensor([False, True, False, True])
If you indeed want single bits (either 0
s or 1
s), cast it using:如果您确实想要单个位(
0
秒或1
秒),请使用以下方法进行转换:
In [14]: (t > 0).type(torch.uint8)
Out[14]: tensor([0, 1, 0, 1], dtype=torch.uint8)
# alternatively, use `torch.gt()` API
In [15]: torch.gt(t, 0).int()
Out[15]: tensor([0, 1, 0, 1], dtype=torch.int32)
The reason for this change has been discussed in this feature-request issue: issues/4764 - Introduce torch.BoolTensor ...此功能请求问题中已讨论了此更改的原因: issues/4764 - Introduce torch.BoolTensor ...
TL;DR : Simple one liner TL;DR : 简单的一个衬垫
t.bool().int()
You can use comparisons as shown below:您可以使用如下所示的比较:
>>> a = tensor([0,10,0,16])
>>> result = (a == 0)
>>> result
tensor([ True, False, True, False])
Convert boolean to number value:
将布尔值转换为数值:
a = torch.tensor([0,4,0,0,5,0.12,0.34,0,0]) print(a.gt(0)) # output in boolean dtype # output: tensor([False, True, False, False, True, True, True, False, False]) print(a.gt(0).to(torch.float32)) # output in float32 dtype # output: tensor([0., 1., 0., 0., 1., 1., 1., 0., 0.])
Another option would be to simply do:另一种选择是简单地做:
temp = torch.tensor([0,10,0,16])
temp.bool()
#Returns
tensor([False, True, False, True])
PyTorch's to(dtype)
method has convenient data-type named aliases . PyTorch 的
to(dtype)
dtype to(dtype)
方法具有方便的名为 aliases 的数据类型。 You can simply call bool
:您可以简单地调用
bool
:
>>> t.bool()
tensor([False, True, False, True])
>>> t.bool().int()
tensor([0, 1, 0, 1], dtype=torch.int32)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.