Accompanying code examples of the book "Introduction to Artificial Neural Networks and Deep Learning: A Practical Guide with Applications in Python" by Sebastian Raschka. All code examples are released under the MIT license. If you find this content useful, please consider supporting the work by buying a copy of the book.

Other code examples and content are available on GitHub. The PDF and ebook versions of the book are available through Leanpub.


In [1]:
%load_ext watermark
%watermark -a 'Sebastian Raschka' -v -p tensorflow


Sebastian Raschka 

CPython 3.6.1
IPython 6.0.0

tensorflow 1.2.0

Model Zoo -- Convolutional General Adversarial Networks

Implementation of General Adversarial Nets (GAN) where both the discriminator and generator have convolutional and deconvolutional layers, respectively. In this example, the GAN generator was trained to generate MNIST images.

Uses

  • samples from a random normal distribution (range [-1, 1])
  • dropout
  • leaky relus
  • batch normalization
  • separate batches for "fake" and "real" images (where the labels are 1 = real images, 0 = fake images)
  • MNIST images normalized to [-1, 1] range
  • generator with tanh output

In [2]:
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import pickle as pkl

tf.test.gpu_device_name()


Out[2]:
'/gpu:0'

In [3]:
### Abbreviatiuons
# dis_*: discriminator network
# gen_*: generator network

########################
### Helper functions
########################

def leaky_relu(x, alpha=0.0001):
    return tf.maximum(alpha * x, x)


########################
### DATASET
########################

mnist = input_data.read_data_sets('MNIST_data')


#########################
### SETTINGS
#########################

# Hyperparameters
learning_rate = 0.001
training_epochs = 50
batch_size = 64
dropout_rate = 0.5

# Architecture
dis_input_size = 784
gen_input_size = 100

# Other settings
print_interval = 200

#########################
### GRAPH DEFINITION
#########################

