Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
nlp-pytorch-zh
提交
43d7d03c
N
nlp-pytorch-zh
项目概览
OpenDocCN
/
nlp-pytorch-zh
通知
17
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
nlp-pytorch-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
43d7d03c
编写于
7月 29, 2019
作者:
片刻小哥哥
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
更新部分文档
上级
aea3d604
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
55 addition
and
0 deletion
+55
-0
docs/chatbot.md
docs/chatbot.md
+55
-0
未找到文件。
docs/chatbot.md
浏览文件 @
43d7d03c
...
...
@@ -102,3 +102,58 @@ u0 +++$+++ u2 +++$+++ m0 +++$+++ ['L207', 'L208']
4.
打印输出 pairs
5.
voc.trim, 过滤掉词频数据(有利于让训练更快收敛的策略是去除词汇表中很少使用的单词。减少特征空间也会降低模型学习目标函数的难度)
```
['there .', 'where ?']
['you have my word . as a gentleman', 'you re sweet .']
['hi .', 'looks like things worked out tonight huh ?']
['you know chastity ?', 'i believe we share an art instructor']
['have fun tonight ?', 'tons']
['well no . . .', 'then that s all you had to say .']
['then that s all you had to say .', 'but']
['but', 'you always been this selfish ?']
['do you listen to this crap ?', 'what crap ?']
['what good stuff ?', 'the real you .']
```
### 为模型格式化数据
1.
加速训练,利用GPU并行计算能力,则需要使用小批量
`mini-batches`
2.
为了保证数据长短一致,设置
`(max_length,batch_size)`
, 短于 max_length 的句子在 EOS_token 之后进行零填充
`(zero padded)`
3.
矩阵转置(以便跨第一维的索引返回批处理中所有句子的时间步长)
![](
https://pytorch.apachecn.org/docs/1.0/img/b2f1969c698070d055c23fc81ab07b1b.jpg
)
## 定义模型
Seq2seq模型的目标是将可变长度序列作为输入,并使用固定大小的模型将可变长度序列作为输出返回。
*
Seq2Seq模型:
1.
编码器,其将可变长度输入序列编码为固定长度上下文向量。
2.
解码器,它接收输入文字和上下文矢量,并返回序列中下一句文字的概率和在下一次迭代中使用的隐藏状态。
![](
https://pytorch.apachecn.org/docs/1.0/img/32a87cf8d0353ceb0037776f833b92a7.jpg
)
*
编码器:
如果将填充的一批序列传递给RNN模块,我们必须分别使用torch.nn.utils.rnn.pack_padded_sequence和torch.nn.utils.rnn.pad_packed_sequence在RNN传递时分别进行填充和反填充。
```
py
def
forward
(
self
,
input_seq
,
input_lengths
,
hidden
=
None
):
# Convert word indexes to embeddings
embedded
=
self
.
embedding
(
input_seq
)
# Pack padded batch of sequences for RNN module
packed
=
torch
.
nn
.
utils
.
rnn
.
pack_padded_sequence
(
embedded
,
input_lengths
)
# Forward pass through GRU
outputs
,
hidden
=
self
.
gru
(
packed
,
hidden
)
# Unpack padding
outputs
,
_
=
torch
.
nn
.
utils
.
rnn
.
pad_packed_sequence
(
outputs
)
# Sum bidirectional GRU outputs
outputs
=
outputs
[:,
:,
:
self
.
hidden_size
]
+
outputs
[:,
:
,
self
.
hidden_size
:]
# Return output and final hidden state
return
outputs
,
hidden
```
![](
https://pytorch.apachecn.org/docs/1.0/img/c653271eb5fb762482bceb5e2464e680.jpg
)
*
解码器:
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录