Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix using mutable default arguments. #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions 11_gan.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand Down
51 changes: 30 additions & 21 deletions 11_gan.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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())
Expand All @@ -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),
Expand All @@ -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()