如何解决pytorch中的目标越界错误?

如何解决pytorch中的目标越界错误?,pytorch,google-colaboratory,Pytorch,Google Colaboratory,模型和培训: 我使用了预训练移动网络和交叉熵损失,我读到了一些答案,说班级标签需要从0开始,我已经确认了 ... MobileNet = models.mobilenet_v2(pretrained = True) for param in MobileNet.parameters(): param.requires_grad = False torch.manual_seed(50) criterion = nn.CrossEnt

模型和培训:

我使用了预训练移动网络和交叉熵损失,我读到了一些答案,说班级标签需要从0开始,我已经确认了

   ...
    MobileNet = models.mobilenet_v2(pretrained = True)
    for param in MobileNet.parameters():
      param.requires_grad = False
      torch.manual_seed(50)
    
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(MobileNet.classifier.parameters(), lr=0.001)
    
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(MobileNet.classifier.parameters(), lr=0.001)    
    ...
...
    epochs = 10
    train_losses = []
    test_losses = []
    train_correct= []
    test_correct = []
    start_time = time.time()
    #Labs = []
    for i in range(epochs):
      trn_corr = 0
      tst_corr = 0
    
      for b, (images, labels) in enumerate(train_loader):
        torch.sub(labels,1)
        if torch.cuda.is_available():
          images = images.cuda()
          labels = labels.cuda()
    
        b+=1
    
        y_pred = MobileNet(images)
        #print(labels)
        #break
        #Labs.append(labels)
        #continue
        print(labels)
        print(y_pred)
        loss = criterion(y_pred, labels) # here is the out of bounds on 52 for first batch output below
        #print(y_pred)
        #print(labels)
        break
        predicted = torch.max(y_pred.data, 1)[1]
        batch_corr = (predicted == labels).sum()
        trn_corr += batch.corr
    
        accuracy = trn_corr.item()*100/(b*batch)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
      
    
    ...
我已经从标签中减去了1,标签从0运行到100,用于我确认的train_loader中的101个类

   ...
    MobileNet = models.mobilenet_v2(pretrained = True)
    for param in MobileNet.parameters():
      param.requires_grad = False
      torch.manual_seed(50)
    
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(MobileNet.classifier.parameters(), lr=0.001)
    
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(MobileNet.classifier.parameters(), lr=0.001)    
    ...
...
    epochs = 10
    train_losses = []
    test_losses = []
    train_correct= []
    test_correct = []
    start_time = time.time()
    #Labs = []
    for i in range(epochs):
      trn_corr = 0
      tst_corr = 0
    
      for b, (images, labels) in enumerate(train_loader):
        torch.sub(labels,1)
        if torch.cuda.is_available():
          images = images.cuda()
          labels = labels.cuda()
    
        b+=1
    
        y_pred = MobileNet(images)
        #print(labels)
        #break
        #Labs.append(labels)
        #continue
        print(labels)
        print(y_pred)
        loss = criterion(y_pred, labels) # here is the out of bounds on 52 for first batch output below
        #print(y_pred)
        #print(labels)
        break
        predicted = torch.max(y_pred.data, 1)[1]
        batch_corr = (predicted == labels).sum()
        trn_corr += batch.corr
    
        accuracy = trn_corr.item()*100/(b*batch)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
      
    
    ...
下面是输出

输出:

...

tensor([29, 33, 39, 85, 85, 39,  0, 23, 23, 76, 70,  2,  3, 57, 84, 11, 89, 31,
        59, 46, 83, 46, 52, 82, 44, 19, 52, 25, 33, 89, 77, 56, 22, 66, 45, 60,
        10, 71, 72, 16, 61,  4, 63, 15, 15, 57, 92, 17, 41, 80, 50, 56, 73, 80,
        68, 34, 47, 33, 31, 62, 68, 17, 47, 11])
tensor([[-0.9616, -1.1671, -1.1827],
        [-0.9615, -1.0641, -1.2995],
        [-0.9577, -1.4886, -0.9403],
        [-1.0647, -1.1032, -1.1289],
        [-1.1586, -1.0393, -1.1015],
        [-1.3143, -0.8895, -1.1379],
        [-0.9678, -0.8286, -1.6960],
        [-1.2635, -0.9934, -1.0583],
        [-0.8513, -1.3184, -1.1856],
        [-1.0806, -1.1071, -1.1084],
        [-1.1126, -1.1289, -1.0559],
        [-0.9698, -1.1128, -1.2303],
        [-1.0450, -1.2471, -1.0190],
        [-1.0669, -1.2297, -1.0119],
        [-1.0175, -1.0630, -1.2272],
        [-1.3280, -0.9455, -1.0598],
        [-0.9930, -1.2005, -1.1132],
        [-1.0558, -1.0148, -1.2392],
        [-1.1386, -1.1681, -0.9976],
        [-0.8060, -1.3254, -1.2460],
        [-0.8056, -1.1826, -1.3995],
        [-1.1848, -1.1338, -0.9879],
        [-0.9672, -1.1772, -1.1657],
        [-1.0675, -1.1348, -1.0946],
        [-1.1790, -1.0338, -1.0884],
        [-0.6344, -1.4177, -1.4806],
        [-1.1404, -0.9266, -1.2573],
        [-1.0258, -1.3760, -0.9445],
        [-0.8296, -1.2309, -1.3030],
        [-1.1527, -0.9346, -1.2328],
        [-0.9683, -1.2902, -1.0640],
        [-0.8827, -1.2443, -1.2100],
        [-0.8772, -1.0590, -1.4386],
        [-1.2291, -0.9050, -1.1944],
        [-0.9446, -1.0091, -1.3998],
        [-1.1590, -0.9005, -1.2736],
        [-0.8921, -1.4032, -1.0659],
        [-1.1511, -1.0787, -1.0681],
        [-1.1140, -1.2735, -0.9368],
        [-0.9793, -1.1024, -1.2299],
        [-0.9485, -1.2577, -1.1136],
        [-1.3164, -1.1032, -0.9160],
        [-1.3043, -1.2827, -0.7955],
        [-1.0703, -1.0073, -1.2314],
        [-1.2843, -1.1376, -0.9099],
        [-0.9937, -0.9318, -1.4441],
        [-1.1619, -1.2057, -0.9477],
        [-0.8990, -1.0851, -1.3659],
        [-1.3180, -0.7940, -1.2719],
        [-1.3387, -1.0477, -0.9492],
        [-0.8630, -1.2431, -1.2392],
        [-1.4398, -0.7549, -1.2277],
        [-1.0610, -1.1391, -1.0972],
        [-1.1071, -0.9328, -1.2872],
        [-1.1587, -1.2632, -0.9080],
        [-1.0558, -1.0159, -1.2378],
        [-1.1243, -1.0349, -1.1399],
        [-0.9771, -1.1900, -1.1414],
        [-1.0418, -1.1889, -1.0711],
        [-1.2620, -0.7551, -1.3985],
        [-1.0239, -0.9320, -1.3982],
        [-0.7640, -1.3455, -1.2953],
        [-1.2208, -0.9823, -1.1069],
        [-1.0224, -1.0562, -1.2293]])
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-71-e133409211d4> in <module>()
     25     print(labels)
     26     print(y_pred)
