Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
fb0d80d5
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看板
提交
fb0d80d5
编写于
11月 10, 2016
作者:
W
wangyang59
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add sample noise to demo/gan
上级
0df8af99
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
32 addition
and
17 deletion
+32
-17
demo/gan/gan_conf_image.py
demo/gan/gan_conf_image.py
+6
-1
demo/gan/gan_trainer_image.py
demo/gan/gan_trainer_image.py
+26
-16
未找到文件。
demo/gan/gan_conf_image.py
浏览文件 @
fb0d80d5
...
...
@@ -232,8 +232,13 @@ if is_discriminator_training:
sample
=
data_layer
(
name
=
"sample"
,
size
=
sample_dim
*
sample_dim
*
c_dim
)
if
is_generator_training
or
is_discriminator_training
:
sample_noise
=
data_layer
(
name
=
"sample_noise"
,
size
=
sample_dim
*
sample_dim
*
c_dim
)
label
=
data_layer
(
name
=
"label"
,
size
=
1
)
prob
=
discriminator
(
sample
)
prob
=
discriminator
(
addto_layer
([
sample
,
sample_noise
],
act
=
LinearActivation
(),
name
=
"add"
,
bias_attr
=
False
))
cost
=
cross_entropy
(
input
=
prob
,
label
=
label
)
classification_error_evaluator
(
input
=
prob
,
label
=
label
,
name
=
mode
+
'_error'
)
outputs
(
cost
)
...
...
demo/gan/gan_trainer_image.py
浏览文件 @
fb0d80d5
...
...
@@ -115,9 +115,13 @@ def get_real_samples(batch_size, data_np):
def
get_noise
(
batch_size
,
noise_dim
):
return
numpy
.
random
.
normal
(
size
=
(
batch_size
,
noise_dim
)).
astype
(
'float32'
)
def
get_sample_noise
(
batch_size
):
return
numpy
.
random
.
normal
(
size
=
(
batch_size
,
28
*
28
),
scale
=
0.1
).
astype
(
'float32'
)
def
get_fake_samples
(
generator_machine
,
batch_size
,
noise
):
gen_inputs
=
prepare_generator_data_batch
(
batch_size
,
noise
)
gen_inputs
.
resize
(
1
)
gen_inputs
=
api
.
Arguments
.
createArguments
(
1
)
gen_inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
createGpuDenseFromNumpy
(
noise
)
)
gen_outputs
=
api
.
Arguments
.
createArguments
(
0
)
generator_machine
.
forward
(
gen_inputs
,
gen_outputs
,
api
.
PASS_TEST
)
fake_samples
=
gen_outputs
.
getSlotValue
(
0
).
copyToNumpyMat
()
...
...
@@ -129,29 +133,33 @@ def get_training_loss(training_machine, inputs):
loss
=
outputs
.
getSlotValue
(
0
).
copyToNumpyMat
()
return
numpy
.
mean
(
loss
)
def
prepare_discriminator_data_batch_pos
(
batch_size
,
data_np
):
def
prepare_discriminator_data_batch_pos
(
batch_size
,
data_np
,
sample_noise
):
real_samples
=
get_real_samples
(
batch_size
,
data_np
)
labels
=
numpy
.
ones
(
batch_size
,
dtype
=
'int32'
)
inputs
=
api
.
Arguments
.
createArguments
(
2
)
inputs
=
api
.
Arguments
.
createArguments
(
3
)
inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
createGpuDenseFromNumpy
(
real_samples
))
inputs
.
setSlotIds
(
1
,
api
.
IVector
.
createGpuVectorFromNumpy
(
labels
))
inputs
.
setSlotValue
(
1
,
api
.
Matrix
.
createGpuDenseFromNumpy
(
sample_noise
))
inputs
.
setSlotIds
(
2
,
api
.
IVector
.
createGpuVectorFromNumpy
(
labels
))
return
inputs
def
prepare_discriminator_data_batch_neg
(
generator_machine
,
batch_size
,
noise
):
def
prepare_discriminator_data_batch_neg
(
generator_machine
,
batch_size
,
noise
,
sample_noise
):
fake_samples
=
get_fake_samples
(
generator_machine
,
batch_size
,
noise
)
#print fake_samples.shape
labels
=
numpy
.
zeros
(
batch_size
,
dtype
=
'int32'
)
inputs
=
api
.
Arguments
.
createArguments
(
2
)
inputs
=
api
.
Arguments
.
createArguments
(
3
)
inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
createGpuDenseFromNumpy
(
fake_samples
))
inputs
.
setSlotIds
(
1
,
api
.
IVector
.
createGpuVectorFromNumpy
(
labels
))
inputs
.
setSlotValue
(
1
,
api
.
Matrix
.
createGpuDenseFromNumpy
(
sample_noise
))
inputs
.
setSlotIds
(
2
,
api
.
IVector
.
createGpuVectorFromNumpy
(
labels
))
return
inputs
def
prepare_generator_data_batch
(
batch_size
,
noise
):
def
prepare_generator_data_batch
(
batch_size
,
noise
,
sample_noise
):
label
=
numpy
.
ones
(
batch_size
,
dtype
=
'int32'
)
#label = numpy.zeros(batch_size, dtype='int32')
inputs
=
api
.
Arguments
.
createArguments
(
2
)
inputs
=
api
.
Arguments
.
createArguments
(
3
)
inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
createGpuDenseFromNumpy
(
noise
))
inputs
.
setSlotIds
(
1
,
api
.
IVector
.
createGpuVectorFromNumpy
(
label
))
inputs
.
setSlotValue
(
1
,
api
.
Matrix
.
createGpuDenseFromNumpy
(
sample_noise
))
inputs
.
setSlotIds
(
2
,
api
.
IVector
.
createGpuVectorFromNumpy
(
label
))
return
inputs
...
...
@@ -216,25 +224,27 @@ def main():
# generator_machine, batch_size, noise_dim, sample_dim)
# dis_loss = get_training_loss(dis_training_machine, data_batch_dis)
noise
=
get_noise
(
batch_size
,
noise_dim
)
sample_noise
=
get_sample_noise
(
batch_size
)
data_batch_dis_pos
=
prepare_discriminator_data_batch_pos
(
batch_size
,
data_np
)
batch_size
,
data_np
,
sample_noise
)
dis_loss_pos
=
get_training_loss
(
dis_training_machine
,
data_batch_dis_pos
)
sample_noise
=
get_sample_noise
(
batch_size
)
data_batch_dis_neg
=
prepare_discriminator_data_batch_neg
(
generator_machine
,
batch_size
,
noise
)
generator_machine
,
batch_size
,
noise
,
sample_noise
)
dis_loss_neg
=
get_training_loss
(
dis_training_machine
,
data_batch_dis_neg
)
dis_loss
=
(
dis_loss_pos
+
dis_loss_neg
)
/
2.0
data_batch_gen
=
prepare_generator_data_batch
(
batch_size
,
noise
)
batch_size
,
noise
,
sample_noise
)
gen_loss
=
get_training_loss
(
gen_training_machine
,
data_batch_gen
)
if
i
%
100
==
0
:
print
"d_pos_loss is %s d_neg_loss is %s"
%
(
dis_loss_pos
,
dis_loss_neg
)
print
"d_loss is %s g_loss is %s"
%
(
dis_loss
,
gen_loss
)
if
(
not
(
curr_train
==
"dis"
and
curr_strike
==
MAX_strike
))
and
((
curr_train
==
"gen"
and
curr_strike
==
MAX_strike
)
or
dis_loss
>
gen_loss
):
if
(
not
(
curr_train
==
"dis"
and
curr_strike
==
MAX_strike
))
and
((
curr_train
==
"gen"
and
curr_strike
==
MAX_strike
)
or
dis_loss
_neg
>
gen_loss
):
if
curr_train
==
"dis"
:
curr_strike
+=
1
else
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录