Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
734e87e5
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
734e87e5
编写于
12月 15, 2017
作者:
Y
yangyaming
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add python wrapper for lstm unit op.
上级
c13805e9
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
132 addition
and
8 deletion
+132
-8
doc/api/v2/fluid/layers.rst
doc/api/v2/fluid/layers.rst
+5
-6
python/paddle/v2/fluid/layers/nn.py
python/paddle/v2/fluid/layers/nn.py
+110
-2
python/paddle/v2/fluid/tests/test_layers.py
python/paddle/v2/fluid/tests/test_layers.py
+17
-0
未找到文件。
doc/api/v2/fluid/layers.rst
浏览文件 @
734e87e5
...
...
@@ -188,12 +188,6 @@ beam_search_decode
:noindex:
lstm
---------
.. autofunction:: paddle.v2.fluid.layers.lstm
:noindex:
lod_rank_table
---------
.. autofunction:: paddle.v2.fluid.layers.lod_rank_table
...
...
@@ -300,3 +294,8 @@ conv2d_transpose
.. autofunction:: paddle.v2.fluid.layers.conv2d_transpose
:noindex:
lstm_unit
---------
.. autofunction:: paddle.v2.fluid.layers.lstm_unit
:noindex:
python/paddle/v2/fluid/layers/nn.py
浏览文件 @
734e87e5
...
...
@@ -5,12 +5,13 @@ All layers just related to the neural network.
from
..layer_helper
import
LayerHelper
from
..initializer
import
Normal
,
Constant
from
..framework
import
Variable
from
tensor
import
concat
__all__
=
[
'fc'
,
'embedding'
,
'dynamic_lstm'
,
'gru_unit'
,
'linear_chain_crf'
,
'crf_decoding'
,
'cos_sim'
,
'cross_entropy'
,
'square_error_cost'
,
'accuracy'
,
'chunk_eval'
,
'sequence_conv'
,
'conv2d'
,
'sequence_pool'
,
'pool2d'
,
'batch_norm'
,
'beam_search_decode'
,
'conv2d_transpose'
'batch_norm'
,
'beam_search_decode'
,
'conv2d_transpose'
,
'lstm_unit'
]
...
...
@@ -392,7 +393,7 @@ def chunk_eval(input,
excluded_chunk_types
=
None
,
**
kwargs
):
"""
This function computes and outputs the precision, recall and
This function computes and outputs the precision, recall and
F1-score of chunk detection.
"""
helper
=
LayerHelper
(
"chunk_eval"
,
**
kwargs
)
...
...
@@ -789,3 +790,110 @@ def conv2d_transpose(input,
attrs
=
op_attr
)
return
out
def
lstm_unit
(
x_t
,
hidden_t_prev
,
cell_t_prev
,
forget_bias
=
0.0
,
main_program
=
None
,
startup_program
=
None
):
"""Lstm unit layer. The equation of a lstm step is:
.. math::
i_t & = \sigma(W_{x_i}x_{t} + W_{h_i}h_{t-1} + W_{c_i}c_{t-1} + b_i)
f_t & = \sigma(W_{x_f}x_{t} + W_{h_f}h_{t-1} + W_{c_f}c_{t-1} + b_f)
c_t & = f_tc_{t-1} + i_t tanh (W_{x_c}x_t+W_{h_c}h_{t-1} + b_c)
o_t & = \sigma(W_{x_o}x_{t} + W_{h_o}h_{t-1} + W_{c_o}c_t + b_o)
h_t & = o_t tanh(c_t)
The inputs of lstm unit includes :math:`x_t`, :math:`h_{t-1}` and
:math:`c_{t-1}`. The implementation separates the linear transformation
and non-linear transformation apart. Here, we take :math:`i_t` as an
example. The linear transformation is applied by calling a `fc` layer and
the equation is:
.. math::
L_{i_t} = W_{x_i}x_{t} + W_{h_i}h_{t-1} + W_{c_i}c_{t-1} + b_i
The non-linear transformation is applied by calling `lstm_unit_op` and the
equation is:
.. math::
i_t = \sigma(L_{i_t})
This layer has two outputs including :math:`o_t` and :math:`h_t`.
Args:
x_t (Variable): The input value of current step.
hidden_t_prev (Variable): The hidden value of lstm unit.
cell_t_prev (Variable): The cell value of lstm unit.
forget_bias (float): The forget bias of lstm unit.
main_program (Program): The main program.
startup_program (Program): the startup program.
Returns:
tuple: The cell value and hidden value of lstm unit.
Raises:
ValueError: The ranks of **x_t**, **hidden_t_prev** and **cell_t_prev**
\
not be 2 or the 1st dimensions of **x_t**, **hidden_t_prev**
\
and **cell_t_prev** not be the same.
Examples:
.. code-block:: python
x_t = fluid.layers.fc(input=x_t_data, size=10)
prev_hidden = fluid.layers.fc(input=prev_hidden_data, size=20)
prev_cell = fluid.layers.fc(input=prev_cell_data, size=30)
cell_value, hidden_value = fluid.layers.lstm_unit(x_t=x_t,
hidden_t_prev=prev_hidden,
cell_t_prev=prev_cell)
"""
helper
=
LayerHelper
(
'lstm_unit'
,
**
locals
())
if
len
(
x_t
.
shape
)
!=
2
:
raise
ValueError
(
"Rank of x_t must be 2."
)
if
len
(
hidden_t_prev
.
shape
)
!=
2
:
raise
ValueError
(
"Rank of hidden_t_prev must be 2."
)
if
len
(
cell_t_prev
.
shape
)
!=
2
:
raise
ValueError
(
"Rank of cell_t_prev must be 2."
)
if
x_t
.
shape
[
0
]
!=
hidden_t_prev
.
shape
[
0
]
or
x_t
.
shape
[
0
]
!=
cell_t_prev
.
shape
[
0
]:
raise
ValueError
(
"The 1s dimension of x_t, hidden_t_prev and "
"cell_t_prev must be the same."
)
size
=
cell_t_prev
.
shape
[
1
]
concat_out
=
concat
(
input
=
[
x_t
,
hidden_t_prev
],
axis
=
1
,
main_program
=
main_program
,
startup_program
=
startup_program
)
fc_out
=
fc
(
input
=
concat_out
,
size
=
4
*
size
,
main_program
=
main_program
,
startup_program
=
startup_program
)
dtype
=
x_t
.
dtype
c
=
helper
.
create_tmp_variable
(
dtype
)
h
=
helper
.
create_tmp_variable
(
dtype
)
helper
.
append_op
(
type
=
'lstm_unit'
,
inputs
=
{
"X"
:
fc_out
,
"C_prev"
:
cell_t_prev
},
outputs
=
{
"C"
:
c
,
"H"
:
h
},
attrs
=
{
"forget_bias"
:
forget_bias
})
return
c
,
h
python/paddle/v2/fluid/tests/test_layers.py
浏览文件 @
734e87e5
...
...
@@ -161,6 +161,23 @@ class TestBook(unittest.TestCase):
x
=
dat
,
label
=
lbl
))
print
(
str
(
program
))
def
test_lstm_unit
(
self
):
program
=
Program
()
with
program_guard
(
program
):
x_t_data
=
layers
.
data
(
name
=
'x_t_data'
,
shape
=
[
10
,
10
],
dtype
=
'float32'
)
x_t
=
layers
.
fc
(
input
=
x_t_data
,
size
=
10
)
prev_hidden_data
=
layers
.
data
(
name
=
'prev_hidden_data'
,
shape
=
[
10
,
20
],
dtype
=
'float32'
)
prev_hidden
=
layers
.
fc
(
input
=
prev_hidden_data
,
size
=
20
)
prev_cell_data
=
layers
.
data
(
name
=
'prev_cell'
,
shape
=
[
10
,
30
],
dtype
=
'float32'
)
prev_cell
=
layers
.
fc
(
input
=
prev_cell_data
,
size
=
30
)
self
.
assertIsNotNone
(
layers
.
lstm_unit
(
x_t
=
x_t
,
hidden_t_prev
=
prev_hidden
,
cell_t_prev
=
prev_cell
))
print
(
str
(
program
))
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录