From ab3c83332dc368febc5261cd6f1c7d9c6b6d8bc4 Mon Sep 17 00:00:00 2001 From: Moonkwanghyun Date: Thu, 8 Nov 2018 10:09:23 +0900 Subject: [PATCH] Fix using mutable default arguments. --- 11_gan.ipynb | 10 +++++++--- 11_gan.py | 51 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/11_gan.ipynb b/11_gan.ipynb index 0949205..3d2aefe 100644 --- a/11_gan.ipynb +++ b/11_gan.ipynb @@ -162,13 +162,17 @@ }, "outputs": [], "source": [ - "def G(z, w=g_weights):\n", + "def G(z, w=None):\n", + " if w is None:\n", + " w = g_weights\n", " h1 = tf.nn.relu(tf.matmul(z, w['w1']) + w['b1'])\n", " return tf.sigmoid(tf.matmul(h1, w['out']) + w['b2'])\n", "\n", - "def D(x, w=d_weights):\n", + "def D(x, w=None):\n", + " if w is None:\n", + " w = d_weights\n", " h1 = tf.nn.relu(tf.matmul(x, w['w1']) + w['b1'])\n", - " return tf.sigmoid(tf.matmul(h1, w['out']) + w['b2'])" + " return tf.sigmoid(tf.matmul(h1, w['out']) + w['b2'])\n" ] }, { diff --git a/11_gan.py b/11_gan.py index 089fec0..346e28a 100644 --- a/11_gan.py +++ b/11_gan.py @@ -1,13 +1,16 @@ import tensorflow as tf import numpy as np from tensorflow.examples.tutorials.mnist import input_data -#from PIL import Image + +# from PIL import Image mnist = input_data.read_data_sets("MNIST_data/") images = mnist.train.images + def xavier_initializer(shape): - return tf.random_normal(shape=shape, stddev=1.0/shape[0]) + return tf.random_normal(shape=shape, stddev=1.0 / shape[0]) + # Generator z_size = 100 # maybe larger @@ -30,40 +33,47 @@ def xavier_initializer(shape): 'b2': tf.Variable(tf.zeros(shape=[g_out_size])), } -d_weights ={ +d_weights = { 'w1': tf.Variable(xavier_initializer(shape=(x_size, d_w1_size))), 'b1': tf.Variable(tf.zeros(shape=[d_w1_size])), 'out': tf.Variable(xavier_initializer(shape=(d_w1_size, d_out_size))), 'b2': tf.Variable(tf.zeros(shape=[d_out_size])), } -def G(z, w=g_weights): + +def G(z, w=None): # here tanh is better than relu + if w is None: + w = g_weights h1 = tf.tanh(tf.matmul(z, w['w1']) + w['b1']) # pixel output is in range [0, 255] return tf.sigmoid(tf.matmul(h1, w['out']) + w['b2']) * 255 -def D(x, w=d_weights): + +def D(x, w=None): # here tanh is better than relu + if w is None: + w = d_weights h1 = tf.tanh(tf.matmul(x, w['w1']) + w['b1']) h2 = tf.matmul(h1, w['out']) + w['b2'] - return h2 # use h2 to calculate logits loss + return h2 # use h2 to calculate logits loss + def generate_z(n=1): return np.random.normal(size=(n, z_size)) -sample = G(z) +sample = G(z) dout_real = D(X) dout_fake = D(G(z)) G_obj = tf.reduce_mean( - tf.nn.sigmoid_cross_entropy_with_logits(logits=dout_fake, labels=tf.ones_like(dout_fake))) -D_obj_real = tf.reduce_mean( # use single side smoothing - tf.nn.sigmoid_cross_entropy_with_logits(logits=dout_real, labels=(tf.ones_like(dout_real)-0.1))) + tf.nn.sigmoid_cross_entropy_with_logits(logits=dout_fake, labels=tf.ones_like(dout_fake))) +D_obj_real = tf.reduce_mean( # use single side smoothing + tf.nn.sigmoid_cross_entropy_with_logits(logits=dout_real, labels=(tf.ones_like(dout_real) - 0.1))) D_obj_fake = tf.reduce_mean( - tf.nn.sigmoid_cross_entropy_with_logits(logits=dout_fake, labels=tf.zeros_like(dout_fake))) + tf.nn.sigmoid_cross_entropy_with_logits(logits=dout_fake, labels=tf.zeros_like(dout_fake))) D_obj = D_obj_real + D_obj_fake G_opt = tf.train.AdamOptimizer().minimize(G_obj, var_list=g_weights.values()) @@ -74,7 +84,7 @@ def generate_z(n=1): with tf.Session() as sess: sess.run(tf.global_variables_initializer()) - + for i in range(200): sess.run(D_opt, feed_dict={ X: images[np.random.choice(range(len(images)), batch_size)].reshape(batch_size, x_size), @@ -87,22 +97,21 @@ def generate_z(n=1): sess.run(G_opt, feed_dict={ z: generate_z(batch_size) }) - + g_cost = sess.run(G_obj, feed_dict={z: generate_z(batch_size)}) d_cost = sess.run(D_obj, feed_dict={ X: images[np.random.choice(range(len(images)), batch_size)].reshape(batch_size, x_size), z: generate_z(batch_size), }) - image = sess.run(G(z), feed_dict={z:generate_z()}) - df = sess.run(tf.sigmoid(dout_fake), feed_dict={z:generate_z()}) + image = sess.run(G(z), feed_dict={z: generate_z()}) + df = sess.run(tf.sigmoid(dout_fake), feed_dict={z: generate_z()}) # print i, G cost, D cost, image max pixel, D output of fake - print (i, g_cost, d_cost, image.max(), df[0][0]) + print(i, g_cost, d_cost, image.max(), df[0][0]) # You may wish to save or plot the image generated # to see how it looks like - image = sess.run(G(z), feed_dict={z:generate_z()}) + image = sess.run(G(z), feed_dict={z: generate_z()}) image1 = image[0].reshape([28, 28]) - #print image1 - #im = Image.fromarray(image1) - #im.show() - + # print image1 + # im = Image.fromarray(image1) + # im.show()