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))
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.
571 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
617 Uncaught (inpromise) Error on Electron: An object could not be cloned
578 Understanding How to Configure Google API Key
581 PHP ssh2_scp_send fails to send files as intended
572 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.