Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
Dive-into-DL-PyTorch
提交
c9fd01bc
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 搜索 >>
提交
c9fd01bc
编写于
1月 11, 2020
作者:
S
ShusenTang
浏览文件
操作
浏览文件
下载
差异文件
pull from remote
上级
73cddebb
2328c646
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
40 addition
and
28 deletion
+40
-28
code/chapter10_natural-language-processing/10.7_sentiment-analysis-rnn.ipynb
...ral-language-processing/10.7_sentiment-analysis-rnn.ipynb
+31
-24
code/d2lzh_pytorch/utils.py
code/d2lzh_pytorch/utils.py
+2
-2
docs/chapter05_CNN/5.8_nin.md
docs/chapter05_CNN/5.8_nin.md
+1
-0
docs/chapter10_natural-language-processing/10.7_sentiment-analysis-rnn.md
...atural-language-processing/10.7_sentiment-analysis-rnn.md
+6
-2
未找到文件。
code/chapter10_natural-language-processing/10.7_sentiment-analysis-rnn.ipynb
浏览文件 @
c9fd01bc
...
...
@@ -21,7 +21,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"1.
0
.0 cuda\n"
"1.
1
.0 cuda\n"
]
}
],
...
...
@@ -39,10 +39,10 @@
"sys.path.append(\"..\") \n",
"import d2lzh_pytorch as d2l\n",
"\n",
"os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"
7
\"\n",
"os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"
2
\"\n",
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
"\n",
"DATA_ROOT = \"/
S1/CSCL
/tangss/Datasets\"\n",
"DATA_ROOT = \"/
data1
/tangss/Datasets\"\n",
"\n",
"print(torch.__version__, device)"
]
...
...
@@ -88,10 +88,10 @@
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 12500/12500 [00:0
4<00:00, 2930.03
it/s]\n",
"100%|██████████| 12500/12500 [00:0
4<00:00, 3008
.48it/s]\n",
"100%|██████████| 12500/12500 [00:0
3<00:00, 3365.08
it/s]\n",
"100%|██████████| 12500/12500 [00:0
3<00:00, 3305.63
it/s]\n"
"100%|██████████| 12500/12500 [00:0
0<00:00, 34211.42
it/s]\n",
"100%|██████████| 12500/12500 [00:0
0<00:00, 38506
.48it/s]\n",
"100%|██████████| 12500/12500 [00:0
0<00:00, 31316.61
it/s]\n",
"100%|██████████| 12500/12500 [00:0
0<00:00, 29664.72
it/s]\n"
]
}
],
...
...
@@ -108,7 +108,8 @@
" random.shuffle(data)\n",
" return data\n",
"\n",
"train_data, test_data = read_imdb('train'), read_imdb('test')"
"data_root = os.path.join(DATA_ROOT, \"aclImdb\")\n",
"train_data, test_data = read_imdb('train', data_root), read_imdb('test', data_root)"
]
},
{
...
...
@@ -152,7 +153,7 @@
{
"data": {
"text/plain": [
"('# words in vocab:', 4615
1
)"
"('# words in vocab:', 4615
2
)"
]
},
"execution_count": 5,
...
...
@@ -330,8 +331,7 @@
"ExecuteTime": {
"end_time": "2019-07-03T04:26:47.895604Z",
"start_time": "2019-07-03T04:26:47.685801Z"
},
"collapsed": true
}
},
"outputs": [],
"source": [
...
...
@@ -345,10 +345,17 @@
"ExecuteTime": {
"end_time": "2019-07-03T04:26:48.102388Z",
"start_time": "2019-07-03T04:26:47.897582Z"
},
"collapsed": true
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"There are 21202 oov words.\n"
]
}
],
"source": [
"def load_pretrained_embedding(words, pretrained_vocab):\n",
" \"\"\"从预训练好的vocab中提取出words对应的词向量\"\"\"\n",
...
...
@@ -359,9 +366,9 @@
" idx = pretrained_vocab.stoi[word]\n",
" embed[i, :] = pretrained_vocab.vectors[idx]\n",
" except KeyError:\n",
" oov_count +=
0
\n",
" oov_count +=
1
\n",
" if oov_count > 0:\n",
" print(\"There are %d oov words.\")\n",
" print(\"There are %d oov words.\"
% oov_count
)\n",
" return embed\n",
"\n",
"net.embedding.weight.data.copy_(load_pretrained_embedding(vocab.itos, glove_vocab))\n",
...
...
@@ -390,11 +397,11 @@
"output_type": "stream",
"text": [
"training on cuda\n",
"epoch 1, loss 0.5
759, train acc 0.666, test acc 0.832, time 250.8
sec\n",
"epoch 2, loss 0.1
785, train acc 0.842, test acc 0.852, time 253.3
sec\n",
"epoch 3, loss 0.1
042, train acc 0.866, test acc 0.856, time 253.7
sec\n",
"epoch 4, loss 0.0
682, train acc 0.888, test acc 0.868, time 254.2
sec\n",
"epoch 5, loss 0.0
483, train acc 0.901, test acc 0.862, time 251.4
sec\n"
"epoch 1, loss 0.5
415, train acc 0.719, test acc 0.819, time 48.7
sec\n",
"epoch 2, loss 0.1
897, train acc 0.837, test acc 0.852, time 53.0
sec\n",
"epoch 3, loss 0.1
105, train acc 0.857, test acc 0.844, time 51.6
sec\n",
"epoch 4, loss 0.0
719, train acc 0.881, test acc 0.865, time 52.1
sec\n",
"epoch 5, loss 0.0
519, train acc 0.894, test acc 0.852, time 51.2
sec\n"
]
}
],
...
...
@@ -488,9 +495,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:py36]",
"display_name": "Python [conda env:py36
_pytorch
]",
"language": "python",
"name": "conda-env-py36-py"
"name": "conda-env-py36
_pytorch
-py"
},
"language_info": {
"codemirror_mode": {
...
...
@@ -502,7 +509,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.
3
"
"version": "3.6.
2
"
},
"varInspector": {
"cols": {
...
...
code/d2lzh_pytorch/utils.py
浏览文件 @
c9fd01bc
...
...
@@ -1203,9 +1203,9 @@ def load_pretrained_embedding(words, pretrained_vocab):
idx
=
pretrained_vocab
.
stoi
[
word
]
embed
[
i
,
:]
=
pretrained_vocab
.
vectors
[
idx
]
except
KeyError
:
oov_count
+=
0
oov_count
+=
1
if
oov_count
>
0
:
print
(
"There are %d oov words."
)
print
(
"There are %d oov words."
%
oov_count
)
return
embed
def
predict_sentiment
(
net
,
vocab
,
sentence
):
...
...
docs/chapter05_CNN/5.8_nin.md
浏览文件 @
c9fd01bc
...
...
@@ -43,6 +43,7 @@ NiN是在AlexNet问世不久后提出的。它们的卷积层设定有类似之
```
python
# 已保存在d2lzh_pytorch
import
torch.nn.functional
as
F
class
GlobalAvgPool2d
(
nn
.
Module
):
# 全局平均池化层可通过将池化窗口形状设置成输入的高和宽实现
def
__init__
(
self
):
...
...
docs/chapter10_natural-language-processing/10.7_sentiment-analysis-rnn.md
浏览文件 @
c9fd01bc
...
...
@@ -195,15 +195,19 @@ def load_pretrained_embedding(words, pretrained_vocab):
idx
=
pretrained_vocab
.
stoi
[
word
]
embed
[
i
,
:]
=
pretrained_vocab
.
vectors
[
idx
]
except
KeyError
:
oov_count
+=
0
oov_count
+=
1
if
oov_count
>
0
:
print
(
"There are %d oov words."
)
print
(
"There are %d oov words."
%
oov_count
)
return
embed
net
.
embedding
.
weight
.
data
.
copy_
(
load_pretrained_embedding
(
vocab
.
itos
,
glove_vocab
))
net
.
embedding
.
weight
.
requires_grad
=
False
# 直接加载预训练好的, 所以不需要更新它
```
输出:
```
There are 21202 oov words.
```
### 10.7.2.2 训练并评价模型
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录