可以看到是输入数据(points)与模型权重的数据类型之间的冲突。
1、尝试将输入数据改为Double类型
一般的Tensor数据类型转化方法:
- 在Tensor数据后加long(), int(), double(), float(), byte()等函数就能将Tensor的类型进行转换
- 使用torch.type()函数,data为Tensor数据类型,data.type()给出data的类型,如果使用data.type(torch.DoubleTensor)则强制转换为torch.DoubleTensor类型的张量
points = points.double()
points = points.type(torch.DoubleTensor)
但是都失败了,数据类型没有变化,为啥呀??没有更改过来?
2、改不了输入数据,那就改参数,权重参数来自于线性层
self.Q_layer = nn.Linear(in_f, hidden_size)
torch.nn.Linear(in_features, # 输入的神经元个数
out_features, # 输出神经元个数
bias=True # 是否包含偏置
)
使用 torch.empty随机生成权重,好像可以指定数据类型的,尝试更改
self.Q_layer = nn.Linear(in_f, hidden_size,dtype=torch.float64)
报错:RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument mat2 in method wrapper_mm)
改了之后还是一堆奇奇怪怪的报错,官网 nn.Linear :This module supports TensorFloat32.还是在看看自己的输入数据怎么改把,回溯看输入数据是如何变为torch.float64的
3、看输入数据是如何变为torch.float64的
一开始输入数据的时候没有问题,继续调试发现中间有一个部分特征由numpy.array转化而来
xyz_new=torch.torch(xyz_new)
在这里输入前特征为numpy.array转化之后就变成torch.float64了,修改为
xyz_new =torch.FloatTensor(xyz_new)
修改之后特征输出为torch.float32,线性层也可以正常工作了
总结疑问:
1、Tensor数据类型转化为什么失败?是有什么使用条件吗?
2、torch.torch(xyz_new)为啥转化为torch.float64了,之前使用的明明是正常的呀
参考:
[报错]RuntimeError: expected scalar type Double but found Float(torch)-CSDN博客