Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
1fda6e1c
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看板
提交
1fda6e1c
编写于
10月 11, 2019
作者:
F
FDInSky
提交者:
lvmengsi
10月 11, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
test=develop enhance uniform_random op python api (#20376)
上级
3c185cfa
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
40 addition
and
5 deletion
+40
-5
paddle/fluid/operators/uniform_random_op.cc
paddle/fluid/operators/uniform_random_op.cc
+9
-4
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+9
-1
python/paddle/fluid/tests/unittests/test_uniform_random_op.py
...on/paddle/fluid/tests/unittests/test_uniform_random_op.py
+22
-0
未找到文件。
paddle/fluid/operators/uniform_random_op.cc
浏览文件 @
1fda6e1c
...
...
@@ -75,7 +75,11 @@ class CPUUniformRandomKernel : public framework::OpKernel<T> {
auto
diag_val
=
static_cast
<
T
>
(
ctx
.
Attr
<
float
>
(
"diag_val"
));
if
(
diag_num
>
0
)
{
PADDLE_ENFORCE_GT
(
size
,
(
diag_num
-
1
)
*
(
diag_step
+
1
),
"The index of diagonal elements is out of bounds"
);
"ShapeError: the diagonal's elements is equal (num-1) "
"* (step-1) with num %d, step %d,"
"It should be smaller than %d, but received %d"
,
diag_num
,
diag_step
,
(
diag_num
-
1
)
*
(
diag_step
+
1
),
size
);
for
(
int64_t
i
=
0
;
i
<
diag_num
;
++
i
)
{
int64_t
pos
=
i
*
diag_step
+
i
;
data
[
pos
]
=
diag_val
;
...
...
@@ -118,9 +122,10 @@ class UniformRandomOp : public framework::OperatorWithKernel {
auto
shape_dims
=
ctx
->
GetInputDim
(
"ShapeTensor"
);
PADDLE_ENFORCE_EQ
(
shape_dims
.
size
(),
1
,
"Input(ShapeTensor)' dimension size of Op(uniform_random) must be 1."
"Please check the Attr(shape)'s dimension size of"
"Op(fluid.layers.uniform_random).)"
);
"ShapeError: Input(ShapeTensor)' dimension size of "
"Op(uniform_random) must be 1."
"But received ShapeTensor's dimensions = %d, shape = [%s]"
,
shape_dims
.
size
(),
shape_dims
);
int
num_ele
=
1
;
for
(
int
i
=
0
;
i
<
shape_dims
.
size
();
++
i
)
{
num_ele
*=
shape_dims
[
i
];
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
1fda6e1c
...
...
@@ -16597,10 +16597,18 @@ def uniform_random(shape, dtype='float32', min=-1.0, max=1.0, seed=0):
"""
if not (isinstance(shape, (list, tuple, Variable))):
raise TypeError("Input shape must be a python list,Variable or tuple.")
raise TypeError(
"Input shape must be a python list,Variable or tuple. But received %s"
% (type(shape)))
if not isinstance(dtype, core.VarDesc.VarType):
dtype = convert_np_dtype_to_dtype_(dtype)
if convert_dtype(dtype) not in ['float32', 'float64']:
raise TypeError(
"The attribute dtype in uniform_random op must be float32 or float64, but received %s."
% (convert_dtype(dtype)))
def contain_var(one_list):
for ele in one_list:
if isinstance(ele, Variable):
...
...
python/paddle/fluid/tests/unittests/test_uniform_random_op.py
浏览文件 @
1fda6e1c
...
...
@@ -20,6 +20,7 @@ from op_test import OpTest
import
paddle.fluid.core
as
core
from
paddle.fluid.op
import
Operator
import
paddle.fluid
as
fluid
from
paddle.fluid
import
Program
,
program_guard
def
output_hist
(
out
):
...
...
@@ -116,6 +117,27 @@ class TestUniformRandomOp(OpTest):
hist
,
prob
,
rtol
=
0
,
atol
=
0.01
),
"hist: "
+
str
(
hist
))
class
TestUniformRandomOpError
(
OpTest
):
def
test_errors
(
self
):
main_prog
=
Program
()
start_prog
=
Program
()
with
program_guard
(
main_prog
,
start_prog
):
def
test_Variable
():
x1
=
fluid
.
create_lod_tensor
(
np
.
zeros
((
4
,
784
)),
[[
1
,
1
,
1
,
1
]],
fluid
.
CPUPlace
())
fluid
.
layers
.
uniform_random
(
x1
)
self
.
assertRaises
(
TypeError
,
test_Variable
)
def
test_dtype
():
x2
=
fluid
.
layers
.
data
(
name
=
'x2'
,
shape
=
[
4
,
784
],
dtype
=
'float32'
)
fluid
.
layers
.
uniform_random
(
x2
,
'int32'
)
self
.
assertRaises
(
TypeError
,
test_dtype
)
class
TestUniformRandomOpWithDiagInit
(
TestUniformRandomOp
):
def
init_attrs
(
self
):
self
.
attrs
=
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录