Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
4767fb67
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4767fb67
编写于
10月 09, 2017
作者:
Z
zchen0211
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
gan api modified
上级
4238b9b9
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
53 addition
and
14 deletion
+53
-14
doc/design/gan_api.md
doc/design/gan_api.md
+53
-14
未找到文件。
doc/design/gan_api.md
浏览文件 @
4767fb67
...
...
@@ -139,10 +139,10 @@ class DCGAN(object):
-
Define data readers as placeholders to hold the data;
-
Build generator and discriminators;
-
Define two training losses for discriminator and generator, respectively.
If we have execution dependency engine to back-trace all tensors, the module building our GAN model will be like this:
```
python
class
DCGAN
(
object
):
def
build_model
(
self
):
# input data
if
self
.
y_dim
:
self
.
y
=
pd
.
data
(
pd
.
float32
,
[
self
.
batch_size
,
self
.
y_dim
])
self
.
images
=
pd
.
data
(
pd
.
float32
,
[
self
.
batch_size
,
self
.
im_size
,
self
.
im_size
])
...
...
@@ -151,17 +151,17 @@ class DCGAN(object):
# step 1: generate images by generator, classify real/fake images with discriminator
if
self
.
y_dim
:
# if conditional GAN, includes label
self
.
G
=
self
.
generator
(
self
.
z
,
self
.
y
)
self
.
D_t
=
self
.
discriminator
(
self
.
images
)
# generated fake images
self
.
sampled
=
self
.
sampler
(
self
.
z
,
self
.
y
)
self
.
D_f
=
self
.
discriminator
(
self
.
images
)
self
.
G
=
self
.
generator
(
self
.
z
,
self
.
y
)
self
.
D_t
=
self
.
discriminator
(
self
.
images
)
# generated fake images
self
.
sampled
=
self
.
sampler
(
self
.
z
,
self
.
y
)
self
.
D_f
=
self
.
discriminator
(
self
.
G
)
else
:
# original version of GAN
self
.
G
=
self
.
generator
(
self
.
z
)
self
.
D_t
=
self
.
discriminator
(
self
.
images
)
# generate fake images
self
.
sampled
=
self
.
sampler
(
self
.
z
)
self
.
D_f
=
self
.
discriminator
(
self
.
images
)
self
.
G
=
self
.
generator
(
self
.
z
)
self
.
D_t
=
self
.
discriminator
(
self
.
images
)
# generate fake images
self
.
sampled
=
self
.
sampler
(
self
.
z
)
self
.
D_f
=
self
.
discriminator
(
self
.
images
)
# step 2: define the two losses
self
.
d_loss_real
=
pd
.
reduce_mean
(
pd
.
cross_entropy
(
self
.
D_t
,
np
.
ones
(
self
.
batch_size
))
...
...
@@ -171,6 +171,44 @@ class DCGAN(object):
self
.
g_loss
=
pd
.
reduce_mean
(
pd
.
cross_entropy
(
self
.
D_f
,
np
.
ones
(
self
.
batch_szie
))
```
If we do not have dependency engine but blocks, the module building our GAN model will be like this:
```
python
class
DCGAN
(
object
):
def
build_model
(
self
,
default_block
):
# input data in the default block
if
self
.
y_dim
:
self
.
y
=
pd
.
data
(
pd
.
float32
,
[
self
.
batch_size
,
self
.
y_dim
])
self
.
images
=
pd
.
data
(
pd
.
float32
,
[
self
.
batch_size
,
self
.
im_size
,
self
.
im_size
])
# self.faked_images = pd.data(pd.float32, [self.batch_size, self.im_size, self.im_size])
self
.
z
=
pd
.
data
(
tf
.
float32
,
[
None
,
self
.
z_size
])
# step 1: generate images by generator, classify real/fake images with discriminator
with
pd
.
default_block
().
g_block
():
if
self
.
y_dim
:
# if conditional GAN, includes label
self
.
G
=
self
.
generator
(
self
.
z
,
self
.
y
)
self
.
D_g
=
self
.
discriminator
(
self
.
G
,
self
.
y
)
else
:
# original version of GAN
self
.
G
=
self
.
generator
(
self
.
z
)
self
.
D_g
=
self
.
discriminator
(
self
.
G
,
self
.
y
)
self
.
g_loss
=
pd
.
reduce_mean
(
pd
.
cross_entropy
(
self
.
D_g
,
np
.
ones
(
self
.
batch_szie
))
with
pd
.
default_block
().
d_block
():
if
self
.
y_dim
:
# if conditional GAN, includes label
self
.
D_t
=
self
.
discriminator
(
self
.
images
,
self
.
y
)
self
.
D_f
=
self
.
discriminator
(
self
.
G
,
self
.
y
)
else
:
# original version of GAN
self
.
D_t
=
self
.
discriminator
(
self
.
images
)
self
.
D_f
=
self
.
discriminator
(
self
.
G
)
# step 2: define the two losses
self
.
d_loss_real
=
pd
.
reduce_mean
(
pd
.
cross_entropy
(
self
.
D_t
,
np
.
ones
(
self
.
batch_size
))
self
.
d_loss_fake
=
pd
.
reduce_mean
(
pd
.
cross_entropy
(
self
.
D_f
,
np
.
zeros
(
self
.
batch_size
))
self
.
d_loss
=
self
.
d_loss_real
+
self
.
d_loss_fake
```
Some small confusion and problems with this design:
-
D
\_
g and D
\_
f are actually the same thing, but has to be written twice;
-
Requires ability to create a block anytime, rather than in if-else or rnn only;
## Main function for the demo:
Generally, the user of GAN just need to the following things:
-
Define an object as DCGAN class;
...
...
@@ -183,9 +221,10 @@ import numpy as np
import
logging
if
__name__
==
"__main__"
:
# dcgan
dcgan
=
DCGAN
()
dcgan
.
build_model
()
# dcgan class in the default graph/block
with
pd
.
block
()
as
def_block
:
dcgan
=
DCGAN
()
dcgan
.
build_model
(
def_block
)
# load mnist data
data_X
,
data_y
=
self
.
load_mnist
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录