Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
03fe3109
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
03fe3109
编写于
1月 15, 2019
作者:
X
Xin Pan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add static GAN
上级
b29eca3b
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
155 addition
and
8 deletion
+155
-8
python/paddle/fluid/imperative/nn.py
python/paddle/fluid/imperative/nn.py
+15
-3
python/paddle/fluid/tests/unittests/test_imperative_base.py
python/paddle/fluid/tests/unittests/test_imperative_base.py
+6
-5
python/paddle/fluid/tests/unittests/test_imperative_gan.py
python/paddle/fluid/tests/unittests/test_imperative_gan.py
+134
-0
未找到文件。
python/paddle/fluid/imperative/nn.py
浏览文件 @
03fe3109
...
...
@@ -209,14 +209,22 @@ class FC(layers.Layer):
def
__init__
(
self
,
size
,
param_attr
=
None
,
bias_attr
=
None
,
num_flatten_dims
=
1
,
dtype
=
core
.
VarDesc
.
VarType
.
FP32
):
dtype
=
core
.
VarDesc
.
VarType
.
FP32
,
act
=
None
,
name
=
None
):
super
(
FC
,
self
).
__init__
()
self
.
_size
=
size
self
.
_num_flatten_dims
=
num_flatten_dims
self
.
_dtype
=
dtype
from
..layer_helper
import
LayerHelper
self
.
_helper
=
LayerHelper
(
'FC'
,
param_attr
=
param_attr
)
self
.
_helper
=
LayerHelper
(
'FC'
,
param_attr
=
param_attr
,
bias_attr
=
bias_attr
,
act
=
act
,
name
=
name
)
def
_build_once
(
self
,
input
):
input_shape
=
input
.
shape
...
...
@@ -247,4 +255,8 @@ class FC(layers.Layer):
inputs
=
{
"X"
:
[
tmp
]},
outputs
=
{
"Out"
:
out
},
attrs
=
{
"use_mkldnn"
:
False
})
return
out
# add bias
pre_activation
=
self
.
_helper
.
append_bias_op
(
out
,
dim_start
=
self
.
_num_flatten_dims
)
# add activation
return
self
.
_helper
.
append_activation
(
pre_activation
)
python/paddle/fluid/tests/unittests/test_imperative_base.py
浏览文件 @
03fe3109
...
...
@@ -21,10 +21,11 @@ from paddle.fluid import core
@
contextlib
.
contextmanager
def
new_program_scope
():
prog
=
fluid
.
Program
()
startup_prog
=
fluid
.
Program
()
scope
=
fluid
.
core
.
Scope
()
def
new_program_scope
(
main
=
None
,
startup
=
None
,
scope
=
None
):
prog
=
main
if
main
else
fluid
.
Program
()
startup_prog
=
startup
if
startup
else
fluid
.
Program
()
scope
=
scope
if
scope
else
fluid
.
core
.
Scope
()
with
fluid
.
scope_guard
(
scope
):
with
fluid
.
program_guard
(
prog
,
startup_prog
):
with
fluid
.
unique_name
.
guard
():
yield
python/paddle/fluid/tests/unittests/test_imperative_gan.py
0 → 100644
浏览文件 @
03fe3109
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
contextlib
import
unittest
import
numpy
as
np
import
six
import
sys
import
paddle
import
paddle.fluid
as
fluid
from
paddle.fluid.optimizer
import
SGDOptimizer
from
paddle.fluid.imperative.nn
import
Conv2D
,
Pool2D
,
FC
from
test_imperative_base
import
new_program_scope
class
Discriminator
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
):
super
(
Discriminator
,
self
).
__init__
()
self
.
_fc1
=
FC
(
size
=
32
,
act
=
'elu'
,
name
=
"d_fc1"
)
self
.
_fc2
=
FC
(
size
=
1
,
name
=
"d_fc2"
)
def
forward
(
self
,
inputs
):
x
=
self
.
_fc1
(
inputs
)
return
self
.
_fc2
(
x
)
class
Generator
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
):
super
(
Generator
,
self
).
__init__
()
self
.
_fc1
=
FC
(
size
=
64
,
act
=
'elu'
,
name
=
"g_fc1"
)
self
.
_fc2
=
FC
(
size
=
64
,
act
=
'elu'
,
name
=
"g_fc2"
)
self
.
_fc3
=
FC
(
size
=
1
,
name
=
"g_fc3"
)
def
forward
(
self
,
inputs
):
x
=
self
.
_fc1
(
inputs
)
x
=
self
.
_fc2
(
x
)
return
self
.
_fc3
(
x
)
class
TestImperativeMnist
(
unittest
.
TestCase
):
def
test_mnist_cpu_float32
(
self
):
seed
=
90
startup
=
fluid
.
Program
()
startup
.
random_seed
=
seed
discriminate_p
=
fluid
.
Program
()
scope
=
fluid
.
core
.
Scope
()
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
with
new_program_scope
(
main
=
discriminate_p
,
startup
=
startup
,
scope
=
scope
):
fluid
.
default_main_program
().
random_seed
=
seed
discriminator
=
Discriminator
()
generator
=
Generator
()
img
=
fluid
.
layers
.
data
(
name
=
"img"
,
shape
=
[
2
,
1
],
append_batch_size
=
False
)
noise
=
fluid
.
layers
.
data
(
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_loss_real
=
fluid
.
layers
.
reduce_mean
(
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
x
=
d_real
,
label
=
label
))
d_fake
=
discriminator
(
generator
(
noise
))
d_loss_fake
=
fluid
.
layers
.
reduce_mean
(
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
x
=
d_fake
,
label
=
label
))
d_loss
=
d_loss_real
+
d_loss_fake
sgd
=
SGDOptimizer
(
learning_rate
=
1e-3
)
sgd
.
minimize
(
d_loss
)
generate_p
=
fluid
.
Program
()
with
new_program_scope
(
main
=
generate_p
,
startup
=
startup
,
scope
=
scope
):
fluid
.
default_main_program
().
random_seed
=
seed
discriminator
=
Discriminator
()
generator
=
Generator
()
noise
=
fluid
.
layers
.
data
(
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
))
g_loss
=
fluid
.
layers
.
reduce_mean
(
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
x
=
d_fake
,
label
=
label
))
sgd
=
SGDOptimizer
(
learning_rate
=
1e-3
)
sgd
.
minimize
(
g_loss
)
img
=
np
.
ones
([
2
,
1
],
np
.
float32
)
label
=
np
.
ones
([
2
,
1
],
np
.
float32
)
noise
=
np
.
ones
([
2
,
2
],
np
.
float32
)
exe
.
run
(
startup
)
d_loss_val
=
exe
.
run
(
discriminate_p
,
feed
=
{
'img'
:
img
,
'noise'
:
noise
,
'label'
:
label
},
fetch_list
=
[
d_loss
])[
0
]
g_loss_val
=
exe
.
run
(
generate_p
,
feed
=
{
'noise'
:
noise
,
'label'
:
label
},
fetch_list
=
[
g_loss
])[
0
]
sys
.
stderr
.
write
(
'd_loss %s, g_loss: %s
\n
'
%
(
d_loss_val
,
g_loss_val
))
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录