g = tf.Graph()
with g.as_default():
    
    # Placeholders for settings
    dropout = tf.placeholder(tf.float32, shape=None, name='dropout')
    is_training = tf.placeholder(tf.bool, shape=None, name='is_training')
    
    # Input data
    dis_x = tf.placeholder(tf.float32, shape=[None, dis_input_size],
                           name='discriminator_inputs')     
    gen_x = tf.placeholder(tf.float32, [None, gen_input_size],
                           name='generator_inputs')


    ##################
    # Generator Model
    ##################

    with tf.variable_scope('generator'):
        
        # 100 => 784 => 7x7x64
        gen_fc = tf.layers.dense(inputs=gen_x, units=3136,
                                 bias_initializer=None, # no bias required when using batch_norm
                                 activation=None)
        gen_fc = tf.layers.batch_normalization(gen_fc, training=is_training)
        gen_fc = leaky_relu(gen_fc)
        gen_fc = tf.reshape(gen_fc, (-1, 7, 7, 64))
        
        # 7x7x64 => 14x14x32
        deconv1 = tf.layers.conv2d_transpose(gen_fc, filters=32, 
                                             kernel_size=(3, 3), strides=(2, 2), 
                                             padding='same',
                                             bias_initializer=None,
                                             activation=None)
        deconv1 = tf.layers.batch_normalization(deconv1, training=is_training)
        deconv1 = leaky_relu(deconv1)     
        deconv1 = tf.layers.dropout(deconv1, rate=dropout_rate)
        
        # 14x14x32 => 28x28x16
        deconv2 = tf.layers.conv2d_transpose(deconv1, filters=16, 
                                             kernel_size=(3, 3), strides=(2, 2), 
                                             padding='same',
                                             bias_initializer=None,
                                             activation=None)
        deconv2 = tf.layers.batch_normalization(deconv2, training=is_training)
        deconv2 = leaky_relu(deconv2)     
        deconv2 = tf.layers.dropout(deconv2, rate=dropout_rate)
        
        # 28x28x16 => 28x28x8
        deconv3 = tf.layers.conv2d_transpose(deconv2, filters=8, 
                                             kernel_size=(3, 3), strides=(1, 1), 
                                             padding='same',
                                             bias_initializer=None,
                                             activation=None)
        deconv3 = tf.layers.batch_normalization(deconv3, training=is_training)
        deconv3 = leaky_relu(deconv3)     
        deconv3 = tf.layers.dropout(deconv3, rate=dropout_rate)
        
        # 28x28x8 => 28x28x1
        gen_logits = tf.layers.conv2d_transpose(deconv3, filters=1, 
                                                kernel_size=(3, 3), strides=(1, 1), 
                                                padding='same',
                                                bias_initializer=None,
                                                activation=None)
        gen_out = tf.tanh(gen_logits, 'generator_outputs')


    ######################
    # Discriminator Model
    ######################
    
    def build_discriminator_graph(input_x, reuse=None):

        with tf.variable_scope('discriminator', reuse=reuse):
            
            # 28x28x1 => 14x14x8
            conv_input = tf.reshape(input_x, (-1, 28, 28, 1))
            conv1 = tf.layers.conv2d(conv_input, filters=8, kernel_size=(3, 3),
                                     strides=(2, 2), padding='same',
                                     bias_initializer=None,
                                     activation=None)
            conv1 = tf.layers.batch_normalization(conv1, training=is_training)
            conv1 = leaky_relu(conv1)
            conv1 = tf.layers.dropout(conv1, rate=dropout_rate)
            
            # 14x14x8 => 7x7x32
            conv2 = tf.layers.conv2d(conv1, filters=32, kernel_size=(3, 3),
                                     strides=(2, 2), padding='same',
                                     bias_initializer=None,
                                     activation=None)
            conv2 = tf.layers.batch_normalization(conv2, training=is_training)
            conv2 = leaky_relu(conv2)
            conv2 = tf.layers.dropout(conv2, rate=dropout_rate)

            # fully connected layer
            fc_input = tf.reshape(conv2, (-1, 7*7*32))
            logits = tf.layers.dense(inputs=fc_input, units=1, activation=None)
            out = tf.sigmoid(logits)
            
        return logits, out    

    # Create a discriminator for real data and a discriminator for fake data
    dis_real_logits, dis_real_out = build_discriminator_graph(dis_x, reuse=False)
    dis_fake_logits, dis_fake_out = build_discriminator_graph(gen_out, reuse=True)


    #####################################
    # Generator and Discriminator Losses
    #####################################
    
    # Two discriminator cost components: loss on real data + loss on fake data
    # Real data has class label 0, fake data has class label 1
    dis_real_loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=dis_real_logits, 
                                                            labels=tf.zeros_like(dis_real_logits))
    dis_fake_loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=dis_fake_logits, 
                                                            labels=tf.ones_like(dis_fake_logits))
    dis_cost = tf.add(tf.reduce_mean(dis_fake_loss), 
                      tf.reduce_mean(dis_real_loss), 
                      name='discriminator_cost')
 
    # Generator cost: difference between dis. prediction and label "0" for real images
    gen_loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=dis_fake_logits,
                                                       labels=tf.zeros_like(dis_fake_logits))
    gen_cost = tf.reduce_mean(gen_loss, name='generator_cost')
    
    
    #########################################
    # Generator and Discriminator Optimizers
    #########################################
      
    dis_optimizer = tf.train.AdamOptimizer(learning_rate)
    dis_train_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='discriminator')
    dis_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, scope='discriminator')
    
    with tf.control_dependencies(dis_update_ops): # required to upd. batch_norm params
        dis_train = dis_optimizer.minimize(dis_cost, var_list=dis_train_vars,
                                           name='train_discriminator')
    
    gen_optimizer = tf.train.AdamOptimizer(learning_rate)
    gen_train_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='generator')
    gen_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, scope='generator')
    
    with tf.control_dependencies(gen_update_ops): # required to upd. batch_norm params
        gen_train = gen_optimizer.minimize(gen_cost, var_list=gen_train_vars,
                                           name='train_generator')
    
    # Saver to save session for reuse
    saver = tf.train.Saver()


Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz

In [4]:
##########################
### TRAINING & EVALUATION
##########################

