not able to predict using pytorch [MNIST]

2019-08-08 15:15发布

问题:

pytorch noob here,trying to learn.

link to my notebook: https://gist.github.com/jagadeesh-kotra/412f371632278a4d9f6cb31a33dfcfeb

I get validation accuracy of 95%.

i use the following to predict:

m.eval()

testset_predictions = []
for batch_id,image in enumerate(test_dataloader):
    image = torch.autograd.Variable(image[0])
    output = m(image)
    _, predictated = torch.max(output.data,1)
    for prediction in predicted:
        testset_predictions.append(prediction.item())

len(testset_predictions)

The problem is i get only 10% accuracy when i submit the result to kaggle competition,which is as good as random prediction.I cant figure out what i'm doing wrong.

please help :)

回答1:

Most probably it is due to a typo; while you want to use the newly created predictated outcomes, you actually use predicted:

_, predictated = torch.max(output.data,1)
    for prediction in predicted:

which predicted comes from earlier in your linked code, and it contains predictions from the validation set instead of your test set:

#validation

# ...

for batch_idx, (data, target) in enumerate(val_dataloader):

    data, target = Variable(data), Variable(target)

    output = m.forward(data)

    _, predicted = torch.max(output.data,1)

So, you don't even get an error message, because predicted indeed exists - it's just not what you actually want to use... You end up submitting the results for the validation set instead of the test one (it certainly doesn't help that both consist of 10,000 samples), hence you expectedly get a random guessing accuracy of ~ 10%...