Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
61eafbe0
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看板
未验证
提交
61eafbe0
编写于
10月 31, 2017
作者:
A
Abhinav Arora
提交者:
GitHub
10月 31, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Adding a framework for variable initializers (#5232)
上级
9b70b6a1
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
128 addition
and
55 deletion
+128
-55
python/paddle/v2/framework/framework.py
python/paddle/v2/framework/framework.py
+4
-15
python/paddle/v2/framework/initializer.py
python/paddle/v2/framework/initializer.py
+109
-0
python/paddle/v2/framework/layer_helper.py
python/paddle/v2/framework/layer_helper.py
+4
-15
python/paddle/v2/framework/layers.py
python/paddle/v2/framework/layers.py
+7
-19
python/paddle/v2/framework/tests/test_recognize_digits_mlp.py
...on/paddle/v2/framework/tests/test_recognize_digits_mlp.py
+4
-6
未找到文件。
python/paddle/v2/framework/framework.py
浏览文件 @
61eafbe0
...
...
@@ -354,8 +354,8 @@ class Block(object):
def
create_var
(
self
,
*
args
,
**
kwargs
):
var
=
Variable
(
self
,
*
args
,
**
kwargs
)
if
'init
_att
r'
in
kwargs
:
self
.
_prepend_initialize_ops_
(
var
,
kwargs
[
'init_attr'
]
)
if
'init
ialize
r'
in
kwargs
:
kwargs
[
'initializer'
](
var
,
self
)
return
var
def
has_var
(
self
,
name
):
...
...
@@ -364,8 +364,8 @@ class Block(object):
def
create_parameter
(
self
,
*
args
,
**
kwargs
):
global_block
=
self
.
program
.
global_block
()
param
=
Parameter
(
global_block
,
*
args
,
**
kwargs
)
if
'init
_att
r'
in
kwargs
:
self
.
_prepend_initialize_ops_
(
param
,
kwargs
[
'init_attr'
]
)
if
'init
ialize
r'
in
kwargs
:
kwargs
[
'initializer'
](
param
,
self
)
return
param
def
append_op
(
self
,
*
args
,
**
kwargs
):
...
...
@@ -424,17 +424,6 @@ class Block(object):
for
index
in
range
(
len
(
self
.
ops
)):
assert
self
.
ops
[
index
].
desc
==
ops_in_cpp
[
index
]
def
_prepend_initialize_ops_
(
self
,
param
,
init_attr
):
op_type
=
init_attr
[
'type'
]
init_attr
[
'shape'
]
=
param
.
shape
init_attr
[
'data_type'
]
=
int
(
param
.
data_type
)
op
=
self
.
prepend_op
(
type
=
op_type
,
inputs
=
None
,
outputs
=
{
'Out'
:
[
param
]},
attrs
=
init_attr
)
param
.
op
=
op
class
Program
(
object
):
def
__init__
(
self
):
...
...
python/paddle/v2/framework/initializer.py
0 → 100644
浏览文件 @
61eafbe0
import
paddle.v2.framework.framework
as
framework
__all__
=
[
'ConstantInitializer'
,
'UniformInitializer'
]
class
Initializer
(
object
):
"""Base class for variable initializers
Defines the common interface of variable initializers.
They add operations to the init program that are used
to initialize variables. Users should not use this class
directly, but need to use one of its implementations.
"""
def
__init_
(
self
):
pass
def
__call__
(
self
,
param
,
block
):
"""Add corresponding initialization operations to the network
"""
raise
NotImplementedError
()
class
ConstantInitializer
(
Initializer
):
"""Implements the constant initializer
"""
def
__init__
(
self
,
value
=
0.0
):
"""Constructor for ConstantInitializer
Args:
value: constant value to initialize the variable
"""
assert
value
is
not
None
super
(
ConstantInitializer
,
self
).
__init__
()
self
.
_value
=
value
def
__call__
(
self
,
var
,
block
):
"""Add constant initialization ops for a variable
Args:
var: Variable that needs to be initialized
block: The block in which initialization ops
should be added
Returns:
the initialization op
"""
assert
isinstance
(
var
,
framework
.
Variable
)
assert
isinstance
(
block
,
framework
.
Block
)
# Initialization Ops should be prepended and not appended
op
=
block
.
prepend_op
(
type
=
"fill_constant"
,
outputs
=
{
"Out"
:
var
},
attrs
=
{
"shape"
:
var
.
shape
,
"data_type"
:
int
(
var
.
data_type
),
"value"
:
self
.
_value
})
var
.
op
=
op
return
op
class
UniformInitializer
(
Initializer
):
"""Implements for random uniform distribution initializer
"""
def
__init__
(
self
,
low
=-
1.0
,
high
=
1.0
,
seed
=
0
):
"""Constructor for UniformInitializer
Args:
low: lower boundary of the uniform distribution
high: upper boundary of the uniform distribution
seed: random seed
"""
assert
low
is
not
None
assert
high
is
not
None
assert
seed
is
not
None
super
(
UniformInitializer
,
self
).
__init__
()
self
.
_low
=
low
self
.
_high
=
high
self
.
_seed
=
seed
def
__call__
(
self
,
var
,
block
):
"""Add uniform distribution initialization ops for a variable
Args:
var: Variable that needs to be initialized
block: The block in which initialization ops
should be added
Returns:
the initialization op
"""
assert
isinstance
(
var
,
framework
.
Variable
)
assert
isinstance
(
block
,
framework
.
Block
)
# Initialization Ops should be prepended and not appended
op
=
block
.
prepend_op
(
type
=
"uniform_random"
,
outputs
=
{
"Out"
:
var
},
attrs
=
{
"shape"
:
var
.
shape
,
"data_type"
:
int
(
var
.
data_type
),
"min"
:
self
.
_low
,
"max"
:
self
.
_high
,
"seed"
:
self
.
_seed
})
var
.
op
=
op
return
op
python/paddle/v2/framework/layer_helper.py
浏览文件 @
61eafbe0
...
...
@@ -5,6 +5,8 @@ import paddle.v2.framework.core as core
from
paddle.v2.framework.framework
import
Variable
,
g_program
,
\
g_init_program
from
paddle.v2.framework.initializer
import
ConstantInitializer
,
\
UniformInitializer
def
unique_name
(
prefix
):
...
...
@@ -66,14 +68,7 @@ class LayerHelper(object):
@
property
def
param_attr
(
self
):
default
=
{
'name'
:
None
,
'init_attr'
:
{
'type'
:
'uniform_random'
,
'min'
:
-
1.0
,
'max'
:
1.0
}
}
default
=
{
'name'
:
None
,
'initializer'
:
UniformInitializer
()}
actual
=
self
.
kwargs
.
get
(
'param_attr'
,
None
)
if
actual
is
None
:
actual
=
default
...
...
@@ -83,13 +78,7 @@ class LayerHelper(object):
return
actual
def
bias_attr
(
self
):
default
=
{
'name'
:
None
,
'init_attr'
:
{
'type'
:
'fill_constant'
,
'value'
:
0.0
}
}
default
=
{
'name'
:
None
,
'initializer'
:
ConstantInitializer
()}
bias_attr
=
self
.
kwargs
.
get
(
'bias_attr'
,
None
)
if
bias_attr
is
True
:
bias_attr
=
default
...
...
python/paddle/v2/framework/layers.py
浏览文件 @
61eafbe0
from
paddle.v2.framework.layer_helper
import
LayerHelper
,
unique_name
import
paddle.v2.framework.core
as
core
from
paddle.v2.framework.framework
import
OpProtoHolder
,
Variable
,
Program
from
paddle.v2.framework.initializer
import
ConstantInitializer
import
re
__all__
=
[
...
...
@@ -440,26 +441,12 @@ def batch_norm(input,
else
:
raise
ValueError
(
"unsupported data layout:"
+
data_layout
)
def
get_init_attr
(
value
):
if
not
isinstance
(
value
,
float
):
raise
ValueError
(
"attr value should be a float"
)
return
{
'type'
:
'fill_constant'
,
'value'
:
value
}
def
prepend_init_op
(
var
,
init_attr
):
assert
isinstance
(
var
,
Variable
)
op_type
=
init_attr
[
'type'
]
init_attr
[
'shape'
]
=
var
.
shape
init_attr
[
'data_type'
]
=
int
(
var
.
data_type
)
op
=
var
.
block
.
prepend_op
(
type
=
op_type
,
inputs
=
None
,
outputs
=
{
'Out'
:
[
var
]},
attrs
=
init_attr
)
return
op
def
create_persistable_var
(
dtype
,
shape
,
init_attr
=
None
):
def
create_persistable_var
(
dtype
,
shape
,
initializer
=
None
):
name
=
unique_name
(
"."
.
join
([
helper
.
name
,
"xxxx"
]))
var
=
init_program
.
global_block
().
create_var
(
dtype
=
dtype
,
shape
=
shape
,
name
=
name
,
persistable
=
True
)
if
'init_attr'
is
not
None
:
prepend_init_op
(
var
,
init_attr
)
if
initializer
is
not
None
:
initializer
(
var
,
var
.
block
)
return
program
.
global_block
().
create_var
(
name
=
name
,
dtype
=
dtype
,
shape
=
shape
,
persistable
=
True
)
...
...
@@ -472,8 +459,9 @@ def batch_norm(input,
attr
=
helper
.
param_attr
,
shape
=
param_shape
,
dtype
=
dtype
)
# create input
mean
=
create_persistable_var
(
dtype
,
param_shape
,
get_init_attr
(
0.0
))
variance
=
create_persistable_var
(
dtype
,
param_shape
,
get_init_attr
(
1.0
))
mean
=
create_persistable_var
(
dtype
,
param_shape
,
ConstantInitializer
(
0.0
))
variance
=
create_persistable_var
(
dtype
,
param_shape
,
ConstantInitializer
(
1.0
))
# create output
# mean and mean_out share the same memory
...
...
python/paddle/v2/framework/tests/test_recognize_digits_mlp.py
浏览文件 @
61eafbe0
...
...
@@ -3,9 +3,10 @@ import paddle.v2.framework.layers as layers
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.optimizer
as
optimizer
from
paddle.v2.framework.framework
import
Program
,
g_program
from
paddle.v2.framework.framework
import
Program
from
paddle.v2.framework.executor
import
Executor
from
paddle.v2.framework.regularizer
import
L2DecayRegularizer
from
paddle.v2.framework.initializer
import
UniformInitializer
import
numpy
as
np
...
...
@@ -21,11 +22,8 @@ image = layers.data(
param_attr
=
{
'name'
:
None
,
'init_attr'
:
{
'type'
:
'uniform_random'
,
'min'
:
-
1.0
,
'max'
:
1.0
},
'initializer'
:
UniformInitializer
(
low
=-
1.0
,
high
=
1.0
),
'regularization'
:
L2DecayRegularizer
(
0.0005
*
BATCH_SIZE
)
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录