Skip to content

Commit c9fd01b

Browse files
committed
pull from remote
2 parents 73cddeb + 2328c64 commit c9fd01b

File tree

4 files changed

+40
-28
lines changed

4 files changed

+40
-28
lines changed

code/chapter10_natural-language-processing/10.7_sentiment-analysis-rnn.ipynb

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"name": "stdout",
2222
"output_type": "stream",
2323
"text": [
24-
"1.0.0 cuda\n"
24+
"1.1.0 cuda\n"
2525
]
2626
}
2727
],
@@ -39,10 +39,10 @@
3939
"sys.path.append(\"..\") \n",
4040
"import d2lzh_pytorch as d2l\n",
4141
"\n",
42-
"os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"7\"\n",
42+
"os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"2\"\n",
4343
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
4444
"\n",
45-
"DATA_ROOT = \"/S1/CSCL/tangss/Datasets\"\n",
45+
"DATA_ROOT = \"/data1/tangss/Datasets\"\n",
4646
"\n",
4747
"print(torch.__version__, device)"
4848
]
@@ -88,10 +88,10 @@
8888
"name": "stderr",
8989
"output_type": "stream",
9090
"text": [
91-
"100%|██████████| 12500/12500 [00:04<00:00, 2930.03it/s]\n",
92-
"100%|██████████| 12500/12500 [00:04<00:00, 3008.48it/s]\n",
93-
"100%|██████████| 12500/12500 [00:03<00:00, 3365.08it/s]\n",
94-
"100%|██████████| 12500/12500 [00:03<00:00, 3305.63it/s]\n"
91+
"100%|██████████| 12500/12500 [00:00<00:00, 34211.42it/s]\n",
92+
"100%|██████████| 12500/12500 [00:00<00:00, 38506.48it/s]\n",
93+
"100%|██████████| 12500/12500 [00:00<00:00, 31316.61it/s]\n",
94+
"100%|██████████| 12500/12500 [00:00<00:00, 29664.72it/s]\n"
9595
]
9696
}
9797
],
@@ -108,7 +108,8 @@
108108
" random.shuffle(data)\n",
109109
" return data\n",
110110
"\n",
111-
"train_data, test_data = read_imdb('train'), read_imdb('test')"
111+
"data_root = os.path.join(DATA_ROOT, \"aclImdb\")\n",
112+
"train_data, test_data = read_imdb('train', data_root), read_imdb('test', data_root)"
112113
]
113114
},
114115
{
@@ -152,7 +153,7 @@
152153
{
153154
"data": {
154155
"text/plain": [
155-
"('# words in vocab:', 46151)"
156+
"('# words in vocab:', 46152)"
156157
]
157158
},
158159
"execution_count": 5,
@@ -330,8 +331,7 @@
330331
"ExecuteTime": {
331332
"end_time": "2019-07-03T04:26:47.895604Z",
332333
"start_time": "2019-07-03T04:26:47.685801Z"
333-
},
334-
"collapsed": true
334+
}
335335
},
336336
"outputs": [],
337337
"source": [
@@ -345,10 +345,17 @@
345345
"ExecuteTime": {
346346
"end_time": "2019-07-03T04:26:48.102388Z",
347347
"start_time": "2019-07-03T04:26:47.897582Z"
348-
},
349-
"collapsed": true
348+
}
350349
},
351-
"outputs": [],
350+
"outputs": [
351+
{
352+
"name": "stdout",
353+
"output_type": "stream",
354+
"text": [
355+
"There are 21202 oov words.\n"
356+
]
357+
}
358+
],
352359
"source": [
353360
"def load_pretrained_embedding(words, pretrained_vocab):\n",
354361
" \"\"\"从预训练好的vocab中提取出words对应的词向量\"\"\"\n",
@@ -359,9 +366,9 @@
359366
" idx = pretrained_vocab.stoi[word]\n",
360367
" embed[i, :] = pretrained_vocab.vectors[idx]\n",
361368
" except KeyError:\n",
362-
" oov_count += 0\n",
369+
" oov_count += 1\n",
363370
" if oov_count > 0:\n",
364-
" print(\"There are %d oov words.\")\n",
371+
" print(\"There are %d oov words.\" % oov_count)\n",
365372
" return embed\n",
366373
"\n",
367374
"net.embedding.weight.data.copy_(load_pretrained_embedding(vocab.itos, glove_vocab))\n",
@@ -390,11 +397,11 @@
390397
"output_type": "stream",
391398
"text": [
392399
"training on cuda\n",
393-
"epoch 1, loss 0.5759, train acc 0.666, test acc 0.832, time 250.8 sec\n",
394-
"epoch 2, loss 0.1785, train acc 0.842, test acc 0.852, time 253.3 sec\n",
395-
"epoch 3, loss 0.1042, train acc 0.866, test acc 0.856, time 253.7 sec\n",
396-
"epoch 4, loss 0.0682, train acc 0.888, test acc 0.868, time 254.2 sec\n",
397-
"epoch 5, loss 0.0483, train acc 0.901, test acc 0.862, time 251.4 sec\n"
400+
"epoch 1, loss 0.5415, train acc 0.719, test acc 0.819, time 48.7 sec\n",
401+
"epoch 2, loss 0.1897, train acc 0.837, test acc 0.852, time 53.0 sec\n",
402+
"epoch 3, loss 0.1105, train acc 0.857, test acc 0.844, time 51.6 sec\n",
403+
"epoch 4, loss 0.0719, train acc 0.881, test acc 0.865, time 52.1 sec\n",
404+
"epoch 5, loss 0.0519, train acc 0.894, test acc 0.852, time 51.2 sec\n"
398405
]
399406
}
400407
],
@@ -488,9 +495,9 @@
488495
],
489496
"metadata": {
490497
"kernelspec": {
491-
"display_name": "Python [conda env:py36]",
498+
"display_name": "Python [conda env:py36_pytorch]",
492499
"language": "python",
493-
"name": "conda-env-py36-py"
500+
"name": "conda-env-py36_pytorch-py"
494501
},
495502
"language_info": {
496503
"codemirror_mode": {
@@ -502,7 +509,7 @@
502509
"name": "python",
503510
"nbconvert_exporter": "python",
504511
"pygments_lexer": "ipython3",
505-
"version": "3.6.3"
512+
"version": "3.6.2"
506513
},
507514
"varInspector": {
508515
"cols": {

code/d2lzh_pytorch/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,9 +1203,9 @@ def load_pretrained_embedding(words, pretrained_vocab):
12031203
idx = pretrained_vocab.stoi[word]
12041204
embed[i, :] = pretrained_vocab.vectors[idx]
12051205
except KeyError:
1206-
oov_count += 0
1206+
oov_count += 1
12071207
if oov_count > 0:
1208-
print("There are %d oov words.")
1208+
print("There are %d oov words." % oov_count)
12091209
return embed
12101210

12111211
def predict_sentiment(net, vocab, sentence):

docs/chapter05_CNN/5.8_nin.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ NiN是在AlexNet问世不久后提出的。它们的卷积层设定有类似之
4343

4444
``` python
4545
# 已保存在d2lzh_pytorch
46+
import torch.nn.functional as F
4647
class GlobalAvgPool2d(nn.Module):
4748
# 全局平均池化层可通过将池化窗口形状设置成输入的高和宽实现
4849
def __init__(self):

docs/chapter10_natural-language-processing/10.7_sentiment-analysis-rnn.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,19 @@ def load_pretrained_embedding(words, pretrained_vocab):
195195
idx = pretrained_vocab.stoi[word]
196196
embed[i, :] = pretrained_vocab.vectors[idx]
197197
except KeyError:
198-
oov_count += 0
198+
oov_count += 1
199199
if oov_count > 0:
200-
print("There are %d oov words.")
200+
print("There are %d oov words." % oov_count)
201201
return embed
202202

203203
net.embedding.weight.data.copy_(
204204
load_pretrained_embedding(vocab.itos, glove_vocab))
205205
net.embedding.weight.requires_grad = False # 直接加载预训练好的, 所以不需要更新它
206206
```
207+
输出:
208+
```
209+
There are 21202 oov words.
210+
```
207211

208212
### 10.7.2.2 训练并评价模型
209213

0 commit comments

Comments
 (0)