torch.tensor(ndarray) works fine for me. However, the labels are a binary mask so I’m storing them as an ndarray of numpy int8. The following code:
label_dict[key] = torch.Tensor(label)
fails with:
RuntimeError: tried to construct a tensor from a nested float sequence, but found an item of type numpy.int8 at index (0, 0, 0)
If label is converted with .astype(np.uint8) ahead of time, I get:
RuntimeError: tried to construct a tensor from a nested int sequence, but found an item of type numpy.uint8 at index (0, 0, 0)
I can obviously preallocate an appropriately sized CharTensor and then copy myself, but it seems like that shouldn’t be necessary.
You can use the torch.from_numpy(ndarray) function to convert numpy array from numpy.
If you use it, you will see that int8 is not supported in torch, the error message by the from_numpy function will tell you which types are supported. In your case, I think you want to use the uint8 type (that is supported).
When you use torch.Tensor(ndarray) (which is a shorthand for torch.FloatTensor(ndarray) if you didn’t changed the default tensor type), it will try to build a FloatTensor from the ndarray, which is not an array of float in your case but int8.
If you want to build a tensor of uint8 elements, you will need to build it with the right type: torch.ByteTensor(ndarray).
I would advice using the from_numpy function as it finds the corresponding tensor type itself, while if you directly build the tensor, you will have to specify the type yourself.
Hi, torch.from_numpy(ndarray) works on uint8,
but if I want to covert the array to int8 what should I do.
I tried to use the torch.CharTensor but getting error
RuntimeError: tried to construct a tensor from a nested int sequence, but found an item of type numpy.int8 at index (0, 0, 0, 0)