Accuracy per epoch in PyTorch
I have made a chatbot using pytorch and would like to display accuracy on every epoch. I am not quite understanding how to do that. I can display loss but cant figure out how to display my accuracy
Here is my code :-
from nltk_utils import tokenize, stem, bag_of_words
import json
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from model import NeuralNet
from torch.autograd import Variable

all_words=[]
tags=[]
xy=[]
questionsP1=[]
questionsP2=[]
questionsP3=[]
questionsP4=[]
questionTag={}

with open('new.json', encoding=&quot;utf8&quot;) as file:
        data = json.load(file)    
    
for intent in data[&quot;intents&quot;]:
    for proficiency in intent[&quot;proficiency&quot;]:
        for questions in proficiency[&quot;questions&quot;]:
            for responses in questions[&quot;responses&quot;]:        
                wrds = tokenize(responses)
                all_words.extend(wrds)
                xy.append((wrds, questions[&quot;tag&quot;]))
                
            if questions[&quot;tag&quot;] in tags:
                print(questions[&quot;tag&quot;])
        
            if questions[&quot;tag&quot;] not in tags:
                tags.append(questions[&quot;tag&quot;])
                    
            if proficiency[&quot;level&quot;] == &quot;P1&quot;:
                questionsP1.append(questions[&quot;question&quot;])
                questionTag[questions[&quot;question&quot;]]=questions[&quot;tag&quot;]
                    
            if proficiency[&quot;level&quot;] == &quot;P2&quot;:
                questionsP2.append(questions[&quot;question&quot;])
                questionTag[questions[&quot;question&quot;]]=questions[&quot;tag&quot;]
                
            if proficiency[&quot;level&quot;] == &quot;P3&quot;:
                questionsP3.append(questions[&quot;question&quot;])
                questionTag[questions[&quot;question&quot;]]=questions[&quot;tag&quot;]
                
            if proficiency[&quot;level&quot;] == &quot;P4&quot;:
                questionsP4.append(questions[&quot;question&quot;])
                questionTag[questions[&quot;question&quot;]]=questions[&quot;tag&quot;]

ignore_words = ['?', '!', '.', ',']
all_words = [stem(x) for x in all_words if x not in ignore_words]
all_words = sorted(set(all_words))
tags = sorted(set(tags))


X_train = []
y_train = []

for tokenized_response, tag in xy:
    bag = bag_of_words(tokenized_response, all_words)
    print(bag)
    X_train.append( bag )

    label = tags.index( tag )
    y_train.append( label )
print(y_train)

X_train = np.array( X_train )
y_train = np.array( y_train )

class ChatDataset(Dataset):
    def __init__(self):
        self.n_samples = len(X_train)
        self.x_data = X_train
        self.y_data = y_train
    
    def __getitem__(self, index):
        return self.x_data[index], self.y_data[index]
    
    def __len__(self):
        return self.n_samples

#HyperParameters
batch_size = 8
hidden_size = 8
output_size = len(tags)
input_size = len(X_train[0])
learning_rate = 0.001
num_epochs = 994

dataset = ChatDataset()
train_loader = DataLoader(dataset = dataset, batch_size=batch_size, shuffle = True, num_workers = 2)


device = 'cpu'
model = NeuralNet(input_size, hidden_size, output_size).to(device)

#loss and optimizer 
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr = learning_rate)

for epoch in range( num_epochs ):
    for (words, labels) in train_loader:
        words = words.to(device)
        labels = labels.to(device)

        #Forward
        outputs = model(words)
        loss = criterion(outputs, labels)

        #backward and optimizer step
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        print(f'epoch {epoch + 1}/ {num_epochs}, loss={loss.item(): .4f}')

print(f'final loss, loss={loss.item(): .4f}')

data = {
    &quot;model_state&quot;: model.state_dict(),
    &quot;input_size&quot;: input_size,
    &quot;output_size&quot;: output_size,
    &quot;hidden_size&quot;: hidden_size,
    &quot;all_words&quot;: all_words,
    &quot;tags&quot;: tags,
}

FILE = &quot;data.pth&quot;
torch.save(data, FILE)

with open('new.json', 'r') as f:
    intents = json.load(f)

bot_name = &quot;Sam&quot;
while True:
    sentence = input(&quot;You: &quot;)
    if sentence == 'quit':
        break
    sentence = tokenize(sentence)
    X = bag_of_words(sentence, all_words)
    X = X.reshape( 1, X.shape[0])
    X = torch.from_numpy( X )

    output = model( X )
    _, predicted = torch.max(output, dim=1)
    tag = tags[predicted.item()]
    print(tag)

    probs = torch.softmax(output, dim=1)
    probs = probs[0][predicted.item()]
    
    print( probs.item() )

    if probs.item() &gt; 0.75:
        for intent in intents[&quot;intents&quot;]:
            for proficiency in intent[&quot;proficiency&quot;]:
                for questions in proficiency[&quot;questions&quot;]:
                    if questions[&quot;tag&quot;] == tag:
                        print(f'{bot_name}: {questions[&quot;question&quot;]}')
    else:
        print(f'{bot_name}: Probability Too Low')

print(f'Training Complete. File saved to {FILE}')

My chatbot is working inverselt... i am trying to map the answer to the right question.
Any help would be appreciated.

According to your code labels contains the indices that should have the highest values in outputs in order for the samples to be counted as correct predictions.
So to calculate the validation accuracy:
correct = 0
total = 0
model.eval()
with torch.no_grad():
    for (words, labels) in validation_loader:
        words = words.to(device)
        labels = labels.to(device)
        total += labels.shape[0]

        outputs = model(words)

        correct += torch.sum(labels == outputs.argmax(dim=-1))

accuracy = correct / total

