Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
Dive-into-DL-PyTorch
提交
d99e947b
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,体验更适合开发者的 AI 搜索 >>
提交
d99e947b
编写于
5月 29, 2019
作者:
S
shusentang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix bug
上级
adbcaf7f
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
33 addition
and
34 deletion
+33
-34
code/chapter05_CNN/5.5_lenet.ipynb
code/chapter05_CNN/5.5_lenet.ipynb
+33
-34
未找到文件。
code/chapter05_CNN/5.5_lenet.ipynb
浏览文件 @
d99e947b
...
...
@@ -12,8 +12,8 @@
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2019-0
3-18T14:00:36.424190
Z",
"start_time": "2019-0
3-18T14:00:35.632104
Z"
"end_time": "2019-0
5-29T13:57:37.383972
Z",
"start_time": "2019-0
5-29T13:57:34.520559
Z"
}
},
"outputs": [
...
...
@@ -21,12 +21,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"
0.4.1
\n",
"
1.0.0
\n",
"cuda\n"
]
}
],
"source": [
"import os\n",
"import time\n",
"import torch\n",
"from torch import nn, optim\n",
...
...
@@ -34,6 +35,8 @@
"import sys\n",
"sys.path.append(\"..\") \n",
"import d2lzh_pytorch as d2l\n",
"\n",
"os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\n",
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
"\n",
"print(torch.__version__)\n",
...
...
@@ -52,10 +55,9 @@
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2019-03-18T14:00:36.462703Z",
"start_time": "2019-03-18T14:00:36.425760Z"
},
"collapsed": true
"end_time": "2019-05-29T13:57:37.394997Z",
"start_time": "2019-05-29T13:57:37.386720Z"
}
},
"outputs": [],
"source": [
...
...
@@ -89,8 +91,8 @@
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2019-0
3-18T14:00:36.469477
Z",
"start_time": "2019-0
3-18T14:00:36.463753
Z"
"end_time": "2019-0
5-29T13:57:37.450484
Z",
"start_time": "2019-0
5-29T13:57:37.397357
Z"
}
},
"outputs": [
...
...
@@ -135,10 +137,9 @@
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2019-03-18T14:00:36.592065Z",
"start_time": "2019-03-18T14:00:36.470522Z"
},
"collapsed": true
"end_time": "2019-05-29T13:57:38.432567Z",
"start_time": "2019-05-29T13:57:37.452521Z"
}
},
"outputs": [],
"source": [
...
...
@@ -151,10 +152,9 @@
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2019-03-18T14:00:36.625077Z",
"start_time": "2019-03-18T14:00:36.594327Z"
},
"collapsed": true
"end_time": "2019-05-29T13:57:38.442887Z",
"start_time": "2019-05-29T13:57:38.435111Z"
}
},
"outputs": [],
"source": [
...
...
@@ -183,10 +183,9 @@
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2019-03-18T14:00:36.656988Z",
"start_time": "2019-03-18T14:00:36.628239Z"
},
"collapsed": true
"end_time": "2019-05-29T13:57:38.453480Z",
"start_time": "2019-05-29T13:57:38.445655Z"
}
},
"outputs": [],
"source": [
...
...
@@ -195,22 +194,24 @@
" net = net.to(device)\n",
" print(\"training on \", device)\n",
" loss = torch.nn.CrossEntropyLoss()\n",
" batch_count = 0\n",
" for epoch in range(num_epochs):\n",
" train_l_sum, train_acc_sum, n, start = 0.0, 0.0, 0, time.time()\n",
" for X, y in train_iter:\n",
" X = X.to(device)\n",
" y = y.to(device)\n",
" y_hat = net(X)\n",
" l = loss(y_hat, y)
.sum()
\n",
" l = loss(y_hat, y)\n",
" optimizer.zero_grad()\n",
" l.backward()\n",
" optimizer.step()\n",
" train_l_sum += l.cpu().item()\n",
" train_acc_sum += (y_hat.argmax(dim=1) == y).sum().cpu().item()\n",
" n += y.shape[0]\n",
" batch_count += 1\n",
" test_acc = evaluate_accuracy(test_iter, net)\n",
" print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f, time %.1f sec'\n",
" % (epoch + 1, train_l_sum /
n
, train_acc_sum / n, test_acc, time.time() - start))"
" % (epoch + 1, train_l_sum /
batch_count
, train_acc_sum / n, test_acc, time.time() - start))"
]
},
{
...
...
@@ -218,8 +219,8 @@
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2019-0
3-18T14:00:48.824449
Z",
"start_time": "2019-0
3-18T14:00:36.658020
Z"
"end_time": "2019-0
5-29T13:58:00.333237
Z",
"start_time": "2019-0
5-29T13:57:38.456012
Z"
}
},
"outputs": [
...
...
@@ -228,11 +229,11 @@
"output_type": "stream",
"text": [
"training on cuda\n",
"epoch 1, loss
0.0072, train acc 0.322, test acc 0.584, time 3.7
sec\n",
"epoch 2, loss 0.
0037, train acc 0.649, test acc 0.699, time 1.8
sec\n",
"epoch 3, loss 0.
0030, train acc 0.718, test acc 0.724, time 1.7
sec\n",
"epoch 4, loss 0.
0027, train acc 0.741, test acc 0.746, time 1.6
sec\n",
"epoch 5, loss 0.
0024, train acc 0.759, test acc 0.759, time 1.7
sec\n"
"epoch 1, loss
1.7885, train acc 0.337, test acc 0.584, time 2.4
sec\n",
"epoch 2, loss 0.
4793, train acc 0.614, test acc 0.666, time 2.3
sec\n",
"epoch 3, loss 0.
2637, train acc 0.704, test acc 0.720, time 2.3
sec\n",
"epoch 4, loss 0.
1747, train acc 0.734, test acc 0.740, time 2.2
sec\n",
"epoch 5, loss 0.
1282, train acc 0.751, test acc 0.749, time 2.2
sec\n"
]
}
],
...
...
@@ -245,9 +246,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": []
}
...
...
@@ -268,7 +267,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.
3
"
"version": "3.6.
8
"
},
"varInspector": {
"cols": {
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录