Python Pytorch:将 FloatTensor 转换为 DoubleTensor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44717100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pytorch: Convert FloatTensor into DoubleTensor
提问by N8_Coder
I have 2 numpy arrays, which I convert into tensors to use the TensorDataset object.
我有 2 个 numpy 数组,我将它们转换为张量以使用 TensorDataset 对象。
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)
类型错误:addmm_ 收到了一个无效的参数组合 - 得到了 (int, int, torch.DoubleTensor, torch.FloatTensor),但需要以下之一:[...]
* (float beta, float alpha, torch.DoubleTensor mat1, torch. DoubleTensor mat2) 不匹配,因为某些参数的类型无效: (int, int, torch.DoubleTensor, torch.FloatTensor)
* (float beta, float alpha, torch.SparseDoubleTensor mat1, torch.DoubleTensor mat2) 没有匹配,因为某些参数的类型无效:(int, int, torch.DoubleTensor, torch.FloatTensor)
The last error comes from:
最后一个错误来自:
output.addmm_(0, 1, input, weight.t())
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?
正如您在我的代码中看到的,我尝试使用 .double() 转换张量 - 但这不起作用。为什么他将一个数组转换为 FloatTensor 对象,而将另一个数组转换为 DoubleTensor?有任何想法吗?
回答by mexmex
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
.
您的numpy
数组64-bit floating point
将被转换为torch.DoubleTensor
标准。现在,如果您将它们与您的模型一起使用,您需要确保您的模型参数也是Double
. 或者您需要确保您的numpy
数组被强制转换为Float
,因为模型参数通常被强制转换为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
.
Depeding,如果您想将模型参数、输入和目标转换Float
为Double
.
回答by jdhao
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. As @mexmex have said, convert data
to FloatTensor
to make it conform with the model parameter type.
这是因为在 PyTorch 中,您无法在不同类型的 Tensor 之间进行操作。您data
是DoubleTensor
,但模型参数是FloatTensor
。所以你会收到这个错误信息。正如@mexmex 所说,转换data
为FloatTensor
使其符合模型参数类型。
Do not do the other way around!Trying to convert the model to double is greatly discouraged by PyTorch devsas GPUs are not good at double precision computation. Also, floating point is pretty enough for deep learning.
不要反过来做!PyTorch 开发人员非常不鼓励尝试将模型转换为双精度,因为 GPU 不擅长双精度计算。此外,浮点数对于深度学习来说已经足够了。