# Test the model# 測試模型with torch.no_grad():
correct =0
total =0for images, labels in test_loader:
images = images.reshape(-1, sequence_length, input_size).to(device)
labels = labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data,1)
total += labels.size(0)
correct +=(predicted == labels).sum().item()print('Test Accuracy of the model on the 10000 test images: {} %'.format(100*
correct /
total))# Save the model checkpoint
torch.save(model.state_dict(),'model.ckpt')
代碼
Epoch [1/2], Step [100/600], Loss:0.6361
Epoch [1/2], Step [200/600], Loss:0.3237
Epoch [1/2], Step [300/600], Loss:0.3490
Epoch [1/2], Step [400/600], Loss:0.0732
Epoch [1/2], Step [500/600], Loss:0.1803
Epoch [1/2], Step [600/600], Loss:0.1077
Epoch [2/2], Step [100/600], Loss:0.0433
Epoch [2/2], Step [200/600], Loss:0.1484
Epoch [2/2], Step [300/600], Loss:0.1480
Epoch [2/2], Step [400/600], Loss:0.1173
Epoch [2/2], Step [500/600], Loss:0.0247
Epoch [2/2], Step [600/600], Loss:0.0960
Test Accuracy of the model on the 10000 test images:98.07%