Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
Dive-into-DL-PyTorch
提交
0c3397f5
D
Dive-into-DL-PyTorch
项目概览
OpenDocCN
/
Dive-into-DL-PyTorch
通知
9
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Dive-into-DL-PyTorch
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0c3397f5
编写于
3月 28, 2019
作者:
S
shusentang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
finish code 5.10
上级
f6f44807
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
88 addition
and
36 deletion
+88
-36
code/chapter05_CNN/5.10_batch-norm.ipynb
code/chapter05_CNN/5.10_batch-norm.ipynb
+88
-36
未找到文件。
code/chapter05_CNN/5.10_batch-norm.ipynb
浏览文件 @
0c3397f5
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 5.10 批量归一化"
]
},
{
"cell_type": "code",
"execution_count": 1,
...
...
@@ -29,6 +36,13 @@
"print(device)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5.10.2 从零开始实现"
]
},
{
"cell_type": "code",
"execution_count": 2,
...
...
@@ -64,28 +78,6 @@
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = torch.tensor(1.0, device=device)\n",
"y = torch.tensor(1.0).to(x.device)\n",
"x.device == y.device"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"class BatchNorm(nn.Module):\n",
...
...
@@ -107,16 +99,23 @@
" if self.moving_mean.device != X.device:\n",
" self.moving_mean = self.moving_mean.to(X.device)\n",
" self.moving_var = self.moving_var.to(X.device)\n",
" # 保存更新过的moving_mean和moving_var\n",
" # 保存更新过的moving_mean和moving_var
, Module实例的traning属性默认为true, 调用.eval()后设成false
\n",
" Y, self.moving_mean, self.moving_var = batch_norm(self.training, \n",
" X, self.gamma, self.beta, self.moving_mean,\n",
" self.moving_var, eps=1e-5, momentum=0.9)\n",
" return Y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.10.2.1 使用批量归一化层的LeNet"
]
},
{
"cell_type": "code",
"execution_count":
5
,
"execution_count":
4
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -142,7 +141,7 @@
},
{
"cell_type": "code",
"execution_count":
6
,
"execution_count":
5
,
"metadata": {},
"outputs": [
{
...
...
@@ -150,11 +149,11 @@
"output_type": "stream",
"text": [
"training on cuda\n",
"epoch 1, loss 0.0039, train acc 0.7
87, test acc 0.825, time 4.6
sec\n",
"epoch 2, loss 0.0018, train acc 0.86
5, test acc 0.837, time 2.6
sec\n",
"epoch 3, loss 0.0014, train acc 0.8
80, test acc 0.80
7, time 2.6 sec\n",
"epoch 4, loss 0.0013, train acc 0.88
7, test acc 0.860, time 2.6
sec\n",
"epoch 5, loss 0.0012, train acc 0.89
5, test acc 0.844, time 2.5
sec\n"
"epoch 1, loss 0.0039, train acc 0.7
90, test acc 0.835, time 2.9
sec\n",
"epoch 2, loss 0.0018, train acc 0.86
6, test acc 0.821, time 3.2
sec\n",
"epoch 3, loss 0.0014, train acc 0.8
79, test acc 0.85
7, time 2.6 sec\n",
"epoch 4, loss 0.0013, train acc 0.88
6, test acc 0.820, time 2.7
sec\n",
"epoch 5, loss 0.0012, train acc 0.89
1, test acc 0.859, time 2.8
sec\n"
]
}
],
...
...
@@ -169,17 +168,17 @@
},
{
"cell_type": "code",
"execution_count":
7
,
"execution_count":
6
,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(tensor([ 1.
0408, 1.2496, 0.9876, 0.9680, 1.1117, 0.9562
], device='cuda:0'),\n",
" tensor([
-0.5720, 0.1018, -0.5304, -0.5216, 0.3563, -0.128
0], device='cuda:0'))"
"(tensor([ 1.
2537, 1.2284, 1.0100, 1.0171, 0.9809, 1.1870
], device='cuda:0'),\n",
" tensor([
0.0962, 0.3299, -0.5506, 0.1522, -0.1556, 0.224
0], device='cuda:0'))"
]
},
"execution_count":
7
,
"execution_count":
6
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -188,12 +187,65 @@
"net[1].gamma.view((-1,)), net[1].beta.view((-1,))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5.10.3 简洁实现"
]
},
{
"cell_type": "code",
"execution_count":
null
,
"execution_count":
7
,
"metadata": {},
"outputs": [],
"source": []
"source": [
"net = nn.Sequential(\n",
" nn.Conv2d(1, 6, 5), # in_channels, out_channels, kernel_size\n",
" nn.BatchNorm2d(6),\n",
" nn.Sigmoid(),\n",
" nn.MaxPool2d(2, 2), # kernel_size, stride\n",
" nn.Conv2d(6, 16, 5),\n",
" nn.BatchNorm2d(16),\n",
" nn.Sigmoid(),\n",
" nn.MaxPool2d(2, 2),\n",
" d2l.FlattenLayer(),\n",
" nn.Linear(16*4*4, 120),\n",
" nn.BatchNorm1d(120),\n",
" nn.Sigmoid(),\n",
" nn.Linear(120, 84),\n",
" nn.BatchNorm1d(84),\n",
" nn.Sigmoid(),\n",
" nn.Linear(84, 10)\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"training on cuda\n",
"epoch 1, loss 0.0054, train acc 0.767, test acc 0.795, time 2.0 sec\n",
"epoch 2, loss 0.0024, train acc 0.851, test acc 0.748, time 2.0 sec\n",
"epoch 3, loss 0.0017, train acc 0.872, test acc 0.814, time 2.2 sec\n",
"epoch 4, loss 0.0014, train acc 0.883, test acc 0.818, time 2.1 sec\n",
"epoch 5, loss 0.0013, train acc 0.889, test acc 0.734, time 1.8 sec\n"
]
}
],
"source": [
"batch_size = 256\n",
"train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size=batch_size)\n",
"\n",
"lr, num_epochs = 0.001, 5\n",
"optimizer = torch.optim.Adam(net.parameters(), lr=lr)\n",
"d2l.train_ch5(net, train_iter, test_iter, batch_size, optimizer, device, num_epochs)"
]
}
],
"metadata": {
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录