with tf.Session(graph=g) as sess:
    sess.run(tf.global_variables_initializer())
    
    avg_costs = {'discriminator': [], 'generator': []}

    for epoch in range(training_epochs):
        dis_avg_cost, gen_avg_cost = 0., 0.
        total_batch = mnist.train.num_examples // batch_size

        for i in range(total_batch):
            
            batch_x, batch_y = mnist.train.next_batch(batch_size)
            batch_x = batch_x*2 - 1 # normalize
            batch_randsample = np.random.uniform(-1, 1, size=(batch_size, gen_input_size))
            
            # Train
            
            _, dc = sess.run(['train_discriminator', 'discriminator_cost:0'],
                             feed_dict={'discriminator_inputs:0': batch_x, 
                                        'generator_inputs:0': batch_randsample,
                                        'dropout:0': dropout_rate,
                                        'is_training:0': True})
            
            _, gc = sess.run(['train_generator', 'generator_cost:0'],
                             feed_dict={'generator_inputs:0': batch_randsample,
                                        'dropout:0': dropout_rate,
                                        'is_training:0': True})
            
            dis_avg_cost += dc
            gen_avg_cost += gc

            if not i % print_interval:
                print("Minibatch: %04d | Dis/Gen Cost:    %.3f/%.3f" % (i + 1, dc, gc))
                

        print("Epoch:     %04d | Dis/Gen AvgCost: %.3f/%.3f" % 
              (epoch + 1, dis_avg_cost / total_batch, gen_avg_cost / total_batch))
        
        avg_costs['discriminator'].append(dis_avg_cost / total_batch)
        avg_costs['generator'].append(gen_avg_cost / total_batch)
    
    
    saver.save(sess, save_path='./gan-conv.ckpt')


