Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
1456b8ec
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
1456b8ec
编写于
10月 10, 2018
作者:
M
minqiyang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add unittest for clip_by_norm_op with SelectedRows
test=develop
上级
bcd8c2cc
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
45 addition
and
25 deletion
+45
-25
paddle/fluid/operators/clip_by_norm_op.h
paddle/fluid/operators/clip_by_norm_op.h
+1
-0
python/paddle/fluid/tests/unittests/test_clip_by_norm_op.py
python/paddle/fluid/tests/unittests/test_clip_by_norm_op.py
+44
-25
未找到文件。
paddle/fluid/operators/clip_by_norm_op.h
浏览文件 @
1456b8ec
...
@@ -61,6 +61,7 @@ class ClipByNormKernel : public framework::OpKernel<T> {
...
@@ -61,6 +61,7 @@ class ClipByNormKernel : public framework::OpKernel<T> {
output_selected_rows
->
set_height
(
merged_input
->
height
());
output_selected_rows
->
set_height
(
merged_input
->
height
());
output
=
output_selected_rows
->
mutable_value
();
output
=
output_selected_rows
->
mutable_value
();
output
->
Resize
(
merged_input
->
value
().
dims
());
output
->
Resize
(
merged_input
->
value
().
dims
());
output
->
mutable_data
<
T
>
(
context
.
GetPlace
());
}
else
{
}
else
{
PADDLE_THROW
(
"Unexpected branch, input variable type is %s"
,
PADDLE_THROW
(
"Unexpected branch, input variable type is %s"
,
in_var
->
Type
().
name
());
in_var
->
Type
().
name
());
...
...
python/paddle/fluid/tests/unittests/test_clip_by_norm_op.py
浏览文件 @
1456b8ec
...
@@ -18,6 +18,7 @@ import unittest
...
@@ -18,6 +18,7 @@ import unittest
import
numpy
as
np
import
numpy
as
np
from
op_test
import
OpTest
from
op_test
import
OpTest
import
paddle.fluid
as
fluid
import
paddle.fluid.core
as
core
import
paddle.fluid.core
as
core
...
@@ -65,39 +66,57 @@ class TestCase3(TestClipByNormOp):
...
@@ -65,39 +66,57 @@ class TestCase3(TestClipByNormOp):
class
TestClipByNormOpWithSelectedRows
(
OpTest
):
class
TestClipByNormOpWithSelectedRows
(
OpTest
):
def
setUp
(
self
):
def
check_with_place
(
self
,
place
):
self
.
initTestCase
()
self
.
config_test_case
()
self
.
max_relative_error
=
0.006
scope
=
core
.
Scope
()
scope
=
core
.
Scope
()
# set input
x_selected_rows
=
scope
.
var
(
'X'
).
get_selected_rows
()
x_selected_rows
=
scope
.
var
(
'X'
).
get_selected_rows
()
x_selected_rows
.
set_rows
(
[
1
,
1
,
2
,
0
]
)
x_selected_rows
.
set_rows
(
self
.
grad_rows
)
x_tensor
=
x_selected_rows
.
get_tensor
()
x_tensor
=
x_selected_rows
.
get_tensor
()
x_tensor
=
np
.
random
.
random
((
4
,
1
)).
astype
(
"float32"
)
x_np
=
np
.
random
.
random
(
self
.
grad_shape
).
astype
(
"float32"
)
x_tensor
[
np
.
abs
(
x_tensor
)
<
self
.
max_relative_error
]
=
0.5
x_np
[
np
.
abs
(
x_np
)
<
self
.
max_relative_error
]
=
0.5
x_tensor
.
set
(
x_np
,
place
)
self
.
op_type
=
"clip_by_norm"
self
.
inputs
=
{
'X'
:
x_selected_rows
,
}
# set output
self
.
attrs
=
{}
out_selected_rows
=
scope
.
var
(
'Out'
).
get_selected_rows
()
self
.
attrs
[
'max_norm'
]
=
self
.
max_norm
y_tensor
=
np
.
zeros
((
3
,
1
))
# run clip_by_norm_op
y_tensor
[
0
::
1
]
=
np
.
sum
(
x_tensor
[
0
::
1
],
x_tensor
[
1
::
1
])
clip_by_norm_op
=
fluid
.
op
.
Operator
(
y_tensor
[
1
::
1
]
=
x_tensor
[
2
::
1
]
"clip_by_norm"
,
max_norm
=
self
.
max_norm
,
X
=
'X'
,
Out
=
'Out'
)
y_tensor
[
2
::
1
]
=
x_tensor
[
3
::
1
]
clip_by_norm_op
.
run
(
scope
,
place
)
norm
=
np
.
sqrt
(
np
.
sum
(
np
.
square
(
y_tensor
)))
# check output
self
.
assertEqual
(
out_selected_rows
.
rows
(),
self
.
grad_clipped_rows
)
out_tensor
=
out_selected_rows
.
get_tensor
()
y_np
=
np
.
zeros
(
self
.
grad_clipped_shape
)
y_np
[
0
]
=
np
.
sum
(
x_np
[
0
:
2
])
y_np
[
1
]
=
x_np
[
2
]
y_np
[
2
]
=
x_np
[
3
]
norm
=
np
.
sqrt
(
np
.
sum
(
np
.
square
(
y_np
)))
if
norm
>
self
.
max_norm
:
if
norm
>
self
.
max_norm
:
output
=
self
.
max_norm
*
y_
tensor
/
norm
output
=
self
.
max_norm
*
y_
np
/
norm
else
:
else
:
output
=
y_tensor
output
=
y_np
self
.
outputs
=
{
'Out'
:
output
}
self
.
assertTrue
(
np
.
allclose
(
np
.
array
(
out_tensor
),
output
,
atol
=
1e-5
,
equal_nan
=
False
))
def
test_check_output
(
self
):
def
test_clip_by_norm_with_selected_ros
(
self
):
self
.
check_output
()
places
=
[
core
.
CPUPlace
()]
if
core
.
is_compiled_with_cuda
():
places
.
append
(
core
.
CUDAPlace
(
0
))
def
initTestCase
(
self
):
for
place
in
places
:
self
.
shape
=
(
100
,
)
self
.
check_with_place
(
place
)
def
config_test_case
(
self
):
self
.
max_norm
=
1.0
self
.
max_norm
=
1.0
self
.
max_relative_error
=
0.006
self
.
grad_shape
=
(
4
,
1
)
self
.
grad_clipped_shape
=
(
3
,
1
)
self
.
grad_rows
=
[
0
,
0
,
1
,
2
]
self
.
grad_clipped_rows
=
[
0
,
1
,
2
]
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录