title: How to display graphs of loss and accuracy on pytorch using matplotlib
body: <p>I am new to pytorch, and i would like to know how to display graphs of loss and accuraccy And how exactly should i store these values,knowing that i'm applying a cnn model for image classification using CIFAR10.</p>
  <p>here is my current implementation :</p>
  <pre><code>    def train(num_epochs,optimizer,criterion,model):
          for epoch in range(num_epochs):
              for i, (images, labels) in enumerate(trainloader):
                  # origin shape: [4, 3, 32, 32] = 4, 3, 1024
                  # input_layer: 3 input channels, 6 output channels, 5 kernel size
                  images = images.to(device)
                  labels = labels.to(device)
      
                  # Forward pass
                  outputs = model(images)
                  loss = criterion(outputs, labels)
      
                  # Backward and optimize
                  optimizer.zero_grad()
                  loss.backward()
                  optimizer.step()
      
                  if (i+1) % 2000 == 0:
                      print (f'Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{n_total_steps}], Loss: {loss.item():.4f}')
                  
          
          PATH = './cnn.pth'
          torch.save(model.state_dict(), PATH)
      
      
      def test ():
          with torch.no_grad():
              n_correct = 0
              n_samples = 0
              n_class_correct = [0 for i in range(10)]
              n_class_samples = [0 for i in range(10)]
          
              for images, labels in testloader:
                  images = images.to(device)
                  labels = labels.to(device)
                  outputs = model(images)
                  # max returns (value ,index)
                  _, predicted = torch.max(outputs, 1)
                  n_samples += labels.size(0)
                  n_correct += (predicted == labels).sum().item()
              
                  for i in range(batch_size):
                      label = labels[i]
                      pred = predicted[i]
                  
                      if (label == pred):
                          n_class_correct[label] += 1
                      n_class_samples[label] += 1
      
              acc = 100.0 * n_correct / n_samples
              print(f'Accuracy of the network: {acc} %')
          
      
              for i in range(10):
                  acc = 100.0 * n_class_correct[i] / n_class_samples[i]
                  print(f'Accuracy of {classes[i]}: {acc} %')
              
              
              test_score = np.mean([100 * n_class_correct[i] / n_class_samples[i] for i in range(10)])
              print(&quot;the score test is : {0:.3f}%&quot;.format(test_score))
              return acc
  </code></pre>