Python 使用GAN生成mnist,但不进行任何研究

Python 使用GAN生成mnist,但不进行任何研究,python,tensorflow,deep-learning,Python,Tensorflow,Deep Learning,最近,我在研究GAN网络,我用它来生成一个mnisit图像,我的计算机环境是ubuntu16.04,tensorflow,python3 代码可以正常运行,但结果表明网络没有学习任何东西,通过训练,输出的图像仍然是有噪声的图像 首先设计了一个生成网络:输入784维的噪声数据,通过一个隐藏层对其进行规则化,生成784维的图像 然后设计了一个鉴别器网络:输入是真图像和假图像,通过一个隐藏层对其进行规则化,输出是一维的logits 然后定义了发生器损耗和鉴别器损耗,然后对发生器和鉴别器进行训练,可以运

最近,我在研究GAN网络,我用它来生成一个mnisit图像,我的计算机环境是ubuntu16.04,tensorflow,python3

代码可以正常运行,但结果表明网络没有学习任何东西,通过训练,输出的图像仍然是有噪声的图像

首先设计了一个生成网络:输入784维的噪声数据,通过一个隐藏层对其进行规则化,生成784维的图像

然后设计了一个鉴别器网络:输入是真图像和假图像,通过一个隐藏层对其进行规则化,输出是一维的logits

然后定义了发生器损耗和鉴别器损耗,然后对发生器和鉴别器进行训练,可以运行,但结果表明网络没有研究,损耗不能收敛

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

mnist = input_data.read_data_sets("/home/zyw/data/tensor_mnist-master/MNIST_data/",one_hot=True)
batch_size = 100

G_in = tf.placeholder(tf.float32,[None,784])
G_h1 = tf.layers.dense(G_in, 128)
G_h1 = tf.maximum(0.01 * G_h1, G_h1)
G_out = tf.tanh(tf.layers.dense(G_h1, 784)) 

real = tf.placeholder(tf.float32,[None,784]) 
Dl0 = tf.layers.dense(G_out, 128)
Dl0 = tf.maximum(0.01 * Dl0, Dl0)
p0 = tf.layers.dense(Dl0, 1)

Dl1 = tf.layers.dense(real, 128)
Dl1 = tf.maximum(0.01 * Dl1, Dl1)
p1 = tf.layers.dense(Dl1, 1)

G_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits =p0,labels=tf.ones_like(p0)*0.9))
D_real_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits =p1,labels=tf.ones_like(p1)*0.9))
D_fake_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits =p0,labels=tf.zeros_like(p0)))
D_total_loss = tf.add(D_fake_loss,D_real_loss) 

G_train = tf.train.AdamOptimizer(0.01).minimize(G_loss)
D_train = tf.train.AdamOptimizer(0.01).minimize(D_total_loss)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)

    for i in range(1000):
        mnist_data,_ = mnist.train.next_batch(batch_size)

        # noise_org = tf.random_normal([batch_size,784],stddev = 0.1,dtype = tf.float32)
        noise_org = np.random.randn(batch_size, 784)
        a,b,dloss=  sess.run([D_real_loss,D_fake_loss,D_total_loss,G_train,D_train],feed_dict={G_in:noise_org,real:mnist_data})[:3]
        if i%100==0:
            print(a,b,dloss)
    #test_generative_image
    noise_org = np.random.randn(1, 784)
    image = sess.run(G_out,feed_dict ={G_in:noise_org})
    outimage = tf.reshape(image, [28,28])
    plt.imshow(outimage.eval(),cmap='gray')
    plt.show()
    print('ok')
结果是:

0.80509 0.63548 1.44057
0.33512 0.20223 0.53735
0.332536 0.97737 1.30991
0.328048 0.814452 1.1425
0.326688 0.411907 0.738596
0.325864 0.570807 0.896671
0.325575 0.970406 1.29598
0.325421 1.02487 1.35029
0.325222 1.34089 1.66612
0.325217 0.747129 1.07235

