Why is toTensor returning a DoubleTensor when its suppose to return a FloatTensor?

According to the docs:

class ToTensor(object):
    """Convert a ``PIL.Image`` or ``numpy.ndarray`` to tensor.

    Converts a PIL.Image or numpy.ndarray (H x W x C) in the range
    [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0].

but I’m getting a DoubleTensor from a numpy array I saved. Why? How does one fix this?

My class:

class MyData(torch.utils.data.Dataset):
    def __init__(self,path_train,path_test=None,transform=None):
        self.transform = transform
        ## train
        np_train_data = np.load(path_train)
        self.X_train = np_train_data['X_train']
        self.Y_train = np_train_data['Y_train']
        ## test
        self.test = None
        if path_test is not None:
            np_train_data = np.load(path_train)
            self.X_train = np_train_data['X_test']
            self.Y_train = np_train_data['Y_test']

    def __getitem__(self, index):
        data = self.X_train[index]
        target = self.Y_train[index]
        if self.transform is not None:
            data = self.transform(data)
        return data,target
        #return data, target, index

    def __len__(self):
        return len(self.X_train)

It seems there is no type check using np.arrays: line of code.
As a workaround you could cast it in your __init__ function to np.float32.

1 Like

ok thats what Im doing right now. Is that a bug in pytorch or was that the intended behaviour?

Seems like a small bug to me. Or at least it’s inconsistent with the doc string. Thanks for reporting!

1 Like