How to find the testing accuracy of a neural network in Pytorch. How to formulate the equation that will compare the predictions to the data

Viewed 141

I am having trouble finding the accuracy of my testing data. I am new in pytorch and keep getting an accuracy of 0 for every iteration. Testing and training loss values are reasonable and they improve as the iterations go on. However, I am not sure if I am evaluating the wrong data for the accuracy. I am using the predicted data and the test_target data. The test_input is feed into the model to generate the predictions. Once I can understand this part I will able to find also the training accuracy.

for e in range(num_epochs):
    
    optimizer.zero_grad()
    out = model(train_input)
    loss = loss_fn(out, train_target)
    loss.backward()
    optimizer.step()

    with torch.no_grad():
        pred = model(train_input, future_preds=future)
        training_loss = loss_fn(pred[:,:-future], train_target)
        train_loss.append(training_loss)


        
        pred = model(test_input, future_preds=future)
        test_loss = loss_fn(pred[:,:-future], test_target)
        testing_loss.append(test_loss)
        
        predictions = pred.detach().numpy()
        testing_accu= sum(pred[:,:-future]==test_target)/20
        test_accu.append(testing_accu)

        print("Testing Accuracy: ", ((pred[:,:-future] > 0.5) == test_target).float().mean().item())

 return testing_loss, train_loss, test_accu

The tensor.shape for my data is:

train_input : (80,999)

train_target: (80,999)

test_input : (20,999)

test_target : (20,999)

0 Answers
Related