无法使用 pytorch [MNIST] 进行预测

not able to predict using pytorch [MNIST]

这里是pytorch菜鸟,正在努力学习。

link 到我的笔记本: https://gist.github.com/jagadeesh-kotra/412f371632278a4d9f6cb31a33dfcfeb

我的验证准确率为 95%。

我使用以下方法进行预测:

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)

问题是当我将结果提交给 kaggle 比赛时,我的准确率只有 10%,这和随机的一样好prediction.I不知道我做错了什么。

请帮忙:)

多半是打错了;当您想使用新创建的 predictated 结果时,您实际上使用的是 predicted:

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

其中 predicted 来自您链接代码的较早部分,它包含来自 validation 集而不是您的 test[=24= 的预测] 设置:

#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)

因此,您甚至都不会收到错误消息,因为 predicted 确实存在 - 它不是您真正想要使用的...您最终提交的是验证集的结果,而不是测试一个(两者都包含 10,000 个样本当然无济于事),因此您预计随机猜测的准确率为 ~ 10%...