Pytorch/ 自定义数据集加载器/ Numpy 数组到张量

时间:2021-04-05 12:08:25

标签: python pytorch-dataloader

嗨,我是 pytorch 的初学者,在理解构建自定义数据集时遇到问题

我正在使用 google colab,我的“数据”和“标签”已经从谷歌驱动器加载到我的 colab 文件中。 “data”是numpy数组,“labels”是Pandas DataFrame。

当我使用下面的第一个示例代码并迭代我的 DataLoader 时, 有一个运行时错误说:“运行时错误:堆栈期望每个张量的大小相等,但得到了~~”

class My_Dataset(Dataset):
  def __init__(self, x, y):
    x = shape_fit(x, (1,6,600))
    x, _ = normalize_data(x, x)
    y = np.array(y.iloc[:,0])
    self.x_data = x
    self.y_data = y
  
  def __len__(self):
    return len(self.x_data)

  def __getitem__(self, idx):
    x = torch.FloatTensor(self.x_data[idx])
    y = torch.FloatTensor(self.y_data[idx])
    return x, y

但是在我稍微更改如下代码后,我的加载程序运行良好。

class My_Dataset(Dataset):
  def __init__(self, x, y):
    x = shape_fit(x, (1,6,600))
    x, _ = normalize_data(x, x)
    y = np.array(y.iloc[:,0])
    self.x_data =  torch.Tensor(x)
    self.y_data =  torch.LongTensor(y)
  
  def __len__(self):
    return len(self.x_data)

  def __getitem__(self, idx):
    x = self.x_data[idx]
    y = self.y_data[idx]
    return x, y

有谁知道为什么会这样? 请解释一下调试中的细微变化是如何产生的??!!

0 个答案:

没有答案
相关问题