2017-11-16 1 views
0

最近私はGANネットワークを研究しています。私はそれをmnisitイメージを生成するために使っています。私のコンピューターの環境はubuntu16.04、tensorflow、python3です。GANからジェネレーターへnnistを使うが、何も勉強しない

コードはエラーなしで実行できますが、結果はネットワーク学習で何も表示されず、トレーニングによって出力画像がノイズの多い画像になります。

私は、入力が784次元のノイズの多いデータであり、隠れたレイヤーを介してそれをルール化し、784次元の画像を生成します。

私は弁別者ネットワークを設計します:入力は実像と偽の画像で、隠れた層を通してそれを支配し、出力は1次元のログです。

次に、generator_lossとdiscriminator_lossを定義して、列車発電機とディスクリミネータを定義しました。実行することはできますが、結果はネットワーク調査で何も表示されず、損失は収束できません。

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 
+0

あなたは本当に5秒余分に空白や改行を加えたり、文章間の句読点を間違えないようにread.doを書くのが面倒だと書いてありますが、これは痛いです。 – Julien

+0

これは私の最初です私はそれを訂正するためにあなたに感謝のために、良いと私の英語を質問するためにstackoverflowを使用する時間。 – zyw

答えて

0

私が変更を加えたコメントと修正されたコードを追加しています。さらに、私は私の変更について以下で説明しました。 GANを実装するときに注意する必要があり

いくつかのポイント、

  1. が弁別損失を実装する場合には(弁別の同じコピー(すなわち共有同じ 重み)を使用する必要が

    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') 
    
    あなたのケースDl0Dl1は同じパラメータを共有する必要があります)。
  2. をトレーニングするときジェネレータの出力のみ
  3. (その画像以降)0 と1の間で変化しなければならないので、発電活性化機能がないTANH シグモイドなければならない

  4. 弁別器の場合は、弁別器に関連付けられている変数のみを訓練する必要があります。同様に、ジェネレーターをトレーニングする場合は、ジェネレーターに関連する変数をトレーニングするだけです。

  5. 時には弁別はそうでないとして、それは正確に 生成と実際の間を区別することができることを学ぶために 十分な容量を持っていないだろう、 発電よりも強力であることを確認することが重要ですサンプル。

これらはあなたが注意すべきGAN Sの唯一の基本的なものです。しかし、GANを開発する際に考慮すべき他の多くの側面があります。 GANの良い基本的なアイデアを得るには、以下の2つの記事を読んでください。

  1. http://blog.aylien.com/introduction-generative-adversarial-networks-code-tensorflow/
  2. http://blog.evjang.com/2016/06/generative-adversarial-nets-in.html

この情報がお役に立てば幸いです。

+0

ありがとう!私はあなたの助言に従って私のネットワークを変更し、それは動作します、4点は非常に私のために、あなたの答えのおかげで非常に便利です。私は2つの記事を読むでしょう。 – zyw

関連する問題