---> 27     loss = criterion(y_pred, labels)
     28     #print(y_pred)
     29     #print(labels)
...
。。。
张量([29,33,39,85,85,39,0,23,23,76,70,2,3,57,84,11,89,31,
59, 46, 83, 46, 52, 82, 44, 19, 52, 25, 33, 89, 77, 56, 22, 66, 45, 60,
10, 71, 72, 16, 61,  4, 63, 15, 15, 57, 92, 17, 41, 80, 50, 56, 73, 80,
68, 34, 47, 33, 31, 62, 68, 17, 47, 11])
张量([-0.9616,-1.1671,-1.1827],
[-0.9615, -1.0641, -1.2995],
[-0.9577, -1.4886, -0.9403],
[-1.0647, -1.1032, -1.1289],
[-1.1586, -1.0393, -1.1015],
[-1.3143, -0.8895, -1.1379],
[-0.9678, -0.8286, -1.6960],
[-1.2635, -0.9934, -1.0583],
[-0.8513, -1.3184, -1.1856],
[-1.0806, -1.1071, -1.1084],
[-1.1126, -1.1289, -1.0559],
[-0.9698, -1.1128, -1.2303],
[-1.0450, -1.2471, -1.0190],
[-1.0669, -1.2297, -1.0119],
[-1.0175, -1.0630, -1.2272],
[-1.3280, -0.9455, -1.0598],
[-0.9930, -1.2005, -1.1132],
[-1.0558, -1.0148, -1.2392],
[-1.1386, -1.1681, -0.9976],
[-0.8060, -1.3254, -1.2460],
[-0.8056, -1.1826, -1.3995],
[-1.1848, -1.1338, -0.9879],
[-0.9672, -1.1772, -1.1657],
[-1.0675, -1.1348, -1.0946],
[-1.1790, -1.0338, -1.0884],
[-0.6344, -1.4177, -1.4806],
[-1.1404, -0.9266, -1.2573],
[-1.0258, -1.3760, -0.9445],
[-0.8296, -1.2309, -1.3030],
[-1.1527, -0.9346, -1.2328],
[-0.9683, -1.2902, -1.0640],
[-0.8827, -1.2443, -1.2100],
[-0.8772, -1.0590, -1.4386],
[-1.2291, -0.9050, -1.1944],
[-0.9446, -1.0091, -1.3998],
[-1.1590, -0.9005, -1.2736],
[-0.8921, -1.4032, -1.0659],
[-1.1511, -1.0787, -1.0681],
[-1.1140, -1.2735, -0.9368],
[-0.9793, -1.1024, -1.2299],
[-0.9485, -1.2577, -1.1136],
[-1.3164, -1.1032, -0.9160],
[-1.3043, -1.2827, -0.7955],
[-1.0703, -1.0073, -1.2314],
[-1.2843, -1.1376, -0.9099],
[-0.9937, -0.9318, -1.4441],
[-1.1619, -1.2057, -0.9477],
[-0.8990, -1.0851, -1.3659],
[-1.3180, -0.7940, -1.2719],
[-1.3387, -1.0477, -0.9492],
[-0.8630, -1.2431, -1.2392],
[-1.4398, -0.7549, -1.2277],
[-1.0610, -1.1391, -1.0972],
[-1.1071, -0.9328, -1.2872],
[-1.1587, -1.2632, -0.9080],
[-1.0558, -1.0159, -1.2378],
[-1.1243, -1.0349, -1.1399],
[-0.9771, -1.1900, -1.1414],
[-1.0418, -1.1889, -1.0711],
[-1.2620, -0.7551, -1.3985],
[-1.0239, -0.9320, -1.3982],
[-0.7640, -1.3455, -1.2953],
[-1.2208, -0.9823, -1.1069],
[-1.0224, -1.0562, -1.2293]])
---------------------------------------------------------------------------
索引器回溯(最后一次最近调用)
在()
25印刷品(标签)
26打印(y_pred)
--->27损失=标准(y_pred,标签)
28#打印(y#U pred)
29#打印(标签)
...
请帮忙