Pytorch 多输出回归中的异常

问题描述 投票:0回答:1

我对 Python 和 Pytorch 都很陌生,我正在尝试创建一个具有 4 个输入和 2 个输出的多输出回归神经网络,全部都是数字。

这是我的代码:

import pandas as pd  #For working with dataframes
import matplotlib.pyplot as plt  #For representation
import torch
from torch import nn
from torch.utils.data import Dataset, DataLoader

torch.manual_seed(42)

# Creating the dataset class
class Data(Dataset):
    # Constructor

    def __init__(self, train=True, transform=None):
        # Loading train.csv
        df = pd.read_csv('C:/path/myData.csv', skipinitialspace=True)
        feature = df.drop(['output1', 'output2'], axis=1)
        # Setting labels
        labels = df.drop(['featureA', 'featureB', 'featureC', 'featureD'], axis=1)

        self.x = feature
        self.y = labels
        self.len = self.x.shape[0]
        print(self.x)
        print(self.y)
        print(self.len)

    def __getitem__(self, index):
        return self.x[index], self.y[index]

    def __len__(self):
        return self.len

data_set = Data()
dataloader = torch.utils.data.DataLoader(data_set, batch_size=1)

# Creating a Multiple Linear Regression Model
class MultipleLinearRegression(torch.nn.Module):
    # Constructor
    def __init__(self, input_dim, output_dim):
        super(MultipleLinearRegression, self).__init__()
        self.linear = torch.nn.Linear(input_dim, output_dim)

    # Prediction
    def forward(self, x):
        y_pred = self.linear(x)
        return y_pred

# Creating the model object
MLR_model = MultipleLinearRegression(4,2)
print("The parameters: ", list(MLR_model.parameters()))

# defining the model optimizer
optimizer = torch.optim.SGD(MLR_model.parameters(), lr=0.1)
# defining the loss criterion
criterion = torch.nn.MSELoss()

# Train the model
losses = []
epochs = 20
for epoch in range(epochs):
    for x, y in dataloader:
        y_pred = MLR_model(x)
        loss = criterion(y_pred, y)
        losses.append(loss.item())
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    print(f"epoch = {epoch}, loss = {loss}")
print("Done training!")

# Plot the losses
plt.plot(losses)
plt.xlabel("no. of iterations")
plt.ylabel("total loss")
plt.show()

打印显示我的特征和输出数据似乎加载正确,但我从模型训练部分得到了一个关键错误/异常。我还确保我的数据条目中没有空格。这是输出:

C:\Users\myDir\AppData\Local\anaconda3\envs\PytorchEnv\python.exe C:\Users\myDir\PycharmProjects\Pytorch\main.py 
    FeatureA FeatureB FeatureC FeatureD
0        30     18     29     16
1        31     32     43     24
2        26     77     12     12
3        46     17     36     26
4        27     16     22     27
...     ...    ...    ...    ...
7172     19     26     13     20
7173     28     44     22     16
7174     14     27     15     38
7175     45     78     27     17
7176     36     20     19     26

[7177 rows x 4 columns]
        Output1    Output2
0    -27.360685  22.501504
1      7.101224  15.801227
2     47.432169  37.576150
3    -20.376308  39.803142
4    -15.633565  26.213762
...         ...        ...
7172  24.476350 -13.874069
7173  49.102965  14.028504
7174  34.066873 -34.725904
7175  52.643273  35.533358
7176  -6.990675  45.893473

[7177 rows x 2 columns]
7177
The parameters:  [Parameter containing:
tensor([[ 0.3823,  0.4150, -0.1171,  0.4593],
        [-0.1096,  0.1009, -0.2434,  0.2936]], requires_grad=True), Parameter containing:
tensor([ 0.4408, -0.3668], requires_grad=True)]
Traceback (most recent call last):
  File "C:\Users\myDir\AppData\Local\anaconda3\envs\PytorchEnv\Lib\site-packages\pandas\core\indexes\base.py", line 3653, in get_loc
    return self._engine.get_loc(casted_key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "pandas\_libs\index.pyx", line 147, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 176, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 7080, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 7088, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\myDir\PycharmProjects\Pytorch\main.py", line 147, in <module>
    for x, y in dataloader:
  File "C:\Users\myDir\AppData\Local\anaconda3\envs\PytorchEnv\Lib\site-packages\torch\utils\data\dataloader.py", line 633, in __next__
    data = self._next_data()
           ^^^^^^^^^^^^^^^^^
  File "C:\Users\myDir\AppData\Local\anaconda3\envs\PytorchEnv\Lib\site-packages\torch\utils\data\dataloader.py", line 677, in _next_data
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\myDir\AppData\Local\anaconda3\envs\PytorchEnv\Lib\site-packages\torch\utils\data\_utils\fetch.py", line 51, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\myDir\AppData\Local\anaconda3\envs\PytorchEnv\Lib\site-packages\torch\utils\data\_utils\fetch.py", line 51, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
            ~~~~~~~~~~~~^^^^^
  File "C:\Users\myDir\PycharmProjects\Pytorch\main.py", line 67, in __getitem__
    return self.x[index], self.y[index]
           ~~~~~~^^^^^^^
  File "C:\Users\myDir\AppData\Local\anaconda3\envs\PytorchEnv\Lib\site-packages\pandas\core\frame.py", line 3761, in __getitem__
    indexer = self.columns.get_loc(key)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\myDir\AppData\Local\anaconda3\envs\PytorchEnv\Lib\site-packages\pandas\core\indexes\base.py", line 3655, in get_loc
    raise KeyError(key) from err
KeyError: 0

Process finished with exit code 1

对于任何格式错误,我们深表歉意,感谢您的阅读。

python pandas deep-learning pytorch neural-network
1个回答
0
投票

您得到的错误是由于您访问数据帧的方式造成的,在使用索引时您应该使用

iloc
。 试试这个例子

import pandas as pd
import numpy as np
x = pd.DataFrame(np.random.randn(100, 10), columns=[f'col_{i}' for i in range(10)])
x.head()
print(x.iloc[0])
print(x[0]) # gives error
© www.soinside.com 2019 - 2024. All rights reserved.