我已经添加了修改后的代码,并在其中添加了注释。此外,我在下面描述了我的变化

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

mnist = input_data.read_data_sets("/home/zyw/data/tensor_mnist-master/MNIST_data/",one_hot=True)

batch_size = 100

#define the generator function
def generator(input):
    G_h1 = tf.layers.dense(input, 128)
    # G_h1 = tf.maximum(0.01 * G_h1, G_h1)
    G_out = tf.sigmoid(tf.layers.dense(G_h1, 784))  # sigmoid function added
    return G_out

#Define the discrminator function
def discriminator(input):
    Dl0 = tf.layers.dense(input, 128)
    # Dl0 = tf.maximum(0.01 * Dl0, Dl0)
    p0 = tf.layers.dense(Dl0, 1)
    return p0

#Generator
with tf.variable_scope('G'):
    G_in = tf.placeholder(tf.float32, [None, 784])
    G_out = generator(G_in)

real = tf.placeholder(tf.float32, [None, 784])

#Discrimnator that takes the real data
with tf.variable_scope('D'):
    D1 = discriminator(real)

#Discriminator that takes fake data
with tf.variable_scope('D', reuse=True):  # need to use the same copy of Discrminator
    D2 = discriminator(G_out)

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

vars = tf.trainable_variables() #all trainable variables
d_training_vars = [v for v in vars if v.name.startswith('D/')]  # varibles associated with the discrminator
g_training_vars = [v for v in vars if v.name.startswith('G/')]  # varibles associated with the generator

G_train = tf.train.AdamOptimizer(0.001).minimize(G_loss,var_list=g_training_vars)  # only train the variables associated with the generator
D_train = tf.train.AdamOptimizer(0.001).minimize(D_total_loss,var_list=d_training_vars)  # only train the variables associated with the discriminator

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)

    for i in range(1000):
        mnist_data, _ = mnist.train.next_batch(batch_size)
        # noise_org = tf.random_normal([batch_size,784],stddev = 0.1,dtype = tf.float32)
        noise_org = np.random.randn(batch_size, 784)

        a, b, dloss = sess.run([D_real_loss, D_fake_loss, D_total_loss, G_train, D_train],feed_dict={G_in: noise_org, real: mnist_data})[:3]
        if i % 100 == 0:
            print(a, b, dloss)

    # test_generative_image
    noise_org = np.random.randn(1, 784)
    image = sess.run(G_out, feed_dict={G_in: noise_org})
    outimage = tf.reshape(image, [28, 28])

    plt.imshow(outimage.eval(), cmap='gray')
    plt.show()
    print('ok')
在实现GAN时应注意的几点

需要使用相同的鉴别器副本,即共享相同的 在您的案例中实施鉴别器损耗时的权重Dl0 和Dl1应共享相同的参数。 发电机激活功能应为S形而非tanh形 因为发电机的输出应仅在0之间变化 和1。因为它是一个图像 训练鉴别器时,应只训练与鉴别器关联的变量。同样,在培训生成器时,只应培训与生成器关联的变量

有时,重要的是要确保鉴别器是正确的 比发电机更强大,否则,它不会 具备足够的学习能力,能够准确区分 在生成的样本和真实样本之间

这些只是你应该注意的GANs的基本内容。然而,在开发GaN时,还有许多其他方面需要考虑。通过阅读以下两篇文章,您可以对GANs有一个很好的基本了解


希望这有帮助。

请把你的问题说得更清楚。因为它写得太难看了。你真的没有5秒钟的时间来添加空格或新行,或者,纠正句子之间的标点符号吗,天哪,这很痛苦这是我第一次使用stackoverflow提问,我的英语不好,谢谢你的提醒,我会改正的,谢谢!根据你的建议,我改变了我的网络,这4点对我很有用,再次感谢你的回答。我将阅读这两篇文章。