提交 d99e947b 编写于 作者: S shusentang

fix bug

上级 adbcaf7f
......@@ -12,8 +12,8 @@
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2019-03-18T14:00:36.424190Z",
"start_time": "2019-03-18T14:00:35.632104Z"
"end_time": "2019-05-29T13:57:37.383972Z",
"start_time": "2019-05-29T13:57:34.520559Z"
}
},
"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-03-18T14:00:36.469477Z",
"start_time": "2019-03-18T14:00:36.463753Z"
"end_time": "2019-05-29T13:57:37.450484Z",
"start_time": "2019-05-29T13:57:37.397357Z"
}
},
"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-03-18T14:00:48.824449Z",
"start_time": "2019-03-18T14:00:36.658020Z"
"end_time": "2019-05-29T13:58:00.333237Z",
"start_time": "2019-05-29T13:57:38.456012Z"
}
},
"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.
先完成此消息的编辑!
想要评论请 注册