Error outputting predictive label on Pythorch: Example has no attribute label

Asked 1 years ago, Updated 1 years ago, 404 views

I'm studying natural language processing, and I was writing code while referring to books.

If the last prediction is this data, the test data will also be labeled, so the correct answer rate will be printed with the following code, but the test data I am using only TEXT, so if you leave this code, I will get an error.

I tried to change the code, but it didn't work well.How can I output the predicted value?

error message

Attribute Error—Example has no attribute label.

Current state code

device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

net_trained.eval()# Put model into validation mode
net_trained.to(device)

epoch_corrects=0#epoch correct answers

for batch in(test_dl): # DataLoader for test data
    # batch is a Text and Lable dictionary object

    # Send data to GPU if GPU is available
    inputs=batch.Text[0].to(device)# sentence
    labels=batch.Label.to(device)#labels

    # forward calculation
    with torch.set_grad_enabled (False):

        # mask creation
        input_pad=1# Since the word ID is '<pad>':1
        input_mask=(inputs!=input_pad)

        # Enter in Transformer
        outputs,_,_=net_trained(inputs, input_mask)
        _,preds=torch.max(outputs,1)# Predict label

        # calculation of results
        # Update the total number of correct answers
        epoch_corrects+=torch.sum (preds==labels.data)

# correct answer rate
epoch_acc=epoch_corrects.double()/len(test_dl.dataset)

print('correct answer rate for test data {: .4f}'.format(len(test_dl.dataset), epoch_acc))

python natural-language-processing pytorch

2022-09-30 21:51

1 Answers

  • The tasks you are trying to accomplish with machine learning are document classification
  • The machine learning model has been learned
  • test dataset has no correct label

And all I want to do is look at the results of machine learning classification

So

Stop reading labels from datasets below

 labels=batch.Label.to(device)# label

Stop giving the correct answer rate below

# Calculating Results
        # Update the total number of correct answers
        epoch_corrects+=torch.sum (preds==labels.data)

# correct answer rate
epoch_acc=epoch_corrects.double()/len(test_dl.dataset)

I thought that was the right response.
If you have finished learning, you don't need the correct answer data, and since it's unknown, you can't give the correct answer rate.If the machine learning model is well generalized, the test data set should also be properly classified.


2022-09-30 21:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.