Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
78404faa
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
78404faa
编写于
10月 13, 2019
作者:
Y
Youwei Song
提交者:
whs
10月 13, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cherry-pick #20512 (#20585)
上级
b0b0d628
变更
3
展开全部
隐藏空白更改
内联
并排
Showing
3 changed file
with
281 addition
and
254 deletion
+281
-254
paddle/fluid/API.spec
paddle/fluid/API.spec
+193
-193
python/paddle/fluid/dygraph/base.py
python/paddle/fluid/dygraph/base.py
+4
-3
python/paddle/fluid/dygraph/layers.py
python/paddle/fluid/dygraph/layers.py
+84
-58
未找到文件。
paddle/fluid/API.spec
浏览文件 @
78404faa
此差异已折叠。
点击以展开。
python/paddle/fluid/dygraph/base.py
浏览文件 @
78404faa
...
@@ -88,10 +88,11 @@ no_grad.__doc__ = _no_grad_.__doc__
...
@@ -88,10 +88,11 @@ no_grad.__doc__ = _no_grad_.__doc__
@
signature_safe_contextmanager
@
signature_safe_contextmanager
def
guard
(
place
=
None
):
def
guard
(
place
=
None
):
"""
"""
This context will create a dygraph context for dygraph to run
This context will create a dygraph context for dygraph to run
, using python ``with`` statement.
Args:
Parameters:
place(fluid.CPUPlace|fluid.CUDAPlace|None): Place to run
place(fluid.CPUPlace or fluid.CUDAPlace, optional): Place to execute dygraph.
If None, the running place will be determined according to the way of paddle compilation. Default: None
return:
return:
None
None
...
...
python/paddle/fluid/dygraph/layers.py
浏览文件 @
78404faa
...
@@ -30,14 +30,20 @@ __all__ = ['Layer']
...
@@ -30,14 +30,20 @@ __all__ = ['Layer']
class
Layer
(
core
.
Layer
):
class
Layer
(
core
.
Layer
):
"""
Layers composed of operators
.
"""
Dynamic graph Layer based on OOD, includes the parameters of the layer, the structure of the forward graph and so on
.
Arg
s:
Parameter
s:
name_scope: prefix name used by the layer to name parameters.
name_scope
(str)
: prefix name used by the layer to name parameters.
If prefix is "my_model/layer_1", parameter name in MyLayer
If prefix is "my_model/layer_1", parameter name in MyLayer
can be "my_model/layer_1/MyLayer/w_n", where w is the parameter
can be "my_model/layer_1/MyLayer/w_n", where w is the parameter
base name and n is an unique suffix auto-generated.
base name and n is an unique suffix auto-generated.
dtype: data type for the variables in the layer.
dtype(str or core.VarDesc.VarType, optional): data type of this parameter.
If set str, it can be "bool", "float16", "float32", "float64",
"int8", "int16", "int32", "int64", "uint8" or "uint16".
Default: ``core.VarDesc.VarType.FP32``
Returns:
None
"""
"""
def
__init__
(
self
,
name_scope
,
dtype
=
core
.
VarDesc
.
VarType
.
FP32
):
def
__init__
(
self
,
name_scope
,
dtype
=
core
.
VarDesc
.
VarType
.
FP32
):
...
@@ -58,11 +64,10 @@ class Layer(core.Layer):
...
@@ -58,11 +64,10 @@ class Layer(core.Layer):
framework
.
_dygraph_tracer
().
eval_mode
()
framework
.
_dygraph_tracer
().
eval_mode
()
def
full_name
(
self
):
def
full_name
(
self
):
"""Full name for this layer
s.
"""Full name for this layer
, composed by name_scope + "/" + MyLayer.__class__.__name__
Full name is composed by name_scope + "/" + MyLayer.__class__.__name__
Returns:
str: full name of this layer.
Returns full name of this name.
"""
"""
return
self
.
_full_name
return
self
.
_full_name
...
@@ -72,16 +77,21 @@ class Layer(core.Layer):
...
@@ -72,16 +77,21 @@ class Layer(core.Layer):
dtype
,
dtype
,
is_bias
=
False
,
is_bias
=
False
,
default_initializer
=
None
):
default_initializer
=
None
):
"""Create parameters for this layers.
"""Create parameters for this layer.
Parameters:
attr(ParamAttr): Parameter attribute of weight. Please refer to :ref:`api_fluid_ParamAttr`
shape(list): shape of the parameter
dtype(str or core.VarDesc.VarType): data type of this parameter.
If set str, it can be "bool", "float16", "float32", "float64",
"int8", "int16", "int32", "int64", "uint8" or "uint16".
is_bias(bool, optional): if this is a bias parameter. Default: False
default_initializer(Initializer, optional): the default initializer for this parameter.
If set None, default initializer will be set to :ref:`api_fluid_initializer_XavierInitializer` and :ref:`api_fluid_initializer_ConstantInitializer`
for non-bias and bias parameter, respectively. Default: None
Args:
Returns:
attr: [ParamAttr] should be the parameter attribute for this parameter
:ref:`api_guide_Variable_en` : created parameter.
shape: shape of the paramter
dtype: data type of this parameter
is_bias: if this is a bias parameter
default_initializer: set the default initializer for this parameter
Returns created parameter Variable.
"""
"""
if
isinstance
(
attr
,
ParamAttr
)
and
(
attr
.
name
is
not
None
):
if
isinstance
(
attr
,
ParamAttr
)
and
(
attr
.
name
is
not
None
):
attr
.
name
=
"."
.
join
([
self
.
_full_name
,
attr
.
name
])
attr
.
name
=
"."
.
join
([
self
.
_full_name
,
attr
.
name
])
...
@@ -96,15 +106,19 @@ class Layer(core.Layer):
...
@@ -96,15 +106,19 @@ class Layer(core.Layer):
persistable
=
None
,
persistable
=
None
,
dtype
=
None
,
dtype
=
None
,
type
=
core
.
VarDesc
.
VarType
.
LOD_TENSOR
):
type
=
core
.
VarDesc
.
VarType
.
LOD_TENSOR
):
"""Create Variable for this layer
s
.
"""Create Variable for this layer.
Args:
Parameters:
name: name of the variable
name(str, optional): name of the variable. Please refer to :ref:`api_guide_Name` . Default: None
persistable: if set this variable persistable
persistable(bool, optional): if set this variable persistable. Default: False
dtype: data type of data in the variable
dtype(str or core.VarDesc.VarType, optional): data type of this parameter.
type: type of the variable
If set str, it can be "bool", "float16", "float32", "float64",
"int8", "int16", "int32", "int64", "uint8" or "uint16".
If set None, it will be ``core.VarDesc.VarType.FP32``. Default: None
type(core.VarDesc.VarType, optional): type of the variable. No need to set this parameter. Default: ``core.VarDesc.VarType.LOD_TENSOR``
Returns created Variable.
Returns:
:ref:`api_guide_Variable_en` : created Variable.
"""
"""
if
name
is
not
None
:
if
name
is
not
None
:
var_name
=
"."
.
join
([
self
.
_full_name
,
name
])
var_name
=
"."
.
join
([
self
.
_full_name
,
name
])
...
@@ -116,13 +130,13 @@ class Layer(core.Layer):
...
@@ -116,13 +130,13 @@ class Layer(core.Layer):
name
=
var_name
,
persistable
=
persistable
,
dtype
=
dtype
,
type
=
type
)
name
=
var_name
,
persistable
=
persistable
,
dtype
=
dtype
,
type
=
type
)
def
parameters
(
self
,
include_sublayers
=
True
):
def
parameters
(
self
,
include_sublayers
=
True
):
"""Returns a list of
Parameters from current and
sub-layers.
"""Returns a list of
all Parameters from current layer and its
sub-layers.
Args:
Parameters:
include_sublayers: If true, also include the parameters from
include_sublayers(bool, optional): Whether include the parameters of sublayers. If True, also include the parameters from sublayers. Default: True
sublayers.
Returns a list of Parameters.
Returns:
list of :ref:`api_guide_Variable_en` : a list of Parameters.
"""
"""
ret
=
[
p
for
p
in
self
.
_parameters
.
values
()]
ret
=
[
p
for
p
in
self
.
_parameters
.
values
()]
if
include_sublayers
:
if
include_sublayers
:
...
@@ -134,10 +148,11 @@ class Layer(core.Layer):
...
@@ -134,10 +148,11 @@ class Layer(core.Layer):
def
sublayers
(
self
,
include_sublayers
=
True
):
def
sublayers
(
self
,
include_sublayers
=
True
):
"""Returns a list of sub layers.
"""Returns a list of sub layers.
Arg
s:
Parameter
s:
include_sublayers
: If true, also include the layers from sublayers.
include_sublayers
(bool, optional): Whether return the sublayers of sublayers. If True, also include the sublayers of sublayers. Default: True
Returns a list of sub layers.
Returns:
list of Layer : a list of sub layers.
"""
"""
ret
=
[
l
for
l
in
self
.
_sub_layers
.
values
()]
ret
=
[
l
for
l
in
self
.
_sub_layers
.
values
()]
if
include_sublayers
:
if
include_sublayers
:
...
@@ -165,6 +180,14 @@ class Layer(core.Layer):
...
@@ -165,6 +180,14 @@ class Layer(core.Layer):
return
outputs
return
outputs
def
forward
(
self
,
*
inputs
,
**
kwargs
):
def
forward
(
self
,
*
inputs
,
**
kwargs
):
"""
Defines the computation performed at every call.
Should be overridden by all subclasses.
Parameters:
*inputs(tuple): unpacked tuple arguments
**kwargs(dict): unpacked dict arguments
"""
raise
NotImplementedError
raise
NotImplementedError
def
backward
(
self
,
*
inputs
):
def
backward
(
self
,
*
inputs
):
...
@@ -173,13 +196,13 @@ class Layer(core.Layer):
...
@@ -173,13 +196,13 @@ class Layer(core.Layer):
def
add_sublayer
(
self
,
name
,
sublayer
):
def
add_sublayer
(
self
,
name
,
sublayer
):
"""Adds a sub Layer instance.
"""Adds a sub Layer instance.
Added sublayer can be access like self.name.
Added sublayer can be accessed by self.name
Arg
s:
Parameter
s:
name: name of this sublayer.
name
(str)
: name of this sublayer.
sublayer: an instance of Layer.
sublayer
(Layer)
: an instance of Layer.
Returns:
Returns:
the sublayer passed in.
Layer:
the sublayer passed in.
"""
"""
assert
isinstance
(
sublayer
,
core
.
Layer
)
assert
isinstance
(
sublayer
,
core
.
Layer
)
...
@@ -189,13 +212,13 @@ class Layer(core.Layer):
...
@@ -189,13 +212,13 @@ class Layer(core.Layer):
def
add_parameter
(
self
,
name
,
parameter
):
def
add_parameter
(
self
,
name
,
parameter
):
"""Adds a Parameter instance.
"""Adds a Parameter instance.
Added parameter can be access like self.name.
Added parameter can be accessed by self.name
Arg
s:
Parameter
s:
name: name of this sublayer.
name
(str)
: name of this sublayer.
parameter: an instance of Parameter.
parameter
(Parameter)
: an instance of Parameter.
Returns:
Returns:
the parameter passed in.
Parameter:
the parameter passed in.
"""
"""
assert
isinstance
(
parameter
,
framework
.
Parameter
)
assert
isinstance
(
parameter
,
framework
.
Parameter
)
...
@@ -255,17 +278,18 @@ class Layer(core.Layer):
...
@@ -255,17 +278,18 @@ class Layer(core.Layer):
def
state_dict
(
self
,
destination
=
None
,
include_sublayers
=
True
):
def
state_dict
(
self
,
destination
=
None
,
include_sublayers
=
True
):
'''
'''
Get all parameter
of current and
sub-layers. And set all the parameters into a dict
Get all parameter
s of current layer and its
sub-layers. And set all the parameters into a dict
Arg
s:
Parameter
s:
destination(dict
|optical) : If provide, all the parameter will set to this dict . Defaul is
None
destination(dict
, optional) : If provide, all the parameters will set to this dict . Default:
None
include_sublayers(bool
) : If true, also include the parameters from sublayers.
include_sublayers(bool
, optional) : If true, also include the parameters from sublayers. Default: True
Retruns:
Retruns:
state_dict(dict) :
dict contains all the parameters
dict: a
dict contains all the parameters
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle.fluid as fluid
import paddle.fluid as fluid
with fluid.dygraph.guard():
with fluid.dygraph.guard():
emb = fluid.dygraph.Embedding( "emb", [10, 10])
emb = fluid.dygraph.Embedding( "emb", [10, 10])
...
@@ -293,16 +317,17 @@ class Layer(core.Layer):
...
@@ -293,16 +317,17 @@ class Layer(core.Layer):
def
set_dict
(
self
,
stat_dict
,
include_sublayers
=
True
):
def
set_dict
(
self
,
stat_dict
,
include_sublayers
=
True
):
'''
'''
Set parameter
from stat_dict. All the parameter
will be reset by the tensor in the stat_dict
Set parameter
s from stat_dict. All the parameters
will be reset by the tensor in the stat_dict
Arg
s:
Parameter
s:
state_dict(dict) : Dict contains all the
Parameter
state_dict(dict) : Dict contains all the
parameters
include_sublayers(bool
) : If true, also include the parameters from sublayers.
include_sublayers(bool
, optional) : If true, also include the parameters from sublayers. Default: True
Returns:
Returns:
None
None
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle.fluid as fluid
import paddle.fluid as fluid
with fluid.dygraph.guard():
with fluid.dygraph.guard():
emb = fluid.dygraph.Embedding( "emb", [10, 10])
emb = fluid.dygraph.Embedding( "emb", [10, 10])
...
@@ -319,18 +344,19 @@ class Layer(core.Layer):
...
@@ -319,18 +344,19 @@ class Layer(core.Layer):
def
load_dict
(
self
,
stat_dict
,
include_sublayers
=
True
):
def
load_dict
(
self
,
stat_dict
,
include_sublayers
=
True
):
'''
'''
Set parameter
from stat_dict. All the parameter
will be reset by the tensor in the stat_dict
Set parameter
s from stat_dict. All the parameters
will be reset by the tensor in the stat_dict
This api will be Deprecated. Please use set_dict
This api will be Deprecated. Please use set_dict
Arg
s:
Parameter
s:
state_dict(dict) : Dict contains all the
Parameter
state_dict(dict) : Dict contains all the
parameters
include_sublayers(bool
) : If true, also include the parameters from sublayers.
include_sublayers(bool
, optional) : If true, also include the parameters from sublayers. Default: True
Returns:
Returns:
None
None
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle.fluid as fluid
import paddle.fluid as fluid
with fluid.dygraph.guard():
with fluid.dygraph.guard():
emb = fluid.dygraph.Embedding( "emb", [10, 10])
emb = fluid.dygraph.Embedding( "emb", [10, 10])
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录