ByteTensor inconsistency between 0.3.1 and 0.4.0

Hello,

I found an (imo unnecessary) inconsistency in ByteTensor handling between 0.3.1 and 0.4.0. You basically cannot assign a boolean into a ByteArray when indexing. Why? It’s very practical, but I am maybe missing part of the story here.

Consider the following ipython experiment:

torch 0.3.1

In [1]: import torch

In [2]:         terminals = [False, False, True,
   ...:                      False, False, True,
   ...:                      False, False, False, True]
   ...:         terminals_tensor = torch.ByteTensor(terminals)
   ...:              

In [3]: terminals_tensor[0] = True

In [4]: 

torch 0.4.0

In [9]:         terminals = [False, False, True,
   ...:                      False, False, True,
   ...:                      False, False, False, True]
   ...:         terminals_tensor = torch.ByteTensor(terminals)
   ...:              

In [10]: terminals_tensor
Out[10]: tensor([ 0,  0,  1,  0,  0,  1,  0,  0,  0,  1], dtype=torch.uint8)

In [11]: terminals_tensor[0] = True
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-9cd5429faa79> in <module>()
----> 1 terminals_tensor[0] = True

TypeError: can't assign a bool to a torch.ByteTensor
1 Like

I would like to state that I found a workaround which allow me not to check for the type of what i put inside the ByteTensor by just using the new torch.tensor function. I do not know if this has any performance implication though.

terminals_tensor[0] = torch.tensor(True)  # works in pytorch 0.4.0