Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
db3b9438
P
Paddle
项目概览
Crayon鑫
/
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看板
未验证
提交
db3b9438
编写于
10月 31, 2017
作者:
A
Abhinav Arora
提交者:
GitHub
10月 31, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Adding Normal distribution initializer and unit tests for python initializers (#5256)
上级
0b76c735
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
177 addition
and
8 deletion
+177
-8
paddle/operators/gaussian_random_op.cc
paddle/operators/gaussian_random_op.cc
+6
-6
python/paddle/v2/framework/initializer.py
python/paddle/v2/framework/initializer.py
+50
-1
python/paddle/v2/framework/tests/test_gaussian_random_op.py
python/paddle/v2/framework/tests/test_gaussian_random_op.py
+1
-1
python/paddle/v2/framework/tests/test_initializer.py
python/paddle/v2/framework/tests/test_initializer.py
+120
-0
未找到文件。
paddle/operators/gaussian_random_op.cc
浏览文件 @
db3b9438
...
...
@@ -45,14 +45,14 @@ class GaussianRandomOp : public framework::OperatorWithKernel {
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) of GaussianRandomOp should not be null."
);
auto
dims
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"dims
"
);
auto
shape
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"shape
"
);
std
::
vector
<
int64_t
>
temp
;
temp
.
reserve
(
dims
.
size
());
for
(
auto
dim
:
dims
)
{
temp
.
reserve
(
shape
.
size
());
for
(
auto
dim
:
shape
)
{
temp
.
push_back
(
static_cast
<
int64_t
>
(
dim
));
}
PADDLE_ENFORCE
(
dims
.
size
()
>
0UL
,
"
dims can be one int or array. dims
must be set."
);
PADDLE_ENFORCE
(
shape
.
size
()
>
0UL
,
"
shape can be one int or array. shape
must be set."
);
ctx
->
SetOutputDim
(
"Out"
,
framework
::
make_ddim
(
temp
));
}
...
...
@@ -74,7 +74,7 @@ GaussianRandom operator.
Use to initialize tensor with gaussian random generator.
)DOC"
);
AddAttr
<
std
::
vector
<
int
>>
(
"
dims
"
,
"The dimension of random tensor."
);
AddAttr
<
std
::
vector
<
int
>>
(
"
shape
"
,
"The dimension of random tensor."
);
AddAttr
<
float
>
(
"mean"
,
"mean of random tensor."
).
SetDefault
(
.0
f
);
AddAttr
<
float
>
(
"std"
,
"std of random tensor."
).
SetDefault
(
1.0
f
);
AddAttr
<
int
>
(
"seed"
,
...
...
python/paddle/v2/framework/initializer.py
浏览文件 @
db3b9438
...
...
@@ -62,7 +62,7 @@ class ConstantInitializer(Initializer):
class
UniformInitializer
(
Initializer
):
"""Implements
for
random uniform distribution initializer
"""Implements
the
random uniform distribution initializer
"""
def
__init__
(
self
,
low
=-
1.0
,
high
=
1.0
,
seed
=
0
):
...
...
@@ -75,6 +75,7 @@ class UniformInitializer(Initializer):
"""
assert
low
is
not
None
assert
high
is
not
None
assert
high
>=
low
assert
seed
is
not
None
super
(
UniformInitializer
,
self
).
__init__
()
self
.
_low
=
low
...
...
@@ -107,3 +108,51 @@ class UniformInitializer(Initializer):
})
var
.
op
=
op
return
op
class
NormalInitializer
(
Initializer
):
"""Implements the random Normal(Gaussian) distribution initializer
"""
def
__init__
(
self
,
loc
=
0.0
,
scale
=
1.0
,
seed
=
0
):
"""Constructor for NormalInitializer
Args:
loc: mean of the normal distribution
scale: standard deviation of the normal distribution
seed: random seed
"""
assert
loc
is
not
None
assert
scale
is
not
None
assert
seed
is
not
None
super
(
NormalInitializer
,
self
).
__init__
()
self
.
_mean
=
loc
self
.
_std_dev
=
scale
self
.
_seed
=
seed
def
__call__
(
self
,
var
,
block
):
"""Add normal 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
=
"gaussian_random"
,
outputs
=
{
"Out"
:
var
},
attrs
=
{
"shape"
:
var
.
shape
,
"data_type"
:
int
(
var
.
data_type
),
"mean"
:
self
.
_mean
,
"std"
:
self
.
_std_dev
,
"seed"
:
self
.
_seed
})
var
.
op
=
op
return
op
python/paddle/v2/framework/tests/test_gaussian_random_op.py
浏览文件 @
db3b9438
...
...
@@ -19,7 +19,7 @@ class TestGaussianRandomOp(unittest.TestCase):
op
=
Operator
(
"gaussian_random"
,
Out
=
'Out'
,
dims
=
[
1000
,
784
],
shape
=
[
1000
,
784
],
mean
=
.
0
,
std
=
1.
,
seed
=
10
)
...
...
python/paddle/v2/framework/tests/test_initializer.py
0 → 100644
浏览文件 @
db3b9438
import
unittest
import
paddle.v2.framework.framework
as
framework
import
paddle.v2.framework.initializer
as
initializer
DELTA
=
0.00001
class
TestConstantInitializer
(
unittest
.
TestCase
):
def
test_constant_initializer_default_value
(
self
):
"""Test the constant initializer with default value
"""
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
.
create_parameter
(
dtype
=
"float32"
,
shape
=
[
5
,
10
],
lod_level
=
0
,
name
=
"param"
,
initializer
=
initializer
.
ConstantInitializer
())
self
.
assertEqual
(
len
(
block
.
ops
),
1
)
init_op
=
block
.
ops
[
0
]
self
.
assertEqual
(
init_op
.
type
,
'fill_constant'
)
self
.
assertAlmostEqual
(
init_op
.
attr
(
'value'
),
0.0
,
delta
=
DELTA
)
def
test_constant_initializer
(
self
):
"""Test constant initializer with supplied value
"""
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
.
create_parameter
(
dtype
=
"float32"
,
shape
=
[
5
,
10
],
lod_level
=
0
,
name
=
"param"
,
initializer
=
initializer
.
ConstantInitializer
(
2.3
))
self
.
assertEqual
(
len
(
block
.
ops
),
1
)
init_op
=
block
.
ops
[
0
]
self
.
assertEqual
(
init_op
.
type
,
'fill_constant'
)
self
.
assertAlmostEqual
(
init_op
.
attr
(
'value'
),
2.3
,
delta
=
DELTA
)
class
TestUniformInitializer
(
unittest
.
TestCase
):
def
test_uniform_initializer_default_value
(
self
):
"""Test the uniform initializer with default value
"""
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
.
create_parameter
(
dtype
=
"float32"
,
shape
=
[
5
,
10
],
lod_level
=
0
,
name
=
"param"
,
initializer
=
initializer
.
UniformInitializer
())
self
.
assertEqual
(
len
(
block
.
ops
),
1
)
init_op
=
block
.
ops
[
0
]
self
.
assertEqual
(
init_op
.
type
,
'uniform_random'
)
self
.
assertAlmostEqual
(
init_op
.
attr
(
'min'
),
-
1.0
,
delta
=
DELTA
)
self
.
assertAlmostEqual
(
init_op
.
attr
(
'max'
),
1.0
,
delta
=
DELTA
)
self
.
assertEqual
(
init_op
.
attr
(
'seed'
),
0
)
def
test_uniform_initializer
(
self
):
"""Test uniform initializer with supplied attributes
"""
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
.
create_parameter
(
dtype
=
"float32"
,
shape
=
[
5
,
10
],
lod_level
=
0
,
name
=
"param"
,
initializer
=
initializer
.
UniformInitializer
(
-
4.2
,
3.1
,
123
))
self
.
assertEqual
(
len
(
block
.
ops
),
1
)
init_op
=
block
.
ops
[
0
]
self
.
assertEqual
(
init_op
.
type
,
'uniform_random'
)
self
.
assertAlmostEqual
(
init_op
.
attr
(
'min'
),
-
4.2
,
delta
=
DELTA
)
self
.
assertAlmostEqual
(
init_op
.
attr
(
'max'
),
3.1
,
delta
=
DELTA
)
self
.
assertEqual
(
init_op
.
attr
(
'seed'
),
123
)
class
TestNormalInitializer
(
unittest
.
TestCase
):
def
test_normal_initializer_default_value
(
self
):
"""Test the normal initializer with default value
"""
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
.
create_parameter
(
dtype
=
"float32"
,
shape
=
[
5
,
10
],
lod_level
=
0
,
name
=
"param"
,
initializer
=
initializer
.
NormalInitializer
())
self
.
assertEqual
(
len
(
block
.
ops
),
1
)
init_op
=
block
.
ops
[
0
]
self
.
assertEqual
(
init_op
.
type
,
'gaussian_random'
)
self
.
assertAlmostEqual
(
init_op
.
attr
(
'mean'
),
0.0
,
delta
=
DELTA
)
self
.
assertAlmostEqual
(
init_op
.
attr
(
'std'
),
1.0
,
delta
=
DELTA
)
self
.
assertEqual
(
init_op
.
attr
(
'seed'
),
0
)
def
test_normal_initializer
(
self
):
"""Test normal initializer with supplied attributes
"""
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
.
create_parameter
(
dtype
=
"float32"
,
shape
=
[
5
,
10
],
lod_level
=
0
,
name
=
"param"
,
initializer
=
initializer
.
NormalInitializer
(
2.3
,
1.9
,
123
))
self
.
assertEqual
(
len
(
block
.
ops
),
1
)
init_op
=
block
.
ops
[
0
]
self
.
assertEqual
(
init_op
.
type
,
'gaussian_random'
)
self
.
assertAlmostEqual
(
init_op
.
attr
(
'mean'
),
2.3
,
delta
=
DELTA
)
self
.
assertAlmostEqual
(
init_op
.
attr
(
'std'
),
1.9
,
delta
=
DELTA
)
self
.
assertEqual
(
init_op
.
attr
(
'seed'
),
123
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录