Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
3dfbef29
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
提交
3dfbef29
编写于
1月 28, 2019
作者:
J
JiabinYang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
polish code and add comments for Embedding
上级
53d558cd
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
46 addition
and
10 deletion
+46
-10
python/paddle/fluid/imperative/nn.py
python/paddle/fluid/imperative/nn.py
+40
-5
python/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py
...n/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py
+6
-5
未找到文件。
python/paddle/fluid/imperative/nn.py
浏览文件 @
3dfbef29
...
...
@@ -22,7 +22,7 @@ from . import layers
from
..framework
import
Variable
,
OpProtoHolder
from
..param_attr
import
ParamAttr
from
..initializer
import
Normal
,
Constant
__all__
=
[
'Conv2D'
,
'Pool2D'
,
'FC'
,
'BatchNorm'
,
'E
MBEDDING
'
]
__all__
=
[
'Conv2D'
,
'Pool2D'
,
'FC'
,
'BatchNorm'
,
'E
mbedding
'
]
class
Conv2D
(
layers
.
Layer
):
...
...
@@ -415,7 +415,44 @@ class BatchNorm(layers.Layer):
return
self
.
_helper
.
append_activation
(
batch_norm_out
)
class
EMBEDDING
(
layers
.
Layer
):
class
Embedding
(
layers
.
Layer
):
"""
**Embedding Layer**
This layer is used to lookup embeddings of IDs, provided by :attr:`input`, in
a lookup table. The result of this lookup is the embedding of each ID in the
:attr:`input`.
All the input variables are passed in as local variables to the LayerHelper
constructor.
Args:
size(tuple|list): The shape of the look up table parameter. It should
have two elements which indicate the size of the dictionary of
embeddings and the size of each embedding vector respectively.
is_sparse(bool): The flag indicating whether to use sparse update.
is_distributed(bool): Whether to run lookup table from remote parameter server.
padding_idx(int|long|None): If :attr:`None`, it makes no effect to lookup.
Otherwise the given :attr:`padding_idx` indicates padding the output
with zeros whenever lookup encounters it in :attr:`input`. If
:math:`padding_idx < 0`, the :attr:`padding_idx` to use in lookup is
:math:`size[0] + dim`.
param_attr(ParamAttr): Parameters for this layer
dtype(np.dtype|core.VarDesc.VarType|str): The type of data : float32, float_16, int etc
Returns:
Variable: The tensor variable storing the embeddings of the
\
supplied inputs.
Examples:
.. code-block:: python
dict_size = len(dataset.ids)
input = fluid.layers.data(name='ids', shape=[32, 32], dtype='float32')
embedding = fluid.imperative.Embedding(size=[dict_size, 16])
fc = embedding(input)
"""
def
__init__
(
self
,
size
,
is_sparse
=
False
,
...
...
@@ -424,7 +461,7 @@ class EMBEDDING(layers.Layer):
param_attr
=
None
,
dtype
=
'float32'
):
super
(
E
MBEDDING
,
self
).
__init__
()
super
(
E
mbedding
,
self
).
__init__
()
self
.
_size
=
size
self
.
_is_sparse
=
is_sparse
self
.
_is_distributed
=
is_distributed
...
...
@@ -440,8 +477,6 @@ class EMBEDDING(layers.Layer):
from
..layer_helper
import
LayerHelper
self
.
_helper
=
LayerHelper
(
'embedding'
,
param_attr
=
param_attr
)
def
_build_once
(
self
,
input
):
self
.
_w
=
self
.
_helper
.
create_parameter
(
attr
=
self
.
_param_attr
,
shape
=
self
.
_size
,
...
...
python/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py
浏览文件 @
3dfbef29
...
...
@@ -16,7 +16,7 @@ from __future__ import print_function
import
unittest
import
paddle.fluid
as
fluid
from
paddle.fluid.imperative.nn
import
E
MBEDDING
from
paddle.fluid.imperative.nn
import
E
mbedding
import
paddle.fluid.framework
as
framework
from
paddle.fluid.optimizer
import
SGDOptimizer
from
paddle.fluid.imperative.base
import
to_variable
...
...
@@ -143,7 +143,7 @@ class PtbModel(fluid.imperative.Layer):
num_layers
=
num_layers
,
init_scale
=
init_scale
,
dropout
=
dropout
)
self
.
embedding
=
E
MBEDDING
(
self
.
embedding
=
E
mbedding
(
size
=
[
vocab_size
,
hidden_size
],
dtype
=
'float32'
,
is_sparse
=
False
,
...
...
@@ -151,8 +151,6 @@ class PtbModel(fluid.imperative.Layer):
name
=
'embedding_para'
,
initializer
=
fluid
.
initializer
.
UniformInitializer
(
low
=-
init_scale
,
high
=
init_scale
)))
def
_build_once
(
self
,
input
,
label
,
init_hidden
,
init_cell
):
self
.
softmax_weight
=
fluid
.
layers
.
create_parameter
(
[
self
.
hidden_size
,
self
.
vocab_size
],
dtype
=
"float32"
,
...
...
@@ -166,6 +164,9 @@ class PtbModel(fluid.imperative.Layer):
default_initializer
=
fluid
.
initializer
.
UniformInitializer
(
low
=-
self
.
init_scale
,
high
=
self
.
init_scale
))
def
_build_once
(
self
,
input
,
label
,
init_hidden
,
init_cell
):
pass
def
forward
(
self
,
input
,
label
,
init_hidden
,
init_cell
):
init_h
=
fluid
.
layers
.
reshape
(
...
...
@@ -203,7 +204,7 @@ class PtbModel(fluid.imperative.Layer):
class
TestImperativePtbRnn
(
unittest
.
TestCase
):
def
test_
mnist
_cpu_float32
(
self
):
def
test_
ptb_rnn
_cpu_float32
(
self
):
seed
=
90
hidden_size
=
10
vocab_size
=
1000
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录