mat1和mat2必须具有相同的数据类型。

3

我正在尝试构建一个神经网络,根据美国各县居民的教育水平来预测人均收入。 X和y具有相同的数据类型(我已经检查过了),但是我遇到了一个错误。 这是我的数据:

   county_FIPS state          county  per_capita_personal_income_2019  \
0        51013    VA   Arlington, VA                            97629   

   per_capita_personal_income_2020  per_capita_personal_income_2021  \
0                           100687                           107603    

   associate_degree_numbers_2016_2020  bachelor_degree_numbers_2016_2020  \
0                               19573                             132394   
 

这是我的网络

import torch
import pandas as pd
df = pd.read_csv("./input/US counties - education vs per capita personal income - results-20221227-213216.csv")
X = torch.tensor(df[["bachelor_degree_numbers_2016_2020", "associate_degree_numbers_2016_2020"]].values)
y = torch.tensor(df["per_capita_personal_income_2020"].values)

X.dtype
torch.int64

y.dtype
torch.int64

import torch.nn as nn
class BaseNet(nn.Module):
    def __init__(self, in_dim, hidden_dim, out_dim):
        super(BaseNet, self).__init__()
        self.classifier = nn.Sequential(
        nn.Linear(in_dim, hidden_dim, bias=True), 
        nn.ReLU(), 
        nn.Linear(feature_dim, out_dim, bias=True))
        
    def forward(self, x): 
        return self.classifier(x)

from torch import optim
import matplotlib.pyplot as plt
in_dim, hidden_dim, out_dim = 2, 20, 1
lr = 1e-3
epochs = 40
loss_fn = nn.CrossEntropyLoss()
classifier = BaseNet(in_dim, hidden_dim, out_dim)
optimizer = optim.SGD(classifier.parameters(), lr=lr)

def train(classifier, optimizer, epochs, loss_fn):
    classifier.train()
    losses = []
    for epoch in range(epochs):
        out = classifier(X)
        loss = loss_fn(out, y)
        loss.backward()
        optimizer.step()
        optimizer.zero_grad()
        losses.append(loss/len(X))
        print("Epoch {} train loss: {}".format(epoch+1, loss/len(X)))
    
    plt.plot([i for i in range(1, epochs + 1)])
    plt.xlabel("Epoch")
    plt.ylabel("Training Loss")
    plt.show()

train(classifier, optimizer, epochs, loss_fn)

这是我在尝试训练网络时遇到的错误的完整堆栈跟踪:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Input In [77], in <cell line: 39>()
     36     plt.ylabel("Training Loss")
     37     plt.show()
---> 39 train(classifier, optimizer, epochs, loss_fn)

Input In [77], in train(classifier, optimizer, epochs, loss_fn)
     24 losses = []
     25 for epoch in range(epochs):
---> 26     out = classifier(X)
     27     loss = loss_fn(out, y)
     28     loss.backward()

File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py:1194, in Module._call_impl(self, *input, **kwargs)
   1190 # If we don't have any hooks, we want to skip the rest of the logic in
   1191 # this function, and just call forward.
   1192 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1193         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1194     return forward_call(*input, **kwargs)
   1195 # Do not call functions when jit is used
   1196 full_backward_hooks, non_full_backward_hooks = [], []

Input In [77], in BaseNet.forward(self, x)
     10 def forward(self, x): 
---> 11     return self.classifier(x)

File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py:1194, in Module._call_impl(self, *input, **kwargs)
   1190 # If we don't have any hooks, we want to skip the rest of the logic in
   1191 # this function, and just call forward.
   1192 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1193         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1194     return forward_call(*input, **kwargs)
   1195 # Do not call functions when jit is used
   1196 full_backward_hooks, non_full_backward_hooks = [], []

File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/modules/container.py:204, in Sequential.forward(self, input)
    202 def forward(self, input):
    203     for module in self:
--> 204         input = module(input)
    205     return input

File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py:1194, in Module._call_impl(self, *input, **kwargs)
   1190 # If we don't have any hooks, we want to skip the rest of the logic in
   1191 # this function, and just call forward.
   1192 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1193         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1194     return forward_call(*input, **kwargs)
   1195 # Do not call functions when jit is used
   1196 full_backward_hooks, non_full_backward_hooks = [], []

File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/modules/linear.py:114, in Linear.forward(self, input)
    113 def forward(self, input: Tensor) -> Tensor:
--> 114     return F.linear(input, self.weight, self.bias)

RuntimeError: mat1 and mat2 must have the same dtype

更新

我尝试将X和y转换为浮点张量,但出现以下错误:expected scalar type Long but found Float。如果有了解PyTorch的人可以尝试运行这个笔记本,那将是一个很大的帮助。我在Kaggle和ML方面遇到了困难。


1
请展示完整的堆栈跟踪,以及任何您认为有用的.dtypes调试输出。 - J_H
我猜测你的输入数据类型与模型参数的数据类型不同。 - Polatkan Polat
我建议您展示整个代码,包括训练之前如何处理数据的部分。 - Elisha Senoo
2
你应该将 x 转换为浮点数。 - Caridorc
但是数据是整数,对吧? - George Garman
完整的代码现在已经上传。 - George Garman
2个回答

3
这是因为nn.Linear的参数dtype与您的输入的dtype不匹配;nn.Linear的默认dtype是torch.float32,而您的输入数据的dtype是float64
解决这个问题的方法可以解决您的问题,并解释了@Anonymous的答案为什么有效。
简而言之,在您的构造函数末尾添加self.double()即可解决问题。

1

我将输入转换为np.float32,这对我解决了一个类似的问题


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接