文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
注:本文为李沐大神的《动手学深度学习》的课程笔记!
注意:mxnet随机种子设为1时,loss一直为nan,经测试,种子为2时,jupyter-notebook有时会出现nan,但在命令行执行python文件多次都不会出现nan。
获取数据
1 | # 导入mxnet |
1 | # 取出单条数据 |
('example shape: ', (28L, 28L, 1L), 'label:', 2.0)
数据可视化
1 | import matplotlib.pyplot as plt |
['pullover', 'ankle boot', 'shirt', 't-shirt', 'dress,', 'coat', 'coat', 'sandal', 'coat']
数据读取
1 | # 批数据大小 |
初始化模型参数
1 | # 输入数据大小 |
1 | # 需要计算梯度, 添加自动求导 |
定义模型
1 | # 定义softmax |
1 | # 测试softmax |
[[ 0.06774449 0.5180918 0.1474141 0.11459844 0.1521512 ]
[ 0.23102701 0.47666225 0.10536087 0.09706162 0.08988826]]
<NDArray 2x5 @cpu(0)>
[ 1. 1.]
<NDArray 2 @cpu(0)>
1 | # 定义模型 |
交叉熵损失函数
1 | # 定义交叉熵损失 |
计算精度
1 | # 计算分类准确率 |
1 | # 评估整个数据集的分类精度 |
1 | # 测试随机初始化参数的分类准确率 |
0.15156249999999999
训练
1 | # 定义SGD |
1 | # 学习率 |
Epoch 0. Loss: 3.725043, Train acc 0.487916, Test acc 0.602344
Epoch 1. Loss: 1.965692, Train acc 0.632812, Test acc 0.666895
Epoch 2. Loss: 1.630362, Train acc 0.677349, Test acc 0.691016
Epoch 3. Loss: 1.450728, Train acc 0.701690, Test acc 0.710352
Epoch 4. Loss: 1.329035, Train acc 0.717996, Test acc 0.720410
- learning_rate = 1
1 | Epoch 0. Loss: 18.803541, Train acc 0.081366, Test acc 0.085840 |
- learning_rate = 0.01
1 | Epoch 0. Loss: 10.406917, Train acc 0.115165, Test acc 0.169629 |
预测
1 | # 从测试集中取数据 |
True labels:
['t-shirt', 'trouser', 'pullover', 'pullover', 'dress,', 'pullover', 'bag', 'shirt', 'sandal']
Predicted labels:
['shirt', 'trouser', 'pullover', 'pullover', 'dress,', 'bag', 'bag', 'shirt', 'sandal']