我想用 Tensorflow 开发一个 GAN,生成器是自动编码器,鉴别器是具有二进制输出的卷积神经网络。开发自动编码器和 CNN 没有问题,但我的想法是为每个组件(判别器和生成器)训练 1 个 epoch,并重复此循环 1000 个 epoch,保持上一个训练 epoch 的结果(权重)为下一个。我该如何操作呢?
问问题
1413 次
2 回答
2
如果您有两个称为train_step_generator
and的操作train_step_discriminator
(例如,每个操作都tf.train.AdamOptimizer().minimize(loss)
具有适当损失的形式),那么您的训练循环应该类似于以下结构:
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(1000):
if epoch%2 == 0: # train discriminator on even epochs
for i in range(training_set_size/batch_size):
z_ = np.random.normal(0,1,batch_size) # this is the input to the generator
batch = get_next_batch(batch_size)
sess.run(train_step_discriminator,feed_dict={z:z_, x:batch})
else: # train generator on odd epochs
for i in range(training_set_size/batch_size):
z_ = np.random.normal(0,1,batch_size) # this is the input to the generator
sess.run(train_step_generator,feed_dict={z:z_})
权重将在迭代之间保持不变。
于 2017-12-26T01:35:15.403 回答
0
我解决了这个问题。事实上,我希望自动编码器的输出成为 CNN 的输入,连接 GAN 并以 1:1 的比例更新权重。我注意到我必须特别注意区分生成器和鉴别器的损失,否则在第二个循环开始时,生成器的张量损失将被浮点数替换,这是鉴别器生成的最后一个损失。
这是代码:
with tf.Session() as sess:
sess.run(init)
for i in range(1, num_steps+1):
这里是发电机培训
batch_x, batch_y=next_batch(batch_size, x_train_noisy, x_train)
_, l = sess.run([optimizer, loss], feed_dict={X: batch_x.reshape(n,784),
Y:batch_y})
if i % display_step == 0 or i == 1:
print('Epoch %i: Denoising Loss: %f' % (i, l))
这里生成器的输出将用作判别器的输入
output=sess.run([decoder_op],feed_dict={X: x_train})
x_train2=np.array(output).reshape(n,784).astype(np.float64)
这里是鉴别器训练
batch_x2, batch_y2 = next_batch(batch_size, x_train2, y_train)
sess.run(train_op, feed_dict={X2: batch_x2.reshape(n,784), Y2: batch_y2, keep_prob: 0.8})
if i % display_step == 0 or i == 1:
loss3, acc = sess.run([loss_op2, accuracy], feed_dict={X2: batch_x2,
Y2: batch_y2,
keep_prob: 1.0})
print("Epoch " + str(i) + ", CNN Loss= " + \
"{:.4f}".format(loss3) + ", Training Accuracy= " + "{:.3f}".format(acc))
这样,异步更新可以按 1:1、1:5、5:1(鉴别器:生成器)或任何其他方式进行操作
于 2017-12-26T11:39:09.473 回答