Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
582011ba
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
582011ba
编写于
12月 12, 2018
作者:
C
chengduo
提交者:
GitHub
12月 12, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add L2 unit test (#14792)
* add l2 unit test test=develop * code refine test=develop
上级
f95ee9c0
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
135 addition
and
1 deletion
+135
-1
python/paddle/fluid/tests/unittests/test_regularizer.py
python/paddle/fluid/tests/unittests/test_regularizer.py
+135
-1
未找到文件。
python/paddle/fluid/tests/unittests/test_regularizer.py
浏览文件 @
582011ba
...
...
@@ -15,7 +15,12 @@
from
__future__
import
print_function
import
unittest
from
functools
import
partial
import
contextlib
import
numpy
as
np
import
paddle
import
paddle.fluid.core
as
core
import
paddle.fluid
as
fluid
import
paddle.fluid.framework
as
framework
import
paddle.fluid.optimizer
as
optimizer
import
paddle.fluid.regularizer
as
regularizer
...
...
@@ -97,5 +102,134 @@ class TestL1DecayRegularizer(unittest.TestCase):
self
.
assertEqual
(
block
.
ops
[
-
3
].
type
,
'sign'
)
def
bow_net
(
data
,
label
,
dict_dim
,
is_sparse
=
False
,
emb_dim
=
128
,
hid_dim
=
128
,
hid_dim2
=
96
,
class_dim
=
2
):
"""
BOW net
This model is from https://github.com/PaddlePaddle/models:
fluid/PaddleNLP/text_classification/nets.py
"""
emb
=
fluid
.
layers
.
embedding
(
input
=
data
,
is_sparse
=
is_sparse
,
size
=
[
dict_dim
,
emb_dim
])
bow
=
fluid
.
layers
.
sequence_pool
(
input
=
emb
,
pool_type
=
'sum'
)
bow_tanh
=
fluid
.
layers
.
tanh
(
bow
)
fc_1
=
fluid
.
layers
.
fc
(
input
=
bow_tanh
,
size
=
hid_dim
,
act
=
"tanh"
)
fc_2
=
fluid
.
layers
.
fc
(
input
=
fc_1
,
size
=
hid_dim2
,
act
=
"tanh"
)
prediction
=
fluid
.
layers
.
fc
(
input
=
[
fc_2
],
size
=
class_dim
,
act
=
"softmax"
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
prediction
,
label
=
label
)
avg_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
return
avg_cost
class
TestRegularizer
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
reader
=
paddle
.
batch
(
paddle
.
dataset
.
imdb
.
train
(
self
.
word_dict
),
batch_size
=
8
)()
self
.
train_data
=
[
next
(
reader
)
for
_
in
range
(
5
)]
def
get_places
(
self
):
places
=
[
core
.
CPUPlace
()]
if
core
.
is_compiled_with_cuda
():
places
.
append
(
core
.
CUDAPlace
(
0
))
return
places
@
contextlib
.
contextmanager
def
scope_prog_guard
(
self
,
main_prog
,
startup_prog
):
scope
=
fluid
.
core
.
Scope
()
with
fluid
.
unique_name
.
guard
():
with
fluid
.
scope_guard
(
scope
):
with
fluid
.
program_guard
(
main_prog
,
startup_prog
):
yield
def
run_program
(
self
,
place
,
feed_list
):
exe
=
fluid
.
Executor
(
place
)
feeder
=
fluid
.
DataFeeder
(
feed_list
=
feed_list
,
place
=
place
)
exe
.
run
(
fluid
.
default_startup_program
())
main_prog
=
fluid
.
default_main_program
()
param_list
=
[
var
.
name
for
var
in
main_prog
.
block
(
0
).
all_parameters
()]
param_sum
=
[]
for
data
in
self
.
train_data
:
out
=
exe
.
run
(
main_prog
,
feed
=
feeder
.
feed
(
data
),
fetch_list
=
param_list
)
p_sum
=
0
for
v
in
out
:
p_sum
+=
np
.
sum
(
np
.
abs
(
v
))
param_sum
.
append
(
p_sum
)
return
param_sum
def
check_l2decay_regularizer
(
self
,
place
,
model
):
main_prog
=
fluid
.
framework
.
Program
()
startup_prog
=
fluid
.
framework
.
Program
()
startup_prog
.
random_seed
=
1
with
self
.
scope_prog_guard
(
main_prog
=
main_prog
,
startup_prog
=
startup_prog
):
data
=
fluid
.
layers
.
data
(
name
=
"words"
,
shape
=
[
1
],
dtype
=
"int64"
,
lod_level
=
1
)
label
=
fluid
.
layers
.
data
(
name
=
"label"
,
shape
=
[
1
],
dtype
=
"int64"
)
avg_cost
=
model
(
data
,
label
,
len
(
self
.
word_dict
))
optimizer
=
fluid
.
optimizer
.
Adagrad
(
learning_rate
=
0.1
,
regularization
=
fluid
.
regularizer
.
L2Decay
(
1.0
))
optimizer
.
minimize
(
avg_cost
)
param_sum
=
self
.
run_program
(
place
,
[
data
,
label
])
return
param_sum
def
check_l2decay
(
self
,
place
,
model
):
main_prog
=
fluid
.
framework
.
Program
()
startup_prog
=
fluid
.
framework
.
Program
()
startup_prog
.
random_seed
=
1
with
self
.
scope_prog_guard
(
main_prog
=
main_prog
,
startup_prog
=
startup_prog
):
data
=
fluid
.
layers
.
data
(
name
=
"words"
,
shape
=
[
1
],
dtype
=
"int64"
,
lod_level
=
1
)
label
=
fluid
.
layers
.
data
(
name
=
"label"
,
shape
=
[
1
],
dtype
=
"int64"
)
avg_cost_l2
=
model
(
data
,
label
,
len
(
self
.
word_dict
))
param_list
=
fluid
.
default_main_program
().
block
(
0
).
all_parameters
()
para_sum
=
[]
for
para
in
param_list
:
para_mul
=
fluid
.
layers
.
square
(
x
=
para
)
para_sum
.
append
(
fluid
.
layers
.
reduce_sum
(
input
=
para_mul
))
avg_cost_l2
+=
fluid
.
layers
.
sums
(
para_sum
)
*
.
5
optimizer
=
fluid
.
optimizer
.
Adagrad
(
learning_rate
=
0.1
)
optimizer
.
minimize
(
avg_cost_l2
)
param_sum
=
self
.
run_program
(
place
,
[
data
,
label
])
return
param_sum
def
test_l2
(
self
):
for
place
in
self
.
get_places
():
dense_sparse_p_sum
=
[]
for
sparse
in
[
True
,
False
]:
model
=
partial
(
bow_net
,
is_sparse
=
sparse
)
framework_l2
=
self
.
check_l2decay_regularizer
(
place
,
model
)
l2
=
self
.
check_l2decay
(
place
,
model
)
assert
len
(
l2
)
==
len
(
framework_l2
)
for
i
in
range
(
len
(
l2
)):
assert
np
.
isclose
(
a
=
framework_l2
[
i
],
b
=
l2
[
i
],
rtol
=
5e-5
)
dense_sparse_p_sum
.
append
(
framework_l2
)
assert
len
(
dense_sparse_p_sum
[
0
])
==
len
(
dense_sparse_p_sum
[
1
])
for
i
in
range
(
len
(
dense_sparse_p_sum
[
0
])):
assert
np
.
isclose
(
a
=
dense_sparse_p_sum
[
0
][
i
],
b
=
dense_sparse_p_sum
[
1
][
i
],
rtol
=
5e-5
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录