我为我的训练数据创建了一个 pyTorch 数据集,该数据集由特征和标签组成,以便能够使用本教程使用 pyTorch DataLoader。这对我的训练数据很有效,但KeyError: "['label'] not found in axis"
在加载测试 csv 文件时出现错误 (),除了没有“标签”列之外,它是相同的。
如果有帮助,预期的输入 csv 文件是 csv 文件中的 MNIST 数据,它具有 28*28 特征列。
import torch
class mnist(torch.utils.data.Dataset):
def __init__(self, csv_file):
self.train = pd.read_csv(csv_file)
self.train_x = self.train.drop("label", axis=1)
def __len__(self):
return len(self.train)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
if isinstance(idx, list):
idx_len = len(idx)
else:
idx_len = 1
X = np.asarray(self.train_x.iloc[idx], dtype=np.float32)
X = np.reshape(X, (1,28,28))
y = np.asarray(self.train.iloc[idx]['label'])
sample = {'X': X, 'y':y}
return torch.from_numpy(sample['X']), torch.from_numpy(sample['y'])