Skip to content

Commit

Permalink
Update 0511, add GAN_MNIST.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhang Yuan committed May 11, 2018
1 parent cbcfadc commit ca3ffbf
Show file tree
Hide file tree
Showing 106 changed files with 164 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tensorflow/TensorGAN/GAN/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def optimizer(loss, var_list):

def log(x):
'''
Sometimes discriminator outputs can reach vakyes close to (or even slightly less than) zero due to numerical rounding.
Sometimes discriminator outputs can reach values close to (or even slightly less than) zero due to numerical rounding.
This just make sure that we exclude those values so that we don't up with NaNs during optimization
'''
return tf.log(tf.maximum(x, 1e-5))
Expand Down
5 changes: 5 additions & 0 deletions tensorflow/TensorGAN/GAN_MNIST/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Data path
mnist_path = "../../datasets/MNIST_DATA/"
# Training hyper paramters
batch_size = 128
input_dim = 100
16 changes: 16 additions & 0 deletions tensorflow/TensorGAN/GAN_MNIST/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import tensorflow as tf
import numpy as np

import config
import net

seed = 42
np.random.seed(seed)
tf.set_random_seed(seed)

def main():
gan = net.GAN(config)
gan.train()

if __name__ == "__main__":
main()
109 changes: 109 additions & 0 deletions tensorflow/TensorGAN/GAN_MNIST/net.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import matplotlib.pyplot as plt

import config
import util

def log(x):
'''
Sometimes discriminator outputs can reach values close to (or even slightly less than) zero due to numerical rounding.
This just make sure that we exclude those values so that we don't up with NaNs during optimization
'''
return tf.log(tf.maximum(x, 1e-5))

def xavier_init(size):
in_dim = size[0]
xavier_stddev = 1. / tf.sqrt(in_dim / 2.)
return tf.random_normal(shape=size, stddev=xavier_stddev)

with tf.variable_scope('D'):
D_W1 = tf.Variable(xavier_init([784, 128]))
D_b1 = tf.Variable(tf.zeros(shape=[128]))

D_W2 = tf.Variable(xavier_init([128, 1]))
D_b2 = tf.Variable(tf.zeros(shape=[1]))

with tf.variable_scope('G'):
G_W1 = tf.Variable(xavier_init([100, 128]))
G_b1 = tf.Variable(tf.zeros(shape=[128]))

G_W2 = tf.Variable(xavier_init([128, 784]))
G_b2 = tf.Variable(tf.zeros(shape=[784]))

def sample_Z(m, n):
return np.random.uniform(-1., 1., size=[m, n])

def generator(z):
G_h1 = tf.nn.relu(tf.matmul(z, G_W1) + G_b1)
G_log_prob = tf.matmul(G_h1, G_W2) + G_b2
G_prob = tf.nn.sigmoid(G_log_prob)

return G_prob

def discriminator(x):
D_h1 = tf.nn.relu(tf.matmul(x, D_W1) + D_b1)
D_logit = tf.matmul(D_h1, D_W2) + D_b2
D_prob = tf.nn.sigmoid(D_logit)

return D_prob, D_logit

class GAN(object):
def __init__(self, config):

self.config = config

self.Z = tf.placeholder(tf.float32, shape=[None, 100])
self.G_sample = generator(self.Z)

self.X = tf.placeholder(tf.float32, shape=[None, 784])
self.D_real, self.D_logit_real = discriminator(self.X)
self.D_fake, self.D_logit_fake = discriminator(self.G_sample)

vars = tf.trainable_variables()
self.d_params = [v for v in vars if v.name.startswith('D/')]
self.g_params = [v for v in vars if v.name.startswith('G/')]

self.D_loss = -tf.reduce_mean(log(self.D_real) + log(1.-self.D_fake))
self.G_loss = -tf.reduce_mean(log(self.D_fake))

self.D_optim = tf.train.AdamOptimizer().minimize(self.D_loss, var_list=self.d_params)
self.G_optim = tf.train.AdamOptimizer().minimize(self.G_loss, var_list=self.g_params)

def train(self):

mnist = input_data.read_data_sets(self.config.mnist_path, one_hot=True)
batch_size = self.config.batch_size
input_dim = self.config.input_dim

with tf.Session() as sess:

sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())

save_index = 0

for iter in range(100000):

batch_X, _ = mnist.train.next_batch(batch_size)
batch_Z_1 = sample_Z(batch_size, input_dim)
batch_Z_2 = sample_Z(batch_size, input_dim)

_, D_loss_curr = sess.run([self.D_optim, self.D_loss], feed_dict={self.X: batch_X, self.Z: batch_Z_1})
_, G_loss_curr = sess.run([self.G_optim, self.G_loss], feed_dict={self.Z: batch_Z_2})

if iter % 1000 == 0:
print("iter", iter)
print("D Loss: ", D_loss_curr)
print("G Loss: ", G_loss_curr)

test_Z = sample_Z(16, input_dim)
samples = sess.run(self.G_sample, feed_dict={self.Z: test_Z})

fig = util.plot(samples)
save_filename = './out/' + str(save_index).zfill(3)
save_index += 1
plt.savefig(save_filename, bbox_inches='tight')
plt.close()

Binary file added tensorflow/TensorGAN/GAN_MNIST/out/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/006.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/007.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/008.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/009.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/010.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/011.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/012.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/013.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/014.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/015.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/016.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/017.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/018.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/019.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/021.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/022.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/023.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/024.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/025.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/026.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/027.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/028.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/029.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/030.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/031.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/032.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/033.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/034.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/035.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/036.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/037.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/038.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/039.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/040.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/041.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/042.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/043.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/044.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/045.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/046.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/047.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/048.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/049.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/050.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/051.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/052.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/053.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/054.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/055.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/056.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/057.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/058.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/059.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/060.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/061.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/062.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/063.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/064.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/065.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/066.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/067.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/068.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/069.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/070.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/071.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/072.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/073.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/074.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/075.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/076.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/077.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/078.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/079.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/080.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/081.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/082.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/083.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/084.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/085.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/086.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/087.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/088.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/089.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/090.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/091.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/092.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/093.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/094.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/095.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/096.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/097.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/098.png
Binary file added tensorflow/TensorGAN/GAN_MNIST/out/099.png
20 changes: 20 additions & 0 deletions tensorflow/TensorGAN/GAN_MNIST/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

def plot(samples):

plt.switch_backend('agg')

fig = plt.figure(figsize=(4, 4))
gs = gridspec.GridSpec(4, 4)
gs.update(wspace=0.05, hspace=0.05)

for i, sample in enumerate(samples):
ax = plt.subplot(gs[i])
plt.axis('off')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_aspect('equal')
plt.imshow(sample.reshape(28, 28), cmap='Greys_r')

return fig
13 changes: 13 additions & 0 deletions tensorflow/TensorGAN/cGAN/net.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import tensorflow as tf

def xavier_init(size):
in_dim = size[0]
xavier_stddev = 1. / tf.sqrt(in_dim / 2.)
return tf.random_normal(shape=size, stddev=xavier_stddev)

# Disriminator Net


# Generator Net

def gene

0 comments on commit ca3ffbf

Please sign in to comment.