Pytorch: Convert FloatTensor into DoubleTensor

前端 未结 2 1964
鱼传尺愫
鱼传尺愫 2021-02-07 20:55

I have 2 numpy arrays, which I convert into tensors to use the TensorDataset object.

import torch.utils.data as data_utils

X = np.zeros((100,30))
Y = np.zeros         


        
2条回答
  •  日久生厌
    2021-02-07 21:30

    Your numpy arrays are 64-bit floating point and will be converted to torch.DoubleTensor standardly. Now, if you use them with your model, you'll need to make sure that your model parameters are also Double. Or you need to make sure, that your numpy arrays are cast as Float, because model parameters are standardly cast as float.

    Hence, do either of the following:

    data_utils.TensorDataset(torch.from_numpy(X).float(), torch.from_numpy(Y).float())
    

    or do:

    model.double()
    

    Depeding, if you want to cast your model parameters, inputs and targets as Float or as Double.

提交回复
热议问题