How to find training accuracy in pytorch
def train_and_test(e):
    epochs = e
    train_losses, test_losses, val_acc, train_acc= [], [], [], []
    valid_loss_min = np.Inf 
    model.train()
    print(&quot;Model Training started.....&quot;)
    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&quot; epoch {epoch + 1} batch {batch} completed&quot;) 
        test_loss = 0
        accuracy = 0
        with torch.no_grad():
            print(f&quot;validation started for {epoch + 1}&quot;)
            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(&quot;Epoch: {}/{}.. &quot;.format(epoch + 1, epochs),&quot;Training Loss: {:.3f}.. &quot;.format(train_losses[-1]), &quot;Valid Loss: {:.3f}.. &quot;.format(test_losses[-1]),
        &quot;Valid Accuracy: {:.3f}&quot;.format(accuracy / len(validloader)), &quot;train Accuracy: {:.3f}&quot;.format(running_loss / len(trainloader)))
      model.train() 
      if test_loss / len(validloader) &lt;= valid_loss_min:
          print('Validation loss decreased ({:.6f} --&gt; {:.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

my output is
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 --&gt; 0.072044).  Saving model ...


I am using dataset that is multi-set classification and getting training accuracy and training loss equal so I think there is error in training accuracy code.
training_acc.append(running_loss / len(trainloader))
&quot;train Accuracy: {:.3f}&quot;.format(running_loss / len(trainloader))

training_acc.append(accuracy / len(trainloader))
&quot;train Accuracy: {:.3f}&quot;.format(accuracy / len(trainloader))

is also not working fine

this method should be followed to plot training loses as well as accuracy
      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))

