GAN无法收敛。鉴别器损耗不断增加

Paramdeep Singh Obheroi

我在mnist数据集上制作了一个简单的生成逆向网络。

这是我的实现:

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)

def noise(batch_size):
    return np.random.uniform(-1, 1, (batch_size, 100))

learning_rate = 0.001
batch_size = 128

input = tf.placeholder('float', [None, 100])
real_data = tf.placeholder('float', [None, 784])

def generator(x):
    weights = {
        'hl1' : tf.Variable(tf.random_normal([100, 200])),
        'ol'  : tf.Variable(tf.random_normal([200, 784]))
    }
    biases = {
        'hl1' : tf.Variable(tf.random_normal([200])),
        'ol'  : tf.Variable(tf.random_normal([784]))
    }

    hl1 = tf.add(tf.matmul(x, weights['hl1']), biases['hl1'])
    ol = tf.nn.sigmoid(tf.add(tf.matmul(hl1, weights['ol']), biases['ol']))

    return ol


def discriminator(x):
    weights = {
        'hl1' : tf.Variable(tf.random_normal([784, 200])),
        'ol'  : tf.Variable(tf.random_normal([200, 1]))
    }
    biases = {
        'hl1' : tf.Variable(tf.random_normal([200])),
        'ol'  : tf.Variable(tf.random_normal([1]))
    }

    hl1 = tf.add(tf.matmul(x, weights['hl1']), biases['hl1'])
    ol = tf.nn.sigmoid(tf.add(tf.matmul(hl1, weights['ol']), biases['ol']))

    return ol

with tf.variable_scope("G"):
    G = generator(input)

with tf.variable_scope("D"):
    D_real = discriminator(real_data)

with tf.variable_scope("D", reuse = True):
    D_gen = discriminator(G)

generator_parameters = [x for x in tf.trainable_variables() if x.name.startswith('G/')]
discriminator_parameters = [x for x in tf.trainable_variables() if x.name.startswith('D/')]

G_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=D_gen, labels=tf.ones_like(D_gen)))
D_real_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=D_real, labels=tf.ones_like(D_real)))
D_fake_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=D_gen, labels=tf.zeros_like(D_gen)))
D_total_loss = tf.add(D_fake_loss, D_real_loss)

G_train = tf.train.AdamOptimizer(learning_rate).minimize(G_loss,var_list=generator_parameters)
D_train = tf.train.AdamOptimizer(learning_rate).minimize(D_total_loss,var_list=discriminator_parameters)

sess = tf.Session()
init = tf.global_variables_initializer()

sess.run(init)

loss_g_function = []
loss_d_function = []

for epoch in range(200):
    for iteratiion in range(int(len(mnist.train.images)/batch_size)):
        real_batch, _ = mnist.train.next_batch(batch_size)

        _, d_err = sess.run([D_train, D_total_loss], feed_dict = {real_data : real_batch, input : noise(batch_size)})
        _, g_err = sess.run([G_train, G_loss], feed_dict = {input : noise(batch_size)})

    print("Epoch = ", epoch)
    print("D_loss = ", d_err)
    print("G_loss = ", g_err)
    loss_g_function.append(g_err)
    loss_d_function.append(d_err)

# Visualizing
import matplotlib.pyplot as plt

test_noise = noise(1)

plt.subplot(2, 2, 1)
plt.plot(test_noise[0])
plt.title("Noise")
plt.subplot(2, 2, 2)
plt.imshow(np.reshape(sess.run(G, feed_dict = {input : test_noise})[0], [28, 28]))
plt.title("Generated Image")
plt.subplot(2, 2, 3)
plt.plot(loss_d_function, 'r')
plt.xlabel("Epochs")
plt.ylabel("Discriminator Loss")
plt.title("D-Loss")
plt.subplot(2, 2, 4)
plt.plot(loss_g_function, 'b')
plt.xlabel("Epochs")
plt.ylabel("Generator Loss")
plt.title("G_Loss")
plt.show()

我曾尝试lr = 0.001 lr = 0.0001lr = 0.00003

这些是我的结果:https : //imgur.com/a/6KUnO1H

可能是什么原因?我的权重初始化是从正态分布中随机抽取的。另外,请检查损失功能,是否正确?

维杰·米

问题:


它只有一层:

hl1 = tf.add(tf.matmul(x, weights['hl1']), biases['hl1'])    
ol = tf.nn.sigmoid(tf.add(tf.matmul(hl1, weights['ol']), biases['ol']))

上面为鉴别器和生成器定义的网络都没有为第一层定义激活。从字面上看,这意味着网络只是一层:y = act(w2(x*w1+b1)+b2) = act(x*w+b)


乙状结肠应用两次

ol = tf.nn.sigmoid(tf.add(tf.matmul(hl1, weights['ol']) ...
D_real_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(...)

如评论中所述,激活被应用两次。


重量初始化:

tf.Variable(tf.random_normal([784, 200]))

如果权重较大,则在进行S型激活时,梯度将较小,这意味着权重实际上不会改变值。(更大的w +非常小的delta(w))。可能是为什么当我运行上面的代码时,损失似乎没有太大变化的原因。最好采用行业最佳做法并使用类似以下内容:xavier_initializer()


动态范围不一致:的输入generator在[-1,1]的动态范围内,它乘以[-1,1]的权重,但输出到[0 1]范围。这没有错,一个偏见可以学习映射输出范围。但是最好使用一个激活层,该激活层像一样输出[-1,1]tanh,以便网络可以更快地学习。如果tanh将用作的激活generator,则输入图像将根据descriminator需要进行缩放,以[-1 1]保持训练的一致性。


通过上述更改,您可以获得类似于以下内容的信息:

在此处输入图片说明

上述网络是一个非常简单的网络,输出质量不是很好。我故意不改变复杂度,以找出可以从简单网络中获得什么样的输出。

您可以构建更大的网络(包括CNN),也可以尝试使用最新的GAN模型以获得更好的质量结果。


可从此处获取用于复制上述内容的代码

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章