Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytorch: Create an boolean tensor (type: torch.ByteTensor)?

Tags:

python

pytorch

I want to create a tensor only containing boolean values. In Matlab that would be

a = false(10,1)
like image 363
mcExchange Avatar asked Jan 08 '18 13:01

mcExchange


People also ask

What is a PyTorch tensor?

PyTorch: Tensors A PyTorch Tensor is basically the same as a numpy array: it does not know anything about deep learning or computational graphs or gradients, and is just a generic n-dimensional array to be used for arbitrary numeric computation.


2 Answers

Isn't this more economical (albeit longer):

a = torch.zeros(10, dtype=torch.bool)

or, in older versions,

a = torch.zeros(10, dtype=torch.uint8)

(Thanks @drevicko for the pointer to bool.)

like image 154
Yul Kang Avatar answered Sep 20 '22 08:09

Yul Kang


Already found it:

a = torch.zeros(10)
b = a.type(torch.ByteTensor)
like image 21
mcExchange Avatar answered Sep 19 '22 08:09

mcExchange