【问题标题】:How to find training accuracy in pytorch如何在 pytorch 中找到训练的准确性
【发布时间】:2021-03-02 16:48:47
【问题描述】:
def train_and_test(e):
    epochs = e
    train_losses, test_losses, val_acc, train_acc= [], [], [], []
    valid_loss_min = np.Inf 
    model.train()
    print("Model Training started.....")
    for epoch in range(epochs):
        running_loss = 0
        batch = 0
        for images, labels in trainloader:
            images, labels = images.to(device), labels.to(device)
            optimizer.zero_grad()
            outputs = model(images)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()
            running_loss += loss.item()
            batch += 1
            if batch % 10 == 0:
                print(f" epoch {epoch + 1} batch {batch} completed") 
        test_loss = 0
        accuracy = 0
        with torch.no_grad():
            print(f"validation started for {epoch + 1}")
            model.eval() 
            for images, labels in validloader:
                images, labels = images.to(device), labels.to(device)
                logps = model(images) 
                test_loss += criterion(logps, labels) 
                ps = torch.exp(logps)
                top_p, top_class = ps.topk(1, dim=1)
                equals = top_class == labels.view(*top_class.shape)
                accuracy += torch.mean(equals.type(torch.FloatTensor))

      train_losses.append(running_loss / len(trainloader))
      test_losses.append(test_loss / len(validloader))
      val_acc.append(accuracy / len(validloader))
      training_acc.append(running_loss / len(trainloader))
      scheduler.step()
      print("Epoch: {}/{}.. ".format(epoch + 1, epochs),"Training Loss: {:.3f}.. ".format(train_losses[-1]), "Valid Loss: {:.3f}.. ".format(test_losses[-1]),
        "Valid Accuracy: {:.3f}".format(accuracy / len(validloader)), "train Accuracy: {:.3f}".format(running_loss / len(trainloader)))
      model.train() 
      if test_loss / len(validloader) <= valid_loss_min:
          print('Validation loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(valid_loss_min, test_loss / len(validloader))) 
        torch.save({
            'epoch': epoch,
            'model': model,
            'model_state_dict': model.state_dict(),
            'optimizer_state_dict': optimizer.state_dict(),
            'loss': valid_loss_min
            }, path)
          valid_loss_min = test_loss / len(validloader)    

    print('Training Completed Succesfully !')    
    return train_losses, test_losses, val_acc ,train_acc

我的输出是

Model Training started.....
 epoch 1 batch 10 completed
 epoch 1 batch 20 completed
 epoch 1 batch 30 completed
 epoch 1 batch 40 completed
validation started for 1
Epoch: 1/2..  Training Loss: 0.088..  Valid Loss: 0.072..  Valid Accuracy: 0.979 train Accuracy: 0.088
Validation loss decreased (inf --> 0.072044).  Saving model ...

我使用的是多集分类的数据集,并且训练准确度和训练损失相等,所以我认为训练准确度代码存在错误。

training_acc.append(running_loss / len(trainloader))
"train Accuracy: {:.3f}".format(running_loss / len(trainloader))
training_acc.append(accuracy / len(trainloader))
"train Accuracy: {:.3f}".format(accuracy / len(trainloader))

也不能正常工作

【问题讨论】:

  • 我认为,训练精度 0.088 显示在输出中。请详细说明您的查询。举例并描述数据集。是二分类还是多集分类
  • 为什么你在计算你的测试损失的时候有这行ps = torch.exp(logps)
  • @gowridev 我使用的是多集分类的数据集,并且训练准确度和训练损失相等,所以我认为训练准确度代码存在错误。 training_acc.append(running_loss / len(trainloader)) "train Accuracy: {:.3f}".format(running_loss / len(trainloader)) 我试过 training_acc.append(accuracy / len(trainloader)) "train Accuracy: { :.3f}".format(accuracy / len(trainloader)) 但结果并不好。从我的学习训练准确度应该接近验证准确度
  • @Nerveless_child 作为网络的输出是对数概率,需要对概率取指数

标签: python pytorch conv-neural-network


【解决方案1】:

应该遵循这种方法来绘制训练损失和准确性

      for images , labels in trainloader:
         #start = time.time()
        images, labels = images.to(device), labels.to(device)
        optimizer.zero_grad()# Clear the gradients, do this because gradients are accumulated as 0 in each epoch

        # Forward pass  - compute outputs on input data using the model
        outputs = model(images) # modeling for each image batch
        loss = criterion(outputs,labels)  # calculating the loss

        # the backward pass
        loss.backward() # This is where the model learns by backpropagating
        optimizer.step() # And optimizes its weights here - Update the parameters
        running_loss += loss.item()

        # as Output of the network are log-probabilities, need to take exponential for probabilities
        ps = torch.exp(outputs)
        top_p , top_class = ps.topk(1,dim=1)
        equals = top_class == labels.view(*top_class.shape)

          # Convert correct_counts to float and then compute the mean
        acc += torch.mean(equals.type(torch.FloatTensor))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 2018-10-13
    • 1970-01-01
    • 2017-11-16
    • 2018-10-19
    相关资源
    最近更新 更多