卷积神经网络效果
构建卷积神经网络
- 卷积网络中的输入和层与传统神经网络有些区别,需重新设计,训练模块基本一致
1 | import torch |
首先读取数据
- 分别构建训练集和测试集(验证集)
- DataLoader来迭代取数据
1 | # 定义超参数 |
卷积网络模块构建
- 一般卷积层,relu层,池化层可以写成一个套餐
- 注意卷积最后结果还是一个特征图,需要把图转换成向量才能做分类或者回归任务
1 | class CNN(nn.Module): |
准确率作为评估标准
1 | def accuracy(predictions, labels): |
训练网络模型
1 | # 实例化 |
当前epoch: 0 [0/60000 (0%)] 损失: 2.298275 训练集准确率: 18.75% 测试集正确率: 16.69%
当前epoch: 0 [6400/60000 (11%)] 损失: 0.366936 训练集准确率: 77.09% 测试集正确率: 91.76%
当前epoch: 0 [12800/60000 (21%)] 损失: 0.197412 训练集准确率: 85.04% 测试集正确率: 95.32%
当前epoch: 0 [19200/60000 (32%)] 损失: 0.065437 训练集准确率: 88.57% 测试集正确率: 95.96%
当前epoch: 0 [25600/60000 (43%)] 损失: 0.245751 训练集准确率: 90.43% 测试集正确率: 97.05%
当前epoch: 0 [32000/60000 (53%)] 损失: 0.116508 训练集准确率: 91.65% 测试集正确率: 97.33%
当前epoch: 0 [38400/60000 (64%)] 损失: 0.106026 训练集准确率: 92.51% 测试集正确率: 97.47%
当前epoch: 0 [44800/60000 (75%)] 损失: 0.024781 训练集准确率: 93.20% 测试集正确率: 97.79%
当前epoch: 0 [51200/60000 (85%)] 损失: 0.040254 训练集准确率: 93.77% 测试集正确率: 97.44%
当前epoch: 0 [57600/60000 (96%)] 损失: 0.013604 训练集准确率: 94.19% 测试集正确率: 97.57%
当前epoch: 1 [0/60000 (0%)] 损失: 0.038379 训练集准确率: 100.00% 测试集正确率: 97.90%
当前epoch: 1 [6400/60000 (11%)] 损失: 0.091921 训练集准确率: 97.94% 测试集正确率: 98.26%
当前epoch: 1 [12800/60000 (21%)] 损失: 0.082685 训练集准确率: 97.88% 测试集正确率: 98.12%
当前epoch: 1 [19200/60000 (32%)] 损失: 0.030613 训练集准确率: 97.95% 测试集正确率: 98.53%
当前epoch: 1 [25600/60000 (43%)] 损失: 0.098491 训练集准确率: 97.96% 测试集正确率: 98.30%
当前epoch: 1 [32000/60000 (53%)] 损失: 0.078065 训练集准确率: 97.97% 测试集正确率: 98.50%
当前epoch: 1 [38400/60000 (64%)] 损失: 0.013370 训练集准确率: 98.02% 测试集正确率: 98.55%
当前epoch: 1 [44800/60000 (75%)] 损失: 0.065581 训练集准确率: 98.09% 测试集正确率: 98.65%
当前epoch: 1 [51200/60000 (85%)] 损失: 0.077535 训练集准确率: 98.12% 测试集正确率: 98.23%
当前epoch: 1 [57600/60000 (96%)] 损失: 0.007826 训练集准确率: 98.16% 测试集正确率: 98.65%
当前epoch: 2 [0/60000 (0%)] 损失: 0.170131 训练集准确率: 98.44% 测试集正确率: 98.57%
当前epoch: 2 [6400/60000 (11%)] 损失: 0.046841 训练集准确率: 98.64% 测试集正确率: 98.40%
当前epoch: 2 [12800/60000 (21%)] 损失: 0.095354 训练集准确率: 98.50% 测试集正确率: 98.58%
当前epoch: 2 [19200/60000 (32%)] 损失: 0.009594 训练集准确率: 98.58% 测试集正确率: 98.68%
当前epoch: 2 [25600/60000 (43%)] 损失: 0.017973 训练集准确率: 98.62% 测试集正确率: 98.82%
当前epoch: 2 [32000/60000 (53%)] 损失: 0.045781 训练集准确率: 98.66% 测试集正确率: 98.63%
当前epoch: 2 [38400/60000 (64%)] 损失: 0.056535 训练集准确率: 98.65% 测试集正确率: 98.94%
当前epoch: 2 [44800/60000 (75%)] 损失: 0.014779 训练集准确率: 98.66% 测试集正确率: 98.97%
当前epoch: 2 [51200/60000 (85%)] 损失: 0.010532 训练集准确率: 98.71% 测试集正确率: 98.50%
当前epoch: 2 [57600/60000 (96%)] 损失: 0.076463 训练集准确率: 98.68% 测试集正确率: 98.79%
1 |
1 |
卷积神经网络效果
https://zhangfuli.github.io/2020/09/03/卷积神经网络效果/