Python Pytorch中(据称)空批次的运行时错误

Python Pytorch中(据称)空批次的运行时错误,python,neural-network,pytorch,dataloader,Python,Neural Network,Pytorch,Dataloader,(更新了对该问题的进一步了解) 我有一个包含3000个图像的数据集,通过以下行进入数据加载器: training_left_eyes = torch.utils.data.DataLoader(train_dataset, batch_size=2,shuffle=True, drop_last=True) print(len(training_left_eyes)) #Outputs 1500 我的训练循环如下所示: for i,(data,output) in enumerate(trai

(更新了对该问题的进一步了解)

我有一个包含3000个图像的数据集,通过以下行进入数据加载器:

training_left_eyes = torch.utils.data.DataLoader(train_dataset, batch_size=2,shuffle=True, drop_last=True)
print(len(training_left_eyes)) #Outputs 1500
我的训练循环如下所示:

for i,(data,output) in enumerate(training_left_eyes):
      data,output = data.to(device),output.to(device)
      prediction = net(data)

      loss = costFunc(prediction,output)
      closs = loss.item()
      optimizer.zero_grad()
      loss.backward()
      optimizer.step()

      print("batch #{}".format(i))
      if i%100 == 0:
        print('[%d %d] loss: %.8f' % (epoch+1,i+1,closs/1000))
        closs = 0
张量“数据”和“输出”(标签)中的信息正确,系统工作正常,直到达到批号1500。我的所有批次均已满,为3000/2=1500,无剩余。一旦到达最后一批,就会出现一个运行时错误,指出存在0维输入大小。但是我不知道为什么会发生这种情况,因为enumerate(training_left_eyes)应该迭代数据加载器的值,这些值是满的

我在网上搜索了如何解决这个问题,一些人提到了DataLoader上的“drop_last=True”属性,尽管这样做是为了防止半空批次进入模型,但我还是尝试了,但没有成功

我开始太过盲目,似乎无法靠自己解决问题。我可以简单地插入一个if语句,但我认为这将是非常糟糕的做法,我想学习正确的解决方案

如果有帮助,以下是我的自定义数据集:

class LeftEyeDataset(torch.utils.data.Dataset):
  """Left eye retinography dataset. Normal/Not-normal"""

  def __init__(self, csv_file, root_dir, transform=None):
    """
    Args:
        csv_file (string): Path to the csv file with annotations.
        root_dir (string): Directory with all the images.
        transform (callable, optional): Optional transform to be applied
            on a sample.
    """
    self.labels  = label_mapping(csv_file)
    self.root_dir = root_dir
    self.transform = transform
    self.names = name_mapping(csv_file)

  def __len__(self):
    return len(self.labels)

  def __getitem__(self, idx):
    if torch.is_tensor(idx):
        idx = idx.tolist()

    img_name = self.root_dir +'/'+ self.names[idx]
    image = io.imread(img_name)
    label = self.labels[idx]

    if self.transform:
        image = self.transform(image)

    return image,label


def label_mapping(csv_file) -> np.array:
  df = read_excel(excel_file, 'Sheet1')
  x= []
  for key,value in df['Left-Diagnostic Keywords'].iteritems():
    if value=='normal fundus':
      x.append(1)
    else:
      x.append(0)
  x_tensor = torch.LongTensor(x)
  return x_tensor

def name_mapping(csv_file) -> list:
  #Reads the names of the excel file
  df = read_excel(excel_file, 'Sheet1')
  names= list()
  serie = df['Left-Fundus']
  for i in range(serie.size):
    names.append(df['Left-Fundus'][i])
  return names

如果需要,我可以提供任何附加代码

更新:在尝试解决问题一段时间后,我设法确定发生了什么。由于某些原因,在最后一批数据中,进入网络的数据是正常的,但就在第一层之前,发生了一些事情,数据消失了。在下一张图片中,您可以看到我在输入前进(self,x)之前和之后的打印。尺寸对齐,直到批号61(本例中,我将其从1500减少),在该批号中,它以某种方式通过打印两次。在该行之后,出现上述错误


在rubber duck调试了一段时间后,我意识到问题不在于培训,而在于验证集。代码被读取

for i,(data,output) in validate_left_eyes:
      data,output = data.to(device),output.to(device)
      prediction = net(data)
Validate_left_eyes_uu未被enumerate()包装,因此第一批数据为空。然后问题就解决了

我很抱歉,因为我的问题中没有提到这部分代码,因此答案并不那么直截了当