Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
PaddleDetection
提交
a61e7d0f
P
PaddleDetection
项目概览
s920243400
/
PaddleDetection
与 Fork 源项目一致
Fork自
PaddlePaddle / PaddleDetection
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
a61e7d0f
编写于
1月 15, 2019
作者:
X
Xin Pan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dy gan mostly working
test=develop
上级
03fe3109
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
101 addition
and
44 deletion
+101
-44
python/paddle/fluid/imperative/layers.py
python/paddle/fluid/imperative/layers.py
+6
-3
python/paddle/fluid/imperative/nn.py
python/paddle/fluid/imperative/nn.py
+23
-8
python/paddle/fluid/tests/unittests/test_imperative_gan.py
python/paddle/fluid/tests/unittests/test_imperative_gan.py
+72
-33
未找到文件。
python/paddle/fluid/imperative/layers.py
浏览文件 @
a61e7d0f
...
@@ -27,18 +27,21 @@ class Layer(core.Layer):
...
@@ -27,18 +27,21 @@ class Layer(core.Layer):
"""Layers composed of operators."""
"""Layers composed of operators."""
def
__init__
(
self
,
dtype
=
core
.
VarDesc
.
VarType
.
FP32
,
name
=
None
):
def
__init__
(
self
,
dtype
=
core
.
VarDesc
.
VarType
.
FP32
,
name
=
None
):
self
.
_
once_
built
=
False
self
.
_built
=
False
self
.
_dtype
=
dtype
self
.
_dtype
=
dtype
def
parameters
(
self
):
return
[]
def
_build_once
(
self
,
inputs
):
def
_build_once
(
self
,
inputs
):
pass
pass
def
__call__
(
self
,
*
inputs
):
def
__call__
(
self
,
*
inputs
):
if
not
self
.
_
once_
built
:
if
not
self
.
_built
:
self
.
_build_once
(
*
inputs
)
self
.
_build_once
(
*
inputs
)
self
.
_once_built
=
True
outputs
=
self
.
forward
(
*
inputs
)
outputs
=
self
.
forward
(
*
inputs
)
self
.
_built
=
True
return
outputs
return
outputs
def
forward
(
self
,
*
inputs
):
def
forward
(
self
,
*
inputs
):
...
...
python/paddle/fluid/imperative/nn.py
浏览文件 @
a61e7d0f
...
@@ -220,11 +220,14 @@ class FC(layers.Layer):
...
@@ -220,11 +220,14 @@ class FC(layers.Layer):
self
.
_dtype
=
dtype
self
.
_dtype
=
dtype
from
..layer_helper
import
LayerHelper
from
..layer_helper
import
LayerHelper
self
.
_helper
=
LayerHelper
(
self
.
_helper
=
LayerHelper
(
'FC'
,
'FC'
,
param_attr
=
param_attr
,
act
=
act
,
name
=
name
)
param_attr
=
param_attr
,
self
.
_bias_attr
=
bias_attr
bias_attr
=
bias_attr
,
act
=
act
,
def
parameters
(
self
):
name
=
name
)
if
self
.
_bias_attr
:
return
[
self
.
_w
,
self
.
_b
]
else
:
return
[
self
.
_w
]
def
_build_once
(
self
,
input
):
def
_build_once
(
self
,
input
):
input_shape
=
input
.
shape
input_shape
=
input
.
shape
...
@@ -255,8 +258,20 @@ class FC(layers.Layer):
...
@@ -255,8 +258,20 @@ class FC(layers.Layer):
inputs
=
{
"X"
:
[
tmp
]},
inputs
=
{
"X"
:
[
tmp
]},
outputs
=
{
"Out"
:
out
},
outputs
=
{
"Out"
:
out
},
attrs
=
{
"use_mkldnn"
:
False
})
attrs
=
{
"use_mkldnn"
:
False
})
if
not
self
.
_bias_attr
:
return
out
# add bias
# add bias
pre_activation
=
self
.
_helper
.
append_bias_op
(
size
=
list
(
out
.
shape
[
1
:])
out
,
dim_start
=
self
.
_num_flatten_dims
)
if
not
self
.
_built
:
self
.
_b
=
self
.
_layer
.
create_parameter
(
attr
=
self
.
_bias_attr
,
shape
=
size
,
dtype
=
out
.
dtype
,
is_bias
=
True
)
bias_out
=
self
.
create_variable_for_type_inference
(
dtype
=
out
.
dtype
)
self
.
append_op
(
type
=
'elementwise_add'
,
inputs
=
{
'X'
:
[
out
],
'Y'
:
[
self
.
_b
]},
outputs
=
{
'Out'
:
[
bias_out
]},
attrs
=
{
'axis'
:
1
})
# add activation
# add activation
return
self
.
_helper
.
append_activation
(
pre_activation
)
return
self
.
_helper
.
append_activation
(
bias_out
)
python/paddle/fluid/tests/unittests/test_imperative_gan.py
浏览文件 @
a61e7d0f
...
@@ -23,6 +23,7 @@ import paddle.fluid as fluid
...
@@ -23,6 +23,7 @@ import paddle.fluid as fluid
from
paddle.fluid.optimizer
import
SGDOptimizer
from
paddle.fluid.optimizer
import
SGDOptimizer
from
paddle.fluid.imperative.nn
import
Conv2D
,
Pool2D
,
FC
from
paddle.fluid.imperative.nn
import
Conv2D
,
Pool2D
,
FC
from
test_imperative_base
import
new_program_scope
from
test_imperative_base
import
new_program_scope
from
paddle.fluid.imperative.base
import
to_variable
class
Discriminator
(
fluid
.
imperative
.
Layer
):
class
Discriminator
(
fluid
.
imperative
.
Layer
):
...
@@ -31,6 +32,9 @@ class Discriminator(fluid.imperative.Layer):
...
@@ -31,6 +32,9 @@ class Discriminator(fluid.imperative.Layer):
self
.
_fc1
=
FC
(
size
=
32
,
act
=
'elu'
,
name
=
"d_fc1"
)
self
.
_fc1
=
FC
(
size
=
32
,
act
=
'elu'
,
name
=
"d_fc1"
)
self
.
_fc2
=
FC
(
size
=
1
,
name
=
"d_fc2"
)
self
.
_fc2
=
FC
(
size
=
1
,
name
=
"d_fc2"
)
def
parameters
(
self
):
return
self
.
_fc1
.
parameters
()
+
self
.
_fc2
.
parameters
()
def
forward
(
self
,
inputs
):
def
forward
(
self
,
inputs
):
x
=
self
.
_fc1
(
inputs
)
x
=
self
.
_fc1
(
inputs
)
return
self
.
_fc2
(
x
)
return
self
.
_fc2
(
x
)
...
@@ -43,6 +47,10 @@ class Generator(fluid.imperative.Layer):
...
@@ -43,6 +47,10 @@ class Generator(fluid.imperative.Layer):
self
.
_fc2
=
FC
(
size
=
64
,
act
=
'elu'
,
name
=
"g_fc2"
)
self
.
_fc2
=
FC
(
size
=
64
,
act
=
'elu'
,
name
=
"g_fc2"
)
self
.
_fc3
=
FC
(
size
=
1
,
name
=
"g_fc3"
)
self
.
_fc3
=
FC
(
size
=
1
,
name
=
"g_fc3"
)
def
parameters
(
self
):
return
self
.
_fc1
.
parameters
()
+
self
.
_fc2
.
parameters
(
)
+
self
.
_fc3
.
parameters
()
def
forward
(
self
,
inputs
):
def
forward
(
self
,
inputs
):
x
=
self
.
_fc1
(
inputs
)
x
=
self
.
_fc1
(
inputs
)
x
=
self
.
_fc2
(
x
)
x
=
self
.
_fc2
(
x
)
...
@@ -56,12 +64,15 @@ class TestImperativeMnist(unittest.TestCase):
...
@@ -56,12 +64,15 @@ class TestImperativeMnist(unittest.TestCase):
startup
=
fluid
.
Program
()
startup
=
fluid
.
Program
()
startup
.
random_seed
=
seed
startup
.
random_seed
=
seed
discriminate_p
=
fluid
.
Program
()
discriminate_p
=
fluid
.
Program
()
generate_p
=
fluid
.
Program
()
discriminate_p
.
random_seed
=
seed
generate_p
.
random_seed
=
seed
scope
=
fluid
.
core
.
Scope
()
scope
=
fluid
.
core
.
Scope
()
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
sys
.
stderr
.
write
(
'1111
\n
'
)
with
new_program_scope
(
with
new_program_scope
(
main
=
discriminate_p
,
startup
=
startup
,
scope
=
scope
):
main
=
discriminate_p
,
startup
=
startup
,
scope
=
scope
):
fluid
.
default_main_program
().
random_seed
=
seed
discriminator
=
Discriminator
()
discriminator
=
Discriminator
()
generator
=
Generator
()
generator
=
Generator
()
...
@@ -70,64 +81,92 @@ class TestImperativeMnist(unittest.TestCase):
...
@@ -70,64 +81,92 @@ class TestImperativeMnist(unittest.TestCase):
noise
=
fluid
.
layers
.
data
(
noise
=
fluid
.
layers
.
data
(
name
=
"noise"
,
shape
=
[
2
,
2
],
append_batch_size
=
False
)
name
=
"noise"
,
shape
=
[
2
,
2
],
append_batch_size
=
False
)
label
=
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
2
,
1
],
dtype
=
'float32'
,
append_batch_size
=
False
)
d_real
=
discriminator
(
img
)
d_real
=
discriminator
(
img
)
d_loss_real
=
fluid
.
layers
.
reduce_mean
(
d_loss_real
=
fluid
.
layers
.
reduce_mean
(
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
x
=
d_real
,
label
=
label
))
x
=
d_real
,
label
=
fluid
.
layers
.
fill_constant
(
shape
=
[
2
,
1
],
dtype
=
'float32'
,
value
=
1.0
)))
d_fake
=
discriminator
(
generator
(
noise
))
d_fake
=
discriminator
(
generator
(
noise
))
d_loss_fake
=
fluid
.
layers
.
reduce_mean
(
d_loss_fake
=
fluid
.
layers
.
reduce_mean
(
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
x
=
d_fake
,
label
=
label
))
x
=
d_fake
,
label
=
fluid
.
layers
.
fill_constant
(
shape
=
[
2
,
1
],
dtype
=
'float32'
,
value
=
0.0
)))
d_loss
=
d_loss_real
+
d_loss_fake
d_loss
=
d_loss_real
+
d_loss_fake
sgd
=
SGDOptimizer
(
learning_rate
=
1e-3
)
sgd
=
SGDOptimizer
(
learning_rate
=
1e-3
)
sgd
.
minimize
(
d_loss
)
sgd
.
minimize
(
d_loss
)
generate_p
=
fluid
.
Program
()
with
new_program_scope
(
main
=
generate_p
,
startup
=
startup
,
scope
=
scope
):
with
new_program_scope
(
main
=
generate_p
,
startup
=
startup
,
scope
=
scope
):
fluid
.
default_main_program
().
random_seed
=
seed
discriminator
=
Discriminator
()
discriminator
=
Discriminator
()
generator
=
Generator
()
generator
=
Generator
()
noise
=
fluid
.
layers
.
data
(
noise
=
fluid
.
layers
.
data
(
name
=
"noise"
,
shape
=
[
2
,
2
],
append_batch_size
=
False
)
name
=
"noise"
,
shape
=
[
2
,
2
],
append_batch_size
=
False
)
label
=
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
2
,
1
],
dtype
=
'float32'
,
append_batch_size
=
False
)
d_fake
=
discriminator
(
generator
(
noise
))
d_fake
=
discriminator
(
generator
(
noise
))
g_loss
=
fluid
.
layers
.
reduce_mean
(
g_loss
=
fluid
.
layers
.
reduce_mean
(
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
x
=
d_fake
,
label
=
label
))
x
=
d_fake
,
label
=
fluid
.
layers
.
fill_constant
(
shape
=
[
2
,
1
],
dtype
=
'float32'
,
value
=
1.0
)))
sgd
=
SGDOptimizer
(
learning_rate
=
1e-3
)
sgd
=
SGDOptimizer
(
learning_rate
=
1e-3
)
sgd
.
minimize
(
g_loss
)
sgd
.
minimize
(
g_loss
)
img
=
np
.
ones
([
2
,
1
],
np
.
float32
)
with
fluid
.
scope_guard
(
scope
):
label
=
np
.
ones
([
2
,
1
],
np
.
float32
)
img
=
np
.
ones
([
2
,
1
],
np
.
float32
)
noise
=
np
.
ones
([
2
,
2
],
np
.
float32
)
noise
=
np
.
ones
([
2
,
2
],
np
.
float32
)
exe
.
run
(
startup
)
exe
.
run
(
startup
)
d_loss_val
=
exe
.
run
(
discriminate_p
,
d_loss_val
=
exe
.
run
(
discriminate_p
,
feed
=
{
'img'
:
img
,
feed
=
{
'img'
:
img
,
'noise'
:
noise
,
'noise'
:
noise
},
'label'
:
label
},
fetch_list
=
[
d_loss
])[
0
]
fetch_list
=
[
d_loss
])[
0
]
g_loss_val
=
exe
.
run
(
generate_p
,
g_loss_val
=
exe
.
run
(
generate_p
,
feed
=
{
'noise'
:
noise
},
feed
=
{
'noise'
:
noise
,
fetch_list
=
[
g_loss
])[
0
]
'label'
:
label
},
sys
.
stderr
.
write
(
'd_loss %s, g_loss: %s
\n
'
%
fetch_list
=
[
g_loss
])[
0
]
(
d_loss_val
,
g_loss_val
))
sys
.
stderr
.
write
(
'd_loss %s, g_loss: %s
\n
'
%
(
d_loss_val
,
g_loss_val
))
static_params
=
dict
()
for
param
in
discriminate_p
.
global_block
().
all_parameters
():
sys
.
stderr
.
write
(
'%s
\n
'
%
param
.
name
)
static_params
[
param
.
name
]
=
np
.
array
(
scope
.
find_var
(
param
.
name
).
get_tensor
())
dy_params
=
dict
()
with
fluid
.
imperative
.
guard
():
fluid
.
default_startup_program
().
random_seed
=
seed
fluid
.
default_main_program
().
random_seed
=
seed
discriminator
=
Discriminator
()
generator
=
Generator
()
sgd
=
SGDOptimizer
(
learning_rate
=
1e-3
)
d_real
=
discriminator
(
to_variable
(
np
.
ones
([
2
,
1
],
np
.
float32
)))
d_loss_real
=
fluid
.
layers
.
reduce_mean
(
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
x
=
d_real
,
label
=
to_variable
(
np
.
ones
([
2
,
1
],
np
.
float32
))))
d_fake
=
discriminator
(
generator
(
to_variable
(
np
.
ones
([
2
,
2
],
np
.
float32
))))
d_loss_fake
=
fluid
.
layers
.
reduce_mean
(
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
x
=
d_fake
,
label
=
to_variable
(
np
.
zeros
([
2
,
1
],
np
.
float32
))))
d_loss
=
d_loss_real
+
d_loss_fake
sys
.
stderr
.
write
(
'dy_d_loss: %s
\n
'
%
d_loss
.
_numpy
())
d_loss
.
_backward
()
sgd
.
minimize
(
d_loss
)
for
p
in
discriminator
.
parameters
():
dy_params
[
p
.
name
]
=
p
.
_numpy
()
for
k
,
v
in
six
.
iteritems
(
dy_params
):
sys
.
stderr
.
write
(
'dy_param_loss: %s: %s
\n
'
%
(
k
,
np
.
sum
(
v
)))
sys
.
stderr
.
write
(
'static_param_loss: %s: %s
\n
'
%
(
k
,
np
.
sum
(
v
)))
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录