gpt4 book ai didi

python - pytorch 运行时错误 : Expected object of scalar type Double but got scalar type Float

转载 作者:行者123 更新时间:2023-12-03 14:20:04 27 4
gpt4 key购买 nike

我正在尝试为我的神经网络实现自定义数据集。但是在运行转发功能时出现此错误。代码如下。

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
import numpy as np

class ParamData(Dataset):
def __init__(self,file_name):
self.data = torch.Tensor(np.loadtxt(file_name,delimiter = ',')) #first place
def __len__(self):
return self.data.size()[0]
def __getitem__(self,i):
return self.data[i]

class Net(nn.Module):
def __init__(self,in_size,out_size,layer_size=200):
super(Net,self).__init__()
self.layer = nn.Linear(in_size,layer_size)
self.out_layer = nn.Linear(layer_size,out_size)

def forward(self,x):
x = F.relu(self.layer(x))
x = self.out_layer(x)
return x

datafile = 'data1.txt'

net = Net(100,1)
dataset = ParamData(datafile)
n_samples = len(dataset)

#dataset = torch.Tensor(dataset,dtype=torch.double) #second place
#net.float() #thrid place

net.forward(dataset[0]) #fourth place

在文件中 data1.txt是一个包含特定数字的 csv 格式文本文件,每个 dataset[i]大小为 100 × 1 torch.Tensor数据类型的对象 torch.float64 .错误信息如下:
Traceback (most recent call last):
File "Z:\Wrong.py", line 33, in <module>
net.forward(dataset[0])
File "Z:\Wrong.py", line 23, in forward
x = F.relu(self.layer(x))
File "E:\Python38\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
result = self.forward(*input, **kwargs)
File "E:\Python38\lib\site-packages\torch\nn\modules\linear.py", line 87, in forward
return F.linear(input, self.weight, self.bias)
File "E:\Python38\lib\site-packages\torch\nn\functional.py", line 1372, in linear
output = input.matmul(weight.t())
RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #2 'mat2' in call to _th_mm

看来我应该改变 dataset中数字的dtype至 torch.double .我试过这样的事情
  • 将第一行更改为 self.data = torch.tensor(np.loadtxt(file_name,delimiter = ','),dtype=torch.double)
  • 将第四位的行更改为 net.forward(dataset[0].double())
  • 取消注释第二或第三处的两行之一

  • 我认为这些是我从类似问题中看到的解决方案,但它们要么给出新的错误,要么什么都不做。我该怎么办?

    更新:所以我通过将第一个位置更改为
    self.data = torch.from_numpy(np.loadtxt(file_name,delimiter = ',')).float()

    这很奇怪,因为它与错误消息完全相反。这是一个错误吗?我还是想解释一下。

    最佳答案

    现在我对pytorch有了更多的经验,我想我可以解释错误信息了。这条线似乎

    RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #2 'mat2' in call to _th_mm

    实际上是指调用矩阵乘法时线性层的权重。由于输入是 double而权重是 float ,这对线有意义
    output = input.matmul(weight.t())

    预计权重为 double .

    关于python - pytorch 运行时错误 : Expected object of scalar type Double but got scalar type Float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60239051/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com