Minibatch: 0001 | Dis/Gen Cost:    1.657/0.917
Minibatch: 0201 | Dis/Gen Cost:    0.776/1.551
Minibatch: 0401 | Dis/Gen Cost:    0.827/1.839
Minibatch: 0601 | Dis/Gen Cost:    0.438/2.190
Minibatch: 0801 | Dis/Gen Cost:    1.583/0.872
Epoch:     0001 | Dis/Gen AvgCost: 0.934/1.593
Minibatch: 0001 | Dis/Gen Cost:    0.634/1.577
Minibatch: 0201 | Dis/Gen Cost:    0.598/2.277
Minibatch: 0401 | Dis/Gen Cost:    0.763/1.207
Minibatch: 0601 | Dis/Gen Cost:    0.524/2.216
Minibatch: 0801 | Dis/Gen Cost:    0.252/2.839
Epoch:     0002 | Dis/Gen AvgCost: 0.789/1.668
Minibatch: 0001 | Dis/Gen Cost:    0.726/1.502
Minibatch: 0201 | Dis/Gen Cost:    0.634/1.563
Minibatch: 0401 | Dis/Gen Cost:    0.956/1.716
Minibatch: 0601 | Dis/Gen Cost:    0.882/1.410
Minibatch: 0801 | Dis/Gen Cost:    0.861/1.835
Epoch:     0003 | Dis/Gen AvgCost: 0.783/1.663
Minibatch: 0001 | Dis/Gen Cost:    0.732/1.914
Minibatch: 0201 | Dis/Gen Cost:    1.240/1.239
Minibatch: 0401 | Dis/Gen Cost:    1.047/1.460
Minibatch: 0601 | Dis/Gen Cost:    0.749/1.630
Minibatch: 0801 | Dis/Gen Cost:    0.883/1.536
Epoch:     0004 | Dis/Gen AvgCost: 0.880/1.611
Minibatch: 0001 | Dis/Gen Cost:    0.726/1.820
Minibatch: 0201 | Dis/Gen Cost:    1.143/1.496
Minibatch: 0401 | Dis/Gen Cost:    0.925/1.249
Minibatch: 0601 | Dis/Gen Cost:    0.839/1.300
Minibatch: 0801 | Dis/Gen Cost:    1.318/0.955
Epoch:     0005 | Dis/Gen AvgCost: 1.018/1.426
Minibatch: 0001 | Dis/Gen Cost:    1.047/1.319
Minibatch: 0201 | Dis/Gen Cost:    0.982/1.724
Minibatch: 0401 | Dis/Gen Cost:    1.723/0.943
Minibatch: 0601 | Dis/Gen Cost:    1.036/1.284
Minibatch: 0801 | Dis/Gen Cost:    1.543/0.896
Epoch:     0006 | Dis/Gen AvgCost: 1.175/1.231
Minibatch: 0001 | Dis/Gen Cost:    1.128/1.209
Minibatch: 0201 | Dis/Gen Cost:    1.045/1.154
Minibatch: 0401 | Dis/Gen Cost:    1.449/0.896
Minibatch: 0601 | Dis/Gen Cost:    1.116/1.281
Minibatch: 0801 | Dis/Gen Cost:    1.380/0.949
Epoch:     0007 | Dis/Gen AvgCost: 1.244/1.107
Minibatch: 0001 | Dis/Gen Cost:    1.292/0.929
Minibatch: 0201 | Dis/Gen Cost:    1.295/0.918
Minibatch: 0401 | Dis/Gen Cost:    1.271/0.998
Minibatch: 0601 | Dis/Gen Cost:    1.078/1.300
Minibatch: 0801 | Dis/Gen Cost:    1.371/1.022
Epoch:     0008 | Dis/Gen AvgCost: 1.261/1.044
Minibatch: 0001 | Dis/Gen Cost:    1.352/1.008
Minibatch: 0201 | Dis/Gen Cost:    1.763/0.743
Minibatch: 0401 | Dis/Gen Cost:    1.040/1.291
Minibatch: 0601 | Dis/Gen Cost:    1.334/1.050
Minibatch: 0801 | Dis/Gen Cost:    1.214/1.039
Epoch:     0009 | Dis/Gen AvgCost: 1.283/1.015
Minibatch: 0001 | Dis/Gen Cost:    1.628/0.699
Minibatch: 0201 | Dis/Gen Cost:    1.204/1.033
Minibatch: 0401 | Dis/Gen Cost:    1.393/0.891
Minibatch: 0601 | Dis/Gen Cost:    1.176/1.043
Minibatch: 0801 | Dis/Gen Cost:    1.493/0.765
Epoch:     0010 | Dis/Gen AvgCost: 1.297/0.980
Minibatch: 0001 | Dis/Gen Cost:    1.421/0.793
Minibatch: 0201 | Dis/Gen Cost:    1.453/0.898
Minibatch: 0401 | Dis/Gen Cost:    1.090/1.107
Minibatch: 0601 | Dis/Gen Cost:    1.412/0.927
Minibatch: 0801 | Dis/Gen Cost:    1.319/0.813
Epoch:     0011 | Dis/Gen AvgCost: 1.309/0.944
Minibatch: 0001 | Dis/Gen Cost:    1.347/1.046
Minibatch: 0201 | Dis/Gen Cost:    1.255/1.034
Minibatch: 0401 | Dis/Gen Cost:    1.205/0.926
Minibatch: 0601 | Dis/Gen Cost:    1.191/0.935
Minibatch: 0801 | Dis/Gen Cost:    1.450/0.762
Epoch:     0012 | Dis/Gen AvgCost: 1.305/0.943
Minibatch: 0001 | Dis/Gen Cost:    1.375/0.727
Minibatch: 0201 | Dis/Gen Cost:    1.361/1.070
Minibatch: 0401 | Dis/Gen Cost:    1.020/0.943
Minibatch: 0601 | Dis/Gen Cost:    1.250/0.921
Minibatch: 0801 | Dis/Gen Cost:    1.461/0.996
Epoch:     0013 | Dis/Gen AvgCost: 1.303/0.927
Minibatch: 0001 | Dis/Gen Cost:    1.258/0.765
Minibatch: 0201 | Dis/Gen Cost:    1.177/1.066
Minibatch: 0401 | Dis/Gen Cost:    1.175/0.970
Minibatch: 0601 | Dis/Gen Cost:    1.213/0.845
Minibatch: 0801 | Dis/Gen Cost:    1.433/0.846
Epoch:     0014 | Dis/Gen AvgCost: 1.305/0.920
Minibatch: 0001 | Dis/Gen Cost:    1.654/0.708
Minibatch: 0201 | Dis/Gen Cost:    1.437/0.770
Minibatch: 0401 | Dis/Gen Cost:    1.487/0.740
Minibatch: 0601 | Dis/Gen Cost:    1.167/1.100
Minibatch: 0801 | Dis/Gen Cost:    1.342/0.854
Epoch:     0015 | Dis/Gen AvgCost: 1.319/0.893
Minibatch: 0001 | Dis/Gen Cost:    1.356/0.826
Minibatch: 0201 | Dis/Gen Cost:    1.161/1.101
Minibatch: 0401 | Dis/Gen Cost:    1.355/0.878
Minibatch: 0601 | Dis/Gen Cost:    1.281/1.022
Minibatch: 0801 | Dis/Gen Cost:    1.198/0.828
Epoch:     0016 | Dis/Gen AvgCost: 1.320/0.887
Minibatch: 0001 | Dis/Gen Cost:    1.197/0.808
Minibatch: 0201 | Dis/Gen Cost:    1.337/0.922
Minibatch: 0401 | Dis/Gen Cost:    1.223/0.934
Minibatch: 0601 | Dis/Gen Cost:    1.376/0.734
Minibatch: 0801 | Dis/Gen Cost:    1.334/0.806
Epoch:     0017 | Dis/Gen AvgCost: 1.338/0.865
Minibatch: 0001 | Dis/Gen Cost:    1.352/0.790
Minibatch: 0201 | Dis/Gen Cost:    1.391/0.910
Minibatch: 0401 | Dis/Gen Cost:    1.329/0.776
Minibatch: 0601 | Dis/Gen Cost:    1.445/0.681
Minibatch: 0801 | Dis/Gen Cost:    1.301/0.840
Epoch:     0018 | Dis/Gen AvgCost: 1.335/0.842
Minibatch: 0001 | Dis/Gen Cost:    1.377/0.781
Minibatch: 0201 | Dis/Gen Cost:    1.277/0.872
Minibatch: 0401 | Dis/Gen Cost:    1.351/0.767
Minibatch: 0601 | Dis/Gen Cost:    1.501/0.657
Minibatch: 0801 | Dis/Gen Cost:    1.343/0.797
Epoch:     0019 | Dis/Gen AvgCost: 1.331/0.850
Minibatch: 0001 | Dis/Gen Cost:    1.429/0.756
Minibatch: 0201 | Dis/Gen Cost:    1.341/0.840
Minibatch: 0401 | Dis/Gen Cost:    1.447/0.768
Minibatch: 0601 | Dis/Gen Cost:    1.284/0.909
Minibatch: 0801 | Dis/Gen Cost:    1.212/1.033
Epoch:     0020 | Dis/Gen AvgCost: 1.342/0.843
Minibatch: 0001 | Dis/Gen Cost:    1.332/0.827
Minibatch: 0201 | Dis/Gen Cost:    1.570/0.884
Minibatch: 0401 | Dis/Gen Cost:    1.455/0.659
Minibatch: 0601 | Dis/Gen Cost:    1.275/0.705
Minibatch: 0801 | Dis/Gen Cost:    1.288/0.851
Epoch:     0021 | Dis/Gen AvgCost: 1.343/0.832
Minibatch: 0001 | Dis/Gen Cost:    1.233/0.942
Minibatch: 0201 | Dis/Gen Cost:    1.375/0.816
Minibatch: 0401 | Dis/Gen Cost:    1.256/0.852
Minibatch: 0601 | Dis/Gen Cost:    1.320/0.970
Minibatch: 0801 | Dis/Gen Cost:    1.159/1.066
Epoch:     0022 | Dis/Gen AvgCost: 1.349/0.834
Minibatch: 0001 | Dis/Gen Cost:    1.429/0.885
Minibatch: 0201 | Dis/Gen Cost:    1.643/0.703
Minibatch: 0401 | Dis/Gen Cost:    1.471/0.893
Minibatch: 0601 | Dis/Gen Cost:    1.407/0.775
Minibatch: 0801 | Dis/Gen Cost:    1.364/0.728
Epoch:     0023 | Dis/Gen AvgCost: 1.331/0.850
Minibatch: 0001 | Dis/Gen Cost:    1.492/0.734
Minibatch: 0201 | Dis/Gen Cost:    1.354/0.808
Minibatch: 0401 | Dis/Gen Cost:    1.280/0.938
Minibatch: 0601 | Dis/Gen Cost:    1.545/0.723
Minibatch: 0801 | Dis/Gen Cost:    1.326/0.814
Epoch:     0024 | Dis/Gen AvgCost: 1.355/0.818
Minibatch: 0001 | Dis/Gen Cost:    1.293/0.903
Minibatch: 0201 | Dis/Gen Cost:    1.456/0.688
Minibatch: 0401 | Dis/Gen Cost:    1.466/0.781
Minibatch: 0601 | Dis/Gen Cost:    1.157/0.831
Minibatch: 0801 | Dis/Gen Cost:    1.445/0.715
Epoch:     0025 | Dis/Gen AvgCost: 1.350/0.811
Minibatch: 0001 | Dis/Gen Cost:    1.500/0.735
Minibatch: 0201 | Dis/Gen Cost:    1.589/0.799
Minibatch: 0401 | Dis/Gen Cost:    1.429/0.675
Minibatch: 0601 | Dis/Gen Cost:    1.329/0.673
Minibatch: 0801 | Dis/Gen Cost:    1.318/0.856
Epoch:     0026 | Dis/Gen AvgCost: 1.348/0.808
Minibatch: 0001 | Dis/Gen Cost:    1.207/0.994
Minibatch: 0201 | Dis/Gen Cost:    1.404/0.758
Minibatch: 0401 | Dis/Gen Cost:    1.410/0.788
Minibatch: 0601 | Dis/Gen Cost:    1.284/0.861
Minibatch: 0801 | Dis/Gen Cost:    1.397/0.760
Epoch:     0027 | Dis/Gen AvgCost: 1.349/0.798
Minibatch: 0001 | Dis/Gen Cost:    1.277/0.772
Minibatch: 0201 | Dis/Gen Cost:    1.252/0.962
Minibatch: 0401 | Dis/Gen Cost:    1.340/0.709
Minibatch: 0601 | Dis/Gen Cost:    1.322/0.947
Minibatch: 0801 | Dis/Gen Cost:    1.389/0.839
Epoch:     0028 | Dis/Gen AvgCost: 1.347/0.807
Minibatch: 0001 | Dis/Gen Cost:    1.485/0.634
Minibatch: 0201 | Dis/Gen Cost:    1.213/0.865
Minibatch: 0401 | Dis/Gen Cost:    1.316/0.836
Minibatch: 0601 | Dis/Gen Cost:    1.405/0.751
Minibatch: 0801 | Dis/Gen Cost:    1.453/0.704
Epoch:     0029 | Dis/Gen AvgCost: 1.350/0.801
Minibatch: 0001 | Dis/Gen Cost:    1.294/0.776
Minibatch: 0201 | Dis/Gen Cost:    1.321/0.800
Minibatch: 0401 | Dis/Gen Cost:    1.447/0.693
Minibatch: 0601 | Dis/Gen Cost:    1.305/0.809
Minibatch: 0801 | Dis/Gen Cost:    1.502/0.622
Epoch:     0030 | Dis/Gen AvgCost: 1.355/0.787
Minibatch: 0001 | Dis/Gen Cost:    1.369/0.679
Minibatch: 0201 | Dis/Gen Cost:    1.406/0.774
Minibatch: 0401 | Dis/Gen Cost:    1.424/0.804
Minibatch: 0601 | Dis/Gen Cost:    1.410/0.703
Minibatch: 0801 | Dis/Gen Cost:    1.273/0.876
Epoch:     0031 | Dis/Gen AvgCost: 1.347/0.796
Minibatch: 0001 | Dis/Gen Cost:    1.426/0.701
Minibatch: 0201 | Dis/Gen Cost:    1.494/0.801
Minibatch: 0401 | Dis/Gen Cost:    1.317/0.771
Minibatch: 0601 | Dis/Gen Cost:    1.404/0.819
Minibatch: 0801 | Dis/Gen Cost:    1.413/0.766
Epoch:     0032 | Dis/Gen AvgCost: 1.357/0.792
Minibatch: 0001 | Dis/Gen Cost:    1.348/0.782
Minibatch: 0201 | Dis/Gen Cost:    1.336/0.759
Minibatch: 0401 | Dis/Gen Cost:    1.470/0.683
Minibatch: 0601 | Dis/Gen Cost:    1.445/0.734
Minibatch: 0801 | Dis/Gen Cost:    1.332/0.863
Epoch:     0033 | Dis/Gen AvgCost: 1.350/0.780
Minibatch: 0001 | Dis/Gen Cost:    1.379/0.783
Minibatch: 0201 | Dis/Gen Cost:    1.392/0.876
Minibatch: 0401 | Dis/Gen Cost:    1.365/0.777
Minibatch: 0601 | Dis/Gen Cost:    1.497/0.734
Minibatch: 0801 | Dis/Gen Cost:    1.337/0.767
Epoch:     0034 | Dis/Gen AvgCost: 1.354/0.780
Minibatch: 0001 | Dis/Gen Cost:    1.340/0.795
Minibatch: 0201 | Dis/Gen Cost:    1.214/0.849
Minibatch: 0401 | Dis/Gen Cost:    1.240/0.846
Minibatch: 0601 | Dis/Gen Cost:    1.367/0.731
Minibatch: 0801 | Dis/Gen Cost:    1.368/0.680
Epoch:     0035 | Dis/Gen AvgCost: 1.351/0.786
Minibatch: 0001 | Dis/Gen Cost:    1.221/0.897
Minibatch: 0201 | Dis/Gen Cost:    1.242/0.850
Minibatch: 0401 | Dis/Gen Cost:    1.291/0.792
Minibatch: 0601 | Dis/Gen Cost:    1.264/0.818
Minibatch: 0801 | Dis/Gen Cost:    1.418/0.774
Epoch:     0036 | Dis/Gen AvgCost: 1.350/0.781
Minibatch: 0001 | Dis/Gen Cost:    1.446/0.740
Minibatch: 0201 | Dis/Gen Cost:    1.264/0.814
Minibatch: 0401 | Dis/Gen Cost:    1.398/0.859
Minibatch: 0601 | Dis/Gen Cost:    1.261/0.833
Minibatch: 0801 | Dis/Gen Cost:    1.409/0.786
Epoch:     0037 | Dis/Gen AvgCost: 1.350/0.797
Minibatch: 0001 | Dis/Gen Cost:    1.375/0.775
Minibatch: 0201 | Dis/Gen Cost:    1.558/0.715
Minibatch: 0401 | Dis/Gen Cost:    1.334/0.807
Minibatch: 0601 | Dis/Gen Cost:    1.445/0.734
Minibatch: 0801 | Dis/Gen Cost:    1.247/0.898
Epoch:     0038 | Dis/Gen AvgCost: 1.355/0.782
Minibatch: 0001 | Dis/Gen Cost:    1.386/0.795
Minibatch: 0201 | Dis/Gen Cost:    1.294/0.812
Minibatch: 0401 | Dis/Gen Cost:    1.395/0.805
Minibatch: 0601 | Dis/Gen Cost:    1.371/0.738
Minibatch: 0801 | Dis/Gen Cost:    1.346/0.752
Epoch:     0039 | Dis/Gen AvgCost: 1.346/0.784
Minibatch: 0001 | Dis/Gen Cost:    1.313/0.776
Minibatch: 0201 | Dis/Gen Cost:    1.300/0.861
Minibatch: 0401 | Dis/Gen Cost:    1.459/0.692
Minibatch: 0601 | Dis/Gen Cost:    1.310/0.822
Minibatch: 0801 | Dis/Gen Cost:    1.410/0.757
Epoch:     0040 | Dis/Gen AvgCost: 1.351/0.783
Minibatch: 0001 | Dis/Gen Cost:    1.253/0.860
Minibatch: 0201 | Dis/Gen Cost:    1.398/0.677
Minibatch: 0401 | Dis/Gen Cost:    1.373/0.787
Minibatch: 0601 | Dis/Gen Cost:    1.318/0.818
Minibatch: 0801 | Dis/Gen Cost:    1.306/0.757
Epoch:     0041 | Dis/Gen AvgCost: 1.350/0.773
Minibatch: 0001 | Dis/Gen Cost:    1.272/0.820
Minibatch: 0201 | Dis/Gen Cost:    1.237/0.793
Minibatch: 0401 | Dis/Gen Cost:    1.443/0.742
Minibatch: 0601 | Dis/Gen Cost:    1.406/0.774
Minibatch: 0801 | Dis/Gen Cost:    1.325/0.766
Epoch:     0042 | Dis/Gen AvgCost: 1.352/0.775
Minibatch: 0001 | Dis/Gen Cost:    1.314/0.775
Minibatch: 0201 | Dis/Gen Cost:    1.328/0.833
Minibatch: 0401 | Dis/Gen Cost:    1.404/0.679
Minibatch: 0601 | Dis/Gen Cost:    1.304/0.806
Minibatch: 0801 | Dis/Gen Cost:    1.358/0.687
Epoch:     0043 | Dis/Gen AvgCost: 1.352/0.775
Minibatch: 0001 | Dis/Gen Cost:    1.467/0.737
Minibatch: 0201 | Dis/Gen Cost:    1.378/0.694
Minibatch: 0401 | Dis/Gen Cost:    1.370/0.798
Minibatch: 0601 | Dis/Gen Cost:    1.244/0.857
Minibatch: 0801 | Dis/Gen Cost:    1.349/0.827
Epoch:     0044 | Dis/Gen AvgCost: 1.358/0.767
Minibatch: 0001 | Dis/Gen Cost:    1.368/0.737
Minibatch: 0201 | Dis/Gen Cost:    1.345/0.766
Minibatch: 0401 | Dis/Gen Cost:    1.378/0.760
Minibatch: 0601 | Dis/Gen Cost:    1.301/0.797
Minibatch: 0801 | Dis/Gen Cost:    1.356/0.789
Epoch:     0045 | Dis/Gen AvgCost: 1.356/0.757
Minibatch: 0001 | Dis/Gen Cost:    1.400/0.711
Minibatch: 0201 | Dis/Gen Cost:    1.311/0.829
Minibatch: 0401 | Dis/Gen Cost:    1.452/0.648
Minibatch: 0601 | Dis/Gen Cost:    1.365/0.765
Minibatch: 0801 | Dis/Gen Cost:    1.397/0.820
Epoch:     0046 | Dis/Gen AvgCost: 1.354/0.758
Minibatch: 0001 | Dis/Gen Cost:    1.385/0.723
Minibatch: 0201 | Dis/Gen Cost:    1.313/0.778
Minibatch: 0401 | Dis/Gen Cost:    1.318/0.773
Minibatch: 0601 | Dis/Gen Cost:    1.384/0.756
Minibatch: 0801 | Dis/Gen Cost:    1.435/0.718
Epoch:     0047 | Dis/Gen AvgCost: 1.351/0.771
Minibatch: 0001 | Dis/Gen Cost:    1.308/0.739
Minibatch: 0201 | Dis/Gen Cost:    1.384/0.739
Minibatch: 0401 | Dis/Gen Cost:    1.339/0.755
Minibatch: 0601 | Dis/Gen Cost:    1.339/0.801
Minibatch: 0801 | Dis/Gen Cost:    1.408/0.822
Epoch:     0048 | Dis/Gen AvgCost: 1.357/0.760
Minibatch: 0001 | Dis/Gen Cost:    1.324/0.782
Minibatch: 0201 | Dis/Gen Cost:    1.325/0.791
Minibatch: 0401 | Dis/Gen Cost:    1.394/0.731
Minibatch: 0601 | Dis/Gen Cost:    1.390/0.718
Minibatch: 0801 | Dis/Gen Cost:    1.374/0.772
Epoch:     0049 | Dis/Gen AvgCost: 1.360/0.755
Minibatch: 0001 | Dis/Gen Cost:    1.364/0.819
Minibatch: 0201 | Dis/Gen Cost:    1.384/0.759
Minibatch: 0401 | Dis/Gen Cost:    1.313/0.776
Minibatch: 0601 | Dis/Gen Cost:    1.351/0.744
Minibatch: 0801 | Dis/Gen Cost:    1.394/0.743
Epoch:     0050 | Dis/Gen AvgCost: 1.363/0.752

In [5]:
%matplotlib inline
import matplotlib.pyplot as plt

plt.plot(range(len(avg_costs['discriminator'])), 
         avg_costs['discriminator'], label='discriminator')
plt.plot(range(len(avg_costs['generator'])),
         avg_costs['generator'], label='generator')
plt.legend()
plt.show()



In [6]:
####################################
### RELOAD & GENERATE SAMPLE IMAGES
####################################


n_examples = 25

with tf.Session(graph=g) as sess:
    saver.restore(sess, save_path='./gan-conv.ckpt')

    batch_randsample = np.random.uniform(-1, 1, size=(n_examples, gen_input_size))
    new_examples = sess.run('generator/generator_outputs:0',
                            feed_dict={'generator_inputs:0': batch_randsample,
                                       'dropout:0': 0.0,
                                       'is_training:0': False})

fig, axes = plt.subplots(nrows=5, ncols=5, figsize=(8, 8),
                         sharey=True, sharex=True)

for image, ax in zip(new_examples, axes.flatten()):
    ax.imshow(image.reshape((dis_input_size // 28, dis_input_size // 28)), cmap='binary')

plt.show()


INFO:tensorflow:Restoring parameters from ./gan-conv.ckpt