导包
1 | import torch |
定义参数
1 | # Device configuration |
导入MNIST数据集,定义数据加载器
1 | # MNIST dataset |
定义网络
1 | # Fully connected neural network with one hidden layer |
定义损失函数和优化器
1 | criterion = nn.CrossEntropyLoss() |
训练模型
1 | total_step = len(train_loader) |
Epoch [1/10], Step [100/600], Loss: 0.3704
Epoch [1/10], Step [200/600], Loss: 0.2298
Epoch [1/10], Step [300/600], Loss: 0.2699
Epoch [1/10], Step [400/600], Loss: 0.2355
Epoch [1/10], Step [500/600], Loss: 0.2695
Epoch [1/10], Step [600/600], Loss: 0.1755
Epoch [2/10], Step [100/600], Loss: 0.1354
Epoch [2/10], Step [200/600], Loss: 0.0762
Epoch [2/10], Step [300/600], Loss: 0.0893
Epoch [2/10], Step [400/600], Loss: 0.1229
Epoch [2/10], Step [500/600], Loss: 0.0545
Epoch [2/10], Step [600/600], Loss: 0.0268
Epoch [3/10], Step [100/600], Loss: 0.0358
Epoch [3/10], Step [200/600], Loss: 0.0872
Epoch [3/10], Step [300/600], Loss: 0.0946
Epoch [3/10], Step [400/600], Loss: 0.0441
Epoch [3/10], Step [500/600], Loss: 0.1179
Epoch [3/10], Step [600/600], Loss: 0.0320
Epoch [4/10], Step [100/600], Loss: 0.0273
Epoch [4/10], Step [200/600], Loss: 0.0865
Epoch [4/10], Step [300/600], Loss: 0.0621
Epoch [4/10], Step [400/600], Loss: 0.0578
Epoch [4/10], Step [500/600], Loss: 0.0433
Epoch [4/10], Step [600/600], Loss: 0.0991
Epoch [5/10], Step [100/600], Loss: 0.0414
Epoch [5/10], Step [200/600], Loss: 0.0539
Epoch [5/10], Step [300/600], Loss: 0.0586
Epoch [5/10], Step [400/600], Loss: 0.0080
Epoch [5/10], Step [500/600], Loss: 0.0269
Epoch [5/10], Step [600/600], Loss: 0.0598
Epoch [6/10], Step [100/600], Loss: 0.0172
Epoch [6/10], Step [200/600], Loss: 0.0168
Epoch [6/10], Step [300/600], Loss: 0.0583
Epoch [6/10], Step [400/600], Loss: 0.0109
Epoch [6/10], Step [500/600], Loss: 0.0197
Epoch [6/10], Step [600/600], Loss: 0.0407
Epoch [7/10], Step [100/600], Loss: 0.0273
Epoch [7/10], Step [200/600], Loss: 0.0346
Epoch [7/10], Step [300/600], Loss: 0.0148
Epoch [7/10], Step [400/600], Loss: 0.0235
Epoch [7/10], Step [500/600], Loss: 0.0157
Epoch [7/10], Step [600/600], Loss: 0.0589
Epoch [8/10], Step [100/600], Loss: 0.0116
Epoch [8/10], Step [200/600], Loss: 0.0071
Epoch [8/10], Step [300/600], Loss: 0.0067
Epoch [8/10], Step [400/600], Loss: 0.0111
Epoch [8/10], Step [500/600], Loss: 0.0081
Epoch [8/10], Step [600/600], Loss: 0.0023
Epoch [9/10], Step [100/600], Loss: 0.0062
Epoch [9/10], Step [200/600], Loss: 0.0041
Epoch [9/10], Step [300/600], Loss: 0.0011
Epoch [9/10], Step [400/600], Loss: 0.0011
Epoch [9/10], Step [500/600], Loss: 0.0050
Epoch [9/10], Step [600/600], Loss: 0.0390
Epoch [10/10], Step [100/600], Loss: 0.0018
Epoch [10/10], Step [200/600], Loss: 0.0152
Epoch [10/10], Step [300/600], Loss: 0.0134
Epoch [10/10], Step [400/600], Loss: 0.0181
Epoch [10/10], Step [500/600], Loss: 0.0428
Epoch [10/10], Step [600/600], Loss: 0.0164
测试模型
1 | # In test phase, we don't need to compute gradients (for memory efficiency) |
Accuracy of the network on the 10000 test images: 97.72 %