Check yourself by running below code:
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=False, download=True, transform=transforms.Compose([
transforms.ToTensor(),
batch_size=1, shuffle=True)
for data, target in test_loader:
print(data, target)
break
这里,data基本上是一个灰度的MNIST图像,target是0和9之间的标签。
因此,在loss = F.nll_loss(output, target)中,output是模型预测(模型在给出图像/数据时的预测),target是给定图像的实际标签。
此外,在上述例子中,检查以下几行。
output = model(data) # shape [1, 10]
init_pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability
# If the initial prediction is wrong, don't bother attacking, just move on
if init_pred.item() != target.item():
continue