Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
7b54b30b
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
7b54b30b
编写于
6月 12, 2018
作者:
Y
yi.wu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
follow comments
上级
efd1d200
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
30 addition
and
79 deletion
+30
-79
paddle/fluid/operators/linear_chain_crf_op.cc
paddle/fluid/operators/linear_chain_crf_op.cc
+2
-0
paddle/fluid/operators/lstm_op.cc
paddle/fluid/operators/lstm_op.cc
+14
-14
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+14
-65
未找到文件。
paddle/fluid/operators/linear_chain_crf_op.cc
浏览文件 @
7b54b30b
...
@@ -84,6 +84,7 @@ CRF. Please refer to http://www.cs.columbia.edu/~mcollins/fb.pdf and
...
@@ -84,6 +84,7 @@ CRF. Please refer to http://www.cs.columbia.edu/~mcollins/fb.pdf and
http://cseweb.ucsd.edu/~elkan/250Bwinter2012/loglinearCRFs.pdf for details.
http://cseweb.ucsd.edu/~elkan/250Bwinter2012/loglinearCRFs.pdf for details.
Equation:
Equation:
1. Denote Input(Emission) to this operator as $x$ here.
1. Denote Input(Emission) to this operator as $x$ here.
2. The first D values of Input(Transition) to this operator are for starting
2. The first D values of Input(Transition) to this operator are for starting
weights, denoted as $a$ here.
weights, denoted as $a$ here.
...
@@ -106,6 +107,7 @@ Finally, the linear chain CRF operator outputs the logarithm of the conditional
...
@@ -106,6 +107,7 @@ Finally, the linear chain CRF operator outputs the logarithm of the conditional
likelihood of each training sample in a mini-batch.
likelihood of each training sample in a mini-batch.
NOTE:
NOTE:
1. The feature function for a CRF is made up of the emission features and the
1. The feature function for a CRF is made up of the emission features and the
transition features. The emission feature weights are NOT computed in
transition features. The emission feature weights are NOT computed in
this operator. They MUST be computed first before this operator is called.
this operator. They MUST be computed first before this operator is called.
...
...
paddle/fluid/operators/lstm_op.cc
浏览文件 @
7b54b30b
...
@@ -198,20 +198,20 @@ c_t = f_t \odot c_{t-1} + i_t \odot \tilde{c_t} \\
...
@@ -198,20 +198,20 @@ c_t = f_t \odot c_{t-1} + i_t \odot \tilde{c_t} \\
h_t = o_t \odot act_h(c_t)
h_t = o_t \odot act_h(c_t)
$$
$$
where the
W terms denote weight matrices (e.g. $W_{xi}$ is the matrix
-
W terms denote weight matrices (e.g. $W_{xi}$ is the matrix
of weights from the input gate to the input), $W_{ic}, W_{fc}, W_{oc}$
of weights from the input gate to the input), $W_{ic}, W_{fc}, W_{oc}$
are diagonal weight matrices for peephole connections. In our implementation,
are diagonal weight matrices for peephole connections. In our implementation,
we use vectors to reprenset these diagonal weight matrices. The b terms
we use vectors to reprenset these diagonal weight matrices.
denote bias vectors ($b_i$ is the input gate bias vector), $\sigma$
- The b terms denote bias vectors ($b_i$ is the input gate bias vector).
is the non-line activations, such as logistic sigmoid function, and
- $\sigma$ is the non-line activations, such as logistic sigmoid function.
$i, f, o$ and $c$ are the input gate, forget gate, output gate,
-
$i, f, o$ and $c$ are the input gate, forget gate, output gate,
and cell activation vectors, respectively, all of which have the same size as
and cell activation vectors, respectively, all of which have the same size as
the cell output activation vector $h$.
the cell output activation vector $h$.
- The $\odot$ is the element-wise product of the vectors.
The $\odot$ is the element-wise product of the vectors. $act_g$ and $act_h$
- $act_g$ and $act_h$ are the cell input and cell output activation functions
are the cell input and cell output activation functions and `tanh` is usually
and `tanh` is usually used for them.
used for them.
$\tilde{c_t}$ is also called candidate hidden state,
-
$\tilde{c_t}$ is also called candidate hidden state,
which is computed based on the current input and the previous hidden state.
which is computed based on the current input and the previous hidden state.
Set `use_peepholes` False to disable peephole connection. The formula
Set `use_peepholes` False to disable peephole connection. The formula
is omitted here, please refer to the paper
is omitted here, please refer to the paper
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
7b54b30b
...
@@ -262,6 +262,7 @@ def embedding(input,
...
@@ -262,6 +262,7 @@ def embedding(input,
# TODO(qijun): expose H0 and C0
# TODO(qijun): expose H0 and C0
@
templatedoc
(
op_type
=
"lstm"
)
def
dynamic_lstm
(
input
,
def
dynamic_lstm
(
input
,
size
,
size
,
param_attr
=
None
,
param_attr
=
None
,
...
@@ -274,64 +275,19 @@ def dynamic_lstm(input,
...
@@ -274,64 +275,19 @@ def dynamic_lstm(input,
dtype
=
'float32'
,
dtype
=
'float32'
,
name
=
None
):
name
=
None
):
"""
"""
**Dynamic LSTM Layer**
${comment}
The defalut implementation is diagonal/peephole connection
(https://arxiv.org/pdf/1402.1128.pdf), the formula is as follows:
.. math::
i_t & = \sigma(W_{ix}x_{t} + W_{ih}h_{t-1} + W_{ic}c_{t-1} + b_i)
f_t & = \sigma(W_{fx}x_{t} + W_{fh}h_{t-1} + W_{fc}c_{t-1} + b_f)
\\
tilde{c_t} & = act_g(W_{cx}x_t + W_{ch}h_{t-1} + b_c)
o_t & = \sigma(W_{ox}x_{t} + W_{oh}h_{t-1} + W_{oc}c_t + b_o)
c_t & = f_t \odot c_{t-1} + i_t \odot
\\
tilde{c_t}
h_t & = o_t \odot act_h(c_t)
where the :math:`W` terms denote weight matrices (e.g. :math:`W_{xi}` is
the matrix of weights from the input gate to the input), :math:`W_{ic},
\
W_{fc}, W_{oc}` are diagonal weight matrices for peephole connections. In
our implementation, we use vectors to reprenset these diagonal weight
matrices. The :math:`b` terms denote bias vectors (:math:`b_i` is the input
gate bias vector), :math:`\sigma` is the non-linear activations, such as
logistic sigmoid function, and :math:`i, f, o` and :math:`c` are the input
gate, forget gate, output gate, and cell activation vectors, respectively,
all of which have the same size as the cell output activation vector :math:`h`.
The :math:`\odot` is the element-wise product of the vectors. :math:`act_g`
and :math:`act_h` are the cell input and cell output activation functions
and `tanh` is usually used for them. :math:`
\\
tilde{c_t}` is also called
candidate hidden state, which is computed based on the current input and
the previous hidden state.
Set `use_peepholes` to `False` to disable peephole connection. The formula
is omitted here, please refer to the paper
http://www.bioinf.jku.at/publications/older/2604.pdf for details.
Note that these :math:`W_{xi}x_{t}, W_{xf}x_{t}, W_{xc}x_{t}, W_{xo}x_{t}`
operations on the input :math:`x_{t}` are NOT included in this operator.
Users can choose to use fully-connect layer before LSTM layer.
Args:
Args:
input(Variable): The input of dynamic_lstm layer, which supports
input (Variable): ${input_comment}
variable-time length input sequence. The underlying
size (int): 4 * hidden size.
tensor in this Variable is a matrix with shape
param_attr (ParamAttr|None): The parameter attribute for the learnable
(T X 4D), where T is the total time steps in this
mini-batch, D is the hidden size.
size(int): 4 * hidden size.
param_attr(ParamAttr|None): The parameter attribute for the learnable
hidden-hidden weights.
hidden-hidden weights.
- Weights = {:math:`W_{ch}, W_{ih},
\
- Weights = {:math:`W_{ch}, W_{ih},
\
W_{fh}, W_{oh}`}
W_{fh}, W_{oh}`}
- The shape is (D x 4D), where D is the hidden
- The shape is (D x 4D), where D is the hidden
size.
size.
bias_attr(ParamAttr|None): The bias attribute for the learnable bias
bias_attr
(ParamAttr|None): The bias attribute for the learnable bias
weights, which contains two parts, input-hidden
weights, which contains two parts, input-hidden
bias weights and peephole connections weights if
bias weights and peephole connections weights if
setting `use_peepholes` to `True`.
setting `use_peepholes` to `True`.
...
@@ -343,20 +299,13 @@ def dynamic_lstm(input,
...
@@ -343,20 +299,13 @@ def dynamic_lstm(input,
- Biases = { :math:`b_c, b_i, b_f, b_o, W_{ic},
\
- Biases = { :math:`b_c, b_i, b_f, b_o, W_{ic},
\
W_{fc}, W_{oc}`}.
W_{fc}, W_{oc}`}.
- The shape is (1 x 7D).
- The shape is (1 x 7D).
use_peepholes(bool): Whether to enable diagonal/peephole connections,
use_peepholes (bool): ${use_peepholes_comment}
default `True`.
is_reverse (bool): ${is_reverse_comment}
is_reverse(bool): Whether to compute reversed LSTM, default `False`.
gate_activation (str): ${gate_activation_comment}
gate_activation(str): The activation for input gate, forget gate and
cell_activation (str): ${cell_activation_comment}
output gate. Choices = ["sigmoid", "tanh", "relu",
candidate_activation (str): ${candidate_activation_comment}
"identity"], default "sigmoid".
dtype (str): Data type. Choices = ["float32", "float64"], default "float32".
cell_activation(str): The activation for cell output. Choices = ["sigmoid",
name (str|None): A name for this layer(optional). If set None, the layer
"tanh", "relu", "identity"], default "tanh".
candidate_activation(str): The activation for candidate hidden state.
Choices = ["sigmoid", "tanh",
"relu", "identity"],
default "tanh".
dtype(str): Data type. Choices = ["float32", "float64"], default "float32".
name(str|None): A name for this layer(optional). If set None, the layer
will be named automatically.
will be named automatically.
Returns:
Returns:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录