Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytorch: Convert FloatTensor into DoubleTensor

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((100,30))

train = data_utils.TensorDataset(torch.from_numpy(X).double(), torch.from_numpy(Y))
train_loader = data_utils.DataLoader(train, batch_size=50, shuffle=True)

when I do:

for batch_idx, (data, target) in enumerate(train_loader):
    data, target = Variable(data), Variable(target)
    optimizer.zero_grad()
    output = model(data)               # error occurs here

I get the fallowing error:

TypeError: addmm_ received an invalid combination of arguments - got (int, int, torch.DoubleTensor, torch.FloatTensor), but expected one of: [...]
* (float beta, float alpha, torch.DoubleTensor mat1, torch.DoubleTensor mat2) didn't match because some of the arguments have invalid types: (int, int, torch.DoubleTensor, torch.FloatTensor)
* (float beta, float alpha, torch.SparseDoubleTensor mat1, torch.DoubleTensor mat2) didn't match because some of the arguments have invalid types: (int, int, torch.DoubleTensor, torch.FloatTensor)

The last error comes from:

output.addmm_(0, 1, input, weight.t())

As you see in my code I tried converting the tensor by using .double() - but this did not work. Why is he casting one array into a FloatTensor object and the other into a DoubleTensor? Any ideas?

like image 796
N8_Coder Avatar asked Jun 23 '17 08:06

N8_Coder


People also ask

How do I change the datatype of a torch tensor?

y = y. long() does the job. There are similar methods for other data types, such as int , char , float and byte . You can check different dtypes here.

How do you convert a float tensor to an int tensor?

How to typecast a float tensor to integer tensor and vice versa in pytorch? This is achieved by using . type(torch. int64) which will return the integer type values, even if the values are in float or in some other data type.

How do you convert a float to a tensor?

You just need to cast Tensor constant to numpy object,then can access by index. Adding . detach() before . numpy() helped me.

How do I convert a list to a PyTorch tensor?

To convert a Python list to a tensor, we are going to use the tf. convert_to_tensor() function and this function will help the user to convert the given object into a tensor. In this example, the object can be a Python list and by using the function will return a tensor.

Why PyTorch can not do operations between two different tensors?

This is because in PyTorch, you can not do operations between Tensor of different types. Your data is DoubleTensor, but the model parameter are FloatTensor. So you get this error message.

Can we convert inttensor to floattensor in PyTorch?

And we see that it is now a PyTorch FloatTensor. The one thing to notice, however, is 6, 2, 8, when we cast or converted the IntTensor back to a FloatTensor, it had not saved anywhere what numbers were past the decimal points.

Is it possible to convert PyTorch model to double precision?

Trying to convert the model to double is greatly discouraged by PyTorch devs as GPUs are not good at double precision computation. Also, floating point is pretty enough for deep learning.

How to convert a matrix to a float in neural network?

When feeding data trough the layer a matrix multiplication is applied and this multiplication requires both matrices to be of same data type. Your input which is fed into gen will be converted to float then.


1 Answers

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.

like image 63
mbpaulus Avatar answered Oct 14 '22 21:10

mbpaulus