0

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

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

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

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

有谁知道为什么会这样?请解释一下,轻微的变化是如何导致调试的??!!

4

0 回答 0