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
            torch.nn.utils.clip_grad_norm_(model.parameters(), 5)
            print("Train Loss", train_losses)
            outputs, outputs_prob = model(Xtest) #TLNN
            test_loss = criterion(outputs, ytest)
            print("Validation Losses", valid_losses)
            _, predicted = torch.max(, 1)
            predicted_prob = outputs_prob[:,1]
            accu = accuracy_score(ytest, predicted)
            cof_mat = confusion_matrix(predicted, ytest)
                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')
        train_loss = np.average(train_losses)
        valid_loss = np.average(valid_losses)
        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(, 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