本节需要掌握的内容:
参数初始化
在torch.nn.init
中提供了多种初始化方式,可以直接对tensor
进行初始化,而网络的参数是tensor
的子类,所以可以直接对某一层初始化。1
2init.xavier_uniform(self.conv1.weight)
init.constant(self.conv1.bias, 0.1)对整个网络初始化如下:
1
2
3
4
5
6def weights_init(m):
if isinstance(m, nn.Conv2d):
xavier(m.weight.data)
xavier(m.bias.data)
net = Net()
net.apply(weights_init)权重衰减设置
1
2
3if epoch > 0 and epoch % 10 == 0:
for param_group in optimizer.param_groups:
param_group['lr'] = param_group['lr'] * lr_decay
导入需要的包
1 | import torch |
构建Residual块
1 | class ResidualBlock(nn.Module): |
1 | class ResNet18(nn.Module): |
超参数设置,gpu or cpu
1 | batch_size = 128 |
device(type='cuda')
准备数据集
1 | transform = transforms.Compose([ |
实例化网络、优化器、评估方法
1 | net = ResNet18(num_class=10).to(device) |
开始训练
1 | for epoch in range(epoch_nums): |
epoch: 1, train loss: 1.697, test acc: 0.504, time cost: 38.9 sec
epoch: 2, train loss: 1.198, test acc: 0.627, time cost: 39.4 sec
epoch: 3, train loss: 0.907, test acc: 0.708, time cost: 40.2 sec
epoch: 4, train loss: 0.731, test acc: 0.754, time cost: 39.3 sec
epoch: 5, train loss: 0.632, test acc: 0.781, time cost: 39.4 sec
epoch: 6, train loss: 0.562, test acc: 0.782, time cost: 39.8 sec
epoch: 7, train loss: 0.508, test acc: 0.812, time cost: 39.9 sec
epoch: 8, train loss: 0.463, test acc: 0.813, time cost: 39.9 sec
epoch: 9, train loss: 0.432, test acc: 0.831, time cost: 40.2 sec
epoch: 10, train loss: 0.396, test acc: 0.843, time cost: 40.6 sec
epoch: 11, train loss: 0.312, test acc: 0.860, time cost: 40.7 sec
epoch: 12, train loss: 0.283, test acc: 0.866, time cost: 40.8 sec
epoch: 13, train loss: 0.267, test acc: 0.870, time cost: 41.1 sec
epoch: 14, train loss: 0.252, test acc: 0.866, time cost: 41.6 sec
epoch: 15, train loss: 0.243, test acc: 0.870, time cost: 41.5 sec
epoch: 16, train loss: 0.232, test acc: 0.874, time cost: 41.2 sec
epoch: 17, train loss: 0.218, test acc: 0.876, time cost: 41.4 sec
epoch: 18, train loss: 0.206, test acc: 0.874, time cost: 40.8 sec
epoch: 19, train loss: 0.197, test acc: 0.874, time cost: 41.3 sec
epoch: 20, train loss: 0.189, test acc: 0.878, time cost: 40.9 sec
epoch: 21, train loss: 0.148, test acc: 0.892, time cost: 41.1 sec
epoch: 22, train loss: 0.130, test acc: 0.888, time cost: 41.5 sec
epoch: 23, train loss: 0.120, test acc: 0.892, time cost: 40.3 sec
epoch: 24, train loss: 0.117, test acc: 0.895, time cost: 41.5 sec
epoch: 25, train loss: 0.115, test acc: 0.893, time cost: 41.7 sec
epoch: 26, train loss: 0.109, test acc: 0.888, time cost: 41.6 sec
epoch: 27, train loss: 0.103, test acc: 0.888, time cost: 41.9 sec
epoch: 28, train loss: 0.098, test acc: 0.890, time cost: 42.6 sec
epoch: 29, train loss: 0.098, test acc: 0.891, time cost: 42.2 sec
epoch: 30, train loss: 0.094, test acc: 0.894, time cost: 40.7 sec