PyTorch:随机数据加载器

PyTorch: Shuffle DataLoader

有几种情况让我对改组数据加载器感到困惑,如下所示。

我在 train_loader 和 valid_loader 上都将“shuffle”参数设置为 False。那么我得到的结果如下

Epoch 1/4    loss=0.8802     val_loss=0.8202     train_acc=0.63      val_acc=0.63  
Epoch 2/4    loss=0.6993     val_loss=0.6500     train_acc=0.66      val_acc=0.72 
Epoch 3/4    loss=0.5363     val_loss=0.5385     train_acc=0.76      val_acc=0.80
Epoch 4/4    loss=0.4055     val_loss=0.5130     train_acc=0.85      val_acc=0.81

我在 train_loader 上将“shuffle”参数设置为 True,在 valid_loader 上设置为 False。那么我得到的结果如下

Epoch 1/4    loss=0.8928     val_loss=0.8284     train_acc=0.63      val_acc=0.63 
Epoch 2/4    loss=0.7308     val_loss=0.6263     train_acc=0.61      val_acc=0.73 
Epoch 3/4    loss=0.5594     val_loss=0.5046     train_acc=0.54      val_acc=0.81 
Epoch 4/4    loss=0.4304     val_loss=0.4525     train_acc=0.49      val_acc=0.82 

根据该结果,当我随机播放 train_loader 时,我的训练准确性表现较差 train_loader。

这是我的代码片段。

for epoch in range(n_epochs):
    model.train()
    avg_loss = 0.
    train_preds = np.zeros((len(train_X),len(le.classes_)))

    for i, (x_batch, y_batch) in enumerate(train_loader):
        y_pred = model(x_batch)
        loss = loss_fn(y_pred, y_batch)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        avg_loss += loss.item() / len(train_loader)
        train_preds[i * batch_size:(i+1) * batch_size] = F.softmax(y_pred).cpu().detach().numpy()            

    train_accuracy = sum(train_preds.argmax(axis=1) == y_train)/len(y_train)

    model.eval()        
    avg_val_loss = 0.
    val_preds = np.zeros((len(x_cv),len(le.classes_)))

    for i, (x_batch, y_batch) in enumerate(valid_loader):
        y_pred = model(x_batch).detach()
        avg_val_loss += loss_fn(y_pred, y_batch).item() / len(valid_loader)
        val_preds[i * batch_size:(i+1) * batch_size] =F.softmax(y_pred).cpu().numpy()
    val_accuracy = sum(val_preds.argmax(axis=1)==y_test)/len(y_test)

我是不是计算训练准确率有误?提前致谢

您正在将打乱的预测与未打乱的标签进行比较。要解决此问题,请计算每次迭代中准确预测的数量,并在最后计算整体准确度。

for epoch in range(n_epochs):
    model.train()
    avg_loss = 0.
    total_correct = 0
    total_samples = 0

    for i, (x_batch, y_batch) in enumerate(train_loader):
        y_pred = model(x_batch)
        loss = loss_fn(y_pred, y_batch)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        avg_loss += loss.item() / len(train_loader)
        total_correct += (torch.argmax(y_pred, 1) == y_batch).sum()
        total_samples += y_batch.shape[0]

    train_accuracy = total_correct / total_samples

(我没有测试过这段代码)