受this article的启发,我正在尝试构建一个条件GAN,它将使用LSTM生成MNIST号码。我希望我使用与图像波纹相同的架构(鉴别器中的双向RNN,取自this paper):
当我运行这个模型时,我得到了非常奇怪的结果。此图显示了我的模型在每个纪元后生成3号。它看起来应该更像this。这真的很糟糕。
我的鉴别器网络的丢失真的快速下降到接近零。但是,我的生成器网络的丢失会在某个固定点附近振荡(可能会缓慢发散)。我真的不知道发生了什么。这是我的代码中最重要的部分(完整代码here):
timesteps = 28
X_dim = 28
Z_dim = 100
y_dim = 10
X = tf.placeholder(tf.float32, [None, timesteps, X_dim]) # reshaped MNIST image to 28x28
y = tf.placeholder(tf.float32, [None, y_dim]) # one-hot label
Z = tf.placeholder(tf.float32, [None, timesteps, Z_dim]) # numpy.random.uniform noise in range [-1; 1]
y_timesteps = tf.tile(tf.expand_dims(y, axis=1), [1, timesteps, 1]) # [None, timesteps, y_dim] - replicate y along axis=1
def discriminator(x, y):
with tf.variable_scope('discriminator', reuse=tf.AUTO_REUSE) as vs:
inputs = tf.concat([x, y], axis=2)
D_cell = tf.contrib.rnn.LSTMCell(64)
output, _ = tf.nn.dynamic_rnn(D_cell, inputs, dtype=tf.float32)
last_output = output[:, -1, :]
logit = tf.contrib.layers.fully_connected(last_output, 1, activation_fn=None)
pred = tf.nn.sigmoid(logit)
variables = [v for v in tf.all_variables() if v.name.startswith(vs.name)]
return variables, pred, logit
def generator(z, y):
with tf.variable_scope('generator', reuse=tf.AUTO_REUSE) as vs:
inputs = tf.concat([z, y], axis=2)
G_cell = tf.contrib.rnn.LSTMCell(64)
output, _ = tf.nn.dynamic_rnn(G_cell, inputs, dtype=tf.float32)
logit = tf.contrib.layers.fully_connected(output, X_dim, activation_fn=None)
pred = tf.nn.sigmoid(logit)
variables = [v for v in tf.all_variables() if v.name.startswith(vs.name)]
return variables, pred
G_vars, G_sample = run_generator(Z, y_timesteps)
D_vars, D_real, D_logit_real = run_discriminator(X, y_timesteps)
_, D_fake, D_logit_fake = run_discriminator(G_sample, y_timesteps)
D_loss = -tf.reduce_mean(tf.log(D_real) + tf.log(1. - D_fake))
G_loss = -tf.reduce_mean(tf.log(D_fake))
D_solver = tf.train.AdamOptimizer().minimize(D_loss, var_list=D_vars)
G_solver = tf.train.AdamOptimizer().minimize(G_loss, var_list=G_vars)
我的模型很可能出现问题。任何人都可以帮助我让发电机网络融合?
答案 0 :(得分:0)
您可以采取一些措施来改善网络架构和培训阶段。
tf.nn.sigmoid(logit)
。只返回pred
。使用数值稳定函数计算损失函数并修复损失函数:
D_loss = -tf.reduce_mean(tf.log(D_real)+ tf.log(1。 - D_fake)) G_loss = -tf.reduce_mean(tf.log(D_fake))
应该是:
D_loss_real = tf.nn.sigmoid_cross_entropy_with_logits(
logits=D_real,
labels=tf.ones_like(D_real))
D_loss_fake = tf.nn.sigmoid_cross_entropy_with_logits(
logits=D_fake,
labels=tf.zeros_like(D_fake))
D_loss = -tf.reduce_mean(D_loss_real + D_loss_fake)
G_loss = -tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(
logits=D_real,
labels=tf.ones_like(D_real)))
一旦你修复了损失并使用了数值稳定的函数,事情会变得更好。此外,根据经验,如果损失中的噪声太大,则降低学习速率(训练GAN时,ADAM的默认lr通常太高)。 希望它有所帮助