Training accuracy LSTM NN

for epoch in range(n_epochs):
        for i in range(1):
            sp_idx = np.random.randint(0, Xtrain.shape[0], batch_size)
            images = Xtrain[sp_idx, :,:]
            labels = ytrain[sp_idx]
            images = images.reshape(-1, time_step, input_size) 
            
            # forward pass
            outputs, _= model(images) 
            loss = criterion(outputs, labels)
            
            # backward and optimize
            optimizer.zero_grad()
            loss.backward()
            torch.nn.utils.clip_grad_norm_(model.parameters(), 5)
            optimizer.step()
            train_losses.append(loss.item())
            print("Train Loss", train_losses)
            outputs, outputs_prob = model(Xtest) #TLNN
            test_loss = criterion(outputs, ytest)
            valid_losses.append(test_loss.item())
            print("Validation Losses", valid_losses)
        
   
            _, predicted = torch.max(outputs_prob.data, 1)
            predicted_prob = outputs_prob[:,1]
            accu = accuracy_score(ytest, predicted)
            cof_mat = confusion_matrix(predicted, ytest)
            
            try:
                accu_all.append(accu)
                auc_all.append(roc_auc_score(ytest.detach().numpy(), predicted_prob.detach().numpy()))
                f1_all.append(f1_score(ytest.detach().numpy(), predicted))
                recall_all.append(recall_score(ytest, predicted))
                precise_all.append(precision_score(ytest, predicted))
            except Exception as e:
                print(e, 'excepted')
                continue
                
        
        train_loss = np.average(train_losses)
        valid_loss = np.average(valid_losses)
        avg_train_losses.append(train_loss)
        avg_valid_losses.append(valid_loss)
        epoch_len = len(str(n_epochs))

How to find Training accuracy in this neural network? I am trying to plot Training accuracy , training loss ad valid loss for this network. @ptrblck

I don’t know what your exact use case is, but you should be able to calculate the accuracy by computing it between the model predictions and the targets.
E.g. for a multi-class classification take a look at the CIFAR10 tutorial which calculates the accuracy of the test datset as:

correct = 0
total = 0
# since we're not training, we don't need to calculate the gradients for our outputs
with torch.no_grad():
    for data in testloader:
        images, labels = data
        # calculate outputs by running images through the network
        outputs = net(images)
        # the class with the highest energy is what we choose as prediction
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print(f'Accuracy of the network on the 10000 test images: {100 * correct // total} %')
1 Like