未验证 提交 d1e78e57 编写于 作者: W whs 提交者: GitHub

Change the loss of c_gan and dc_gan. (#1283)

* Change the loss of c_gan and dc_gan.

* Remove unused comment.
上级 ff60ed86
...@@ -40,7 +40,9 @@ add_arg('use_gpu', bool, True, "Whether to use GPU to train.") ...@@ -40,7 +40,9 @@ add_arg('use_gpu', bool, True, "Whether to use GPU to train.")
def loss(x, label): def loss(x, label):
return fluid.layers.mean(x * (label - 0.5)) return fluid.layers.mean(
fluid.layers.sigmoid_cross_entropy_with_logits(
x=x, label=label))
def train(args): def train(args):
...@@ -67,7 +69,10 @@ def train(args): ...@@ -67,7 +69,10 @@ def train(args):
g_program_test = dg_program.clone(for_test=True) g_program_test = dg_program.clone(for_test=True)
dg_logit = D_cond(g_img, conditions) dg_logit = D_cond(g_img, conditions)
dg_loss = loss(dg_logit, 1) dg_loss = loss(
dg_logit,
fluid.layers.fill_constant_batch_size_like(
input=noise, dtype='float32', shape=[-1, 1], value=1.0))
opt = fluid.optimizer.Adam(learning_rate=LEARNING_RATE) opt = fluid.optimizer.Adam(learning_rate=LEARNING_RATE)
......
...@@ -32,15 +32,17 @@ LEARNING_RATE = 2e-4 ...@@ -32,15 +32,17 @@ LEARNING_RATE = 2e-4
parser = argparse.ArgumentParser(description=__doc__) parser = argparse.ArgumentParser(description=__doc__)
add_arg = functools.partial(add_arguments, argparser=parser) add_arg = functools.partial(add_arguments, argparser=parser)
# yapf: disable # yapf: disable
add_arg('batch_size', int, 121, "Minibatch size.") add_arg('batch_size', int, 128, "Minibatch size.")
add_arg('epoch', int, 20, "The number of epoched to be trained.") add_arg('epoch', int, 20, "The number of epoched to be trained.")
add_arg('output', str, "./output", "The directory the model and the test result to be saved to.") add_arg('output', str, "./output_dcgan", "The directory the model and the test result to be saved to.")
add_arg('use_gpu', bool, True, "Whether to use GPU to train.") add_arg('use_gpu', bool, True, "Whether to use GPU to train.")
# yapf: enable # yapf: enable
def loss(x, label): def loss(x, label):
return fluid.layers.mean(x * (label - 0.5)) return fluid.layers.mean(
fluid.layers.sigmoid_cross_entropy_with_logits(
x=x, label=label))
def train(args): def train(args):
...@@ -63,7 +65,10 @@ def train(args): ...@@ -63,7 +65,10 @@ def train(args):
g_program_test = dg_program.clone(for_test=True) g_program_test = dg_program.clone(for_test=True)
dg_logit = D(g_img) dg_logit = D(g_img)
dg_loss = loss(dg_logit, 1) dg_loss = loss(
dg_logit,
fluid.layers.fill_constant_batch_size_like(
input=noise, dtype='float32', shape=[-1, 1], value=1.0))
opt = fluid.optimizer.Adam(learning_rate=LEARNING_RATE) opt = fluid.optimizer.Adam(learning_rate=LEARNING_RATE)
...@@ -139,9 +144,9 @@ def train(args): ...@@ -139,9 +144,9 @@ def train(args):
fetch_list={g_img})[0] fetch_list={g_img})[0]
total_images = np.concatenate([real_image, generated_images]) total_images = np.concatenate([real_image, generated_images])
fig = plot(total_images) fig = plot(total_images)
msg = "Epoch ID={0}\n Batch ID={1}\n D-Loss={2}\n DG-Loss={3}\n gen={4}".format( msg = "Epoch ID={0} Batch ID={1} D-Loss={2} DG-Loss={3}\n gen={4}".format(
pass_id, batch_id, d_loss_np, dg_loss_np, pass_id, batch_id,
check(generated_images)) np.sum(d_loss_np), dg_loss_np, check(generated_images))
print(msg) print(msg)
plt.title(msg) plt.title(msg)
plt.savefig( plt.savefig(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册