Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. import tensorflow as tf
  2. from tensorflow.examples.tutorials.mnist import input_data
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import matplotlib.gridspec as gridspec
  6. import os
  7.  
  8.  
  9. def xavier_init(size):
  10. in_dim = size[0]
  11. xavier_stddev = 1. / tf.sqrt(in_dim / 2.)
  12. return tf.random_normal(shape=size, stddev=xavier_stddev)
  13.  
  14.  
  15. X = tf.placeholder(tf.float32, shape=[None, 784])
  16.  
  17. D_W1 = tf.Variable(xavier_init([784, 128]))
  18. D_b1 = tf.Variable(tf.zeros(shape=[128]))
  19.  
  20. D_W2 = tf.Variable(xavier_init([128, 1]))
  21. D_b2 = tf.Variable(tf.zeros(shape=[1]))
  22.  
  23. theta_D = [D_W1, D_W2, D_b1, D_b2]
  24.  
  25.  
  26. Z = tf.placeholder(tf.float32, shape=[None, 100])
  27.  
  28. G_W1 = tf.Variable(xavier_init([100, 128]))
  29. G_b1 = tf.Variable(tf.zeros(shape=[128]))
  30.  
  31. G_W2 = tf.Variable(xavier_init([128, 784]))
  32. G_b2 = tf.Variable(tf.zeros(shape=[784]))
  33.  
  34. theta_G = [G_W1, G_W2, G_b1, G_b2]
  35.  
  36.  
  37. DC_D_W1 = tf.Variable(xavier_init([5, 5, 1, 16]))
  38. DC_D_b1 = tf.Variable(tf.zeros(shape=[16]))
  39.  
  40. DC_D_W2 = tf.Variable(xavier_init([3, 3, 16, 32]))
  41. DC_D_b2 = tf.Variable(tf.zeros(shape=[32]))
  42.  
  43. DC_D_W3 = tf.Variable(xavier_init([7 * 7 * 32, 128]))
  44. DC_D_b3 = tf.Variable(tf.zeros(shape=[128]))
  45.  
  46. DC_D_W4 = tf.Variable(xavier_init([128, 1]))
  47. DC_D_b4 = tf.Variable(tf.zeros(shape=[1]))
  48.  
  49. theta_DC_D = [DC_D_W1, DC_D_b1, DC_D_W2, DC_D_b2, DC_D_W3, DC_D_b3, DC_D_W4, DC_D_b4]
  50.  
  51.  
  52. def sample_Z(m, n):
  53. return np.random.uniform(-1., 1., size=[m, n])
  54.  
  55.  
  56. def generator(z):
  57. G_h1 = tf.nn.relu(tf.matmul(z, G_W1) + G_b1)
  58. G_log_prob = tf.matmul(G_h1, G_W2) + G_b2
  59. G_prob = tf.nn.sigmoid(G_log_prob)
  60.  
  61. return G_prob
  62.  
  63.  
  64. def discriminator(x):
  65. D_h1 = tf.nn.relu(tf.matmul(x, D_W1) + D_b1)
  66. D_logit = tf.matmul(D_h1, D_W2) + D_b2
  67. D_prob = tf.nn.sigmoid(D_logit)
  68.  
  69. return D_prob, D_logit
  70.  
  71.  
  72. def dc_generator(z):
  73. pass
  74.  
  75.  
  76. def dc_discriminator(x):
  77. x = tf.reshape(x, shape=[-1, 28, 28, 1])
  78. conv1 = tf.nn.relu(tf.nn.conv2d(x, DC_D_W1, strides=[1, 2, 2, 1], padding='SAME') + DC_D_b1)
  79. conv2 = tf.nn.relu(tf.nn.conv2d(conv1, DC_D_W2, strides=[1, 2, 2, 1], padding='SAME') + DC_D_b2)
  80. conv2 = tf.reshape(conv2, shape=[-1, 7 * 7 * 32])
  81. h = tf.nn.relu(tf.matmul(conv2, DC_D_W3) + DC_D_b3)
  82. logit = tf.matmul(h, DC_D_W4) + DC_D_b4
  83. prob = tf.nn.sigmoid(logit)
  84.  
  85. return prob, logit
  86.  
  87.  
  88. def plot(samples):
  89. fig = plt.figure(figsize=(4, 4))
  90. gs = gridspec.GridSpec(4, 4)
  91. gs.update(wspace=0.05, hspace=0.05)
  92.  
  93. for i, sample in enumerate(samples):
  94. ax = plt.subplot(gs[i])
  95. plt.axis('off')
  96. ax.set_xticklabels([])
  97. ax.set_yticklabels([])
  98. ax.set_aspect('equal')
  99. plt.imshow(sample.reshape(28, 28), cmap='Greys_r')
  100.  
  101. return fig
  102.  
  103.  
  104. G_sample = generator(Z)
  105. D_real, D_logit_real = dc_discriminator(X)
  106. D_fake, D_logit_fake = dc_discriminator(G_sample)
  107.  
  108. # D_loss = -tf.reduce_mean(tf.log(D_real) + tf.log(1. - D_fake))
  109. # G_loss = -tf.reduce_mean(tf.log(D_fake))
  110.  
  111. # Alternative losses:
  112. # -------------------
  113. D_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(D_logit_real, tf.ones_like(D_logit_real)))
  114. D_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(D_logit_fake, tf.zeros_like(D_logit_fake)))
  115. D_loss = D_loss_real + D_loss_fake
  116. G_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(D_logit_fake, tf.ones_like(D_logit_fake)))
  117.  
  118. D_solver = tf.train.AdamOptimizer().minimize(D_loss, var_list=theta_DC_D)
  119. G_solver = tf.train.AdamOptimizer().minimize(G_loss, var_list=theta_G)
  120.  
  121. mb_size = 128
  122. Z_dim = 100
  123.  
  124. mnist = input_data.read_data_sets('../data/MNIST_data', one_hot=True)
  125.  
  126. sess = tf.Session()
  127. sess.run(tf.initialize_all_variables())
  128.  
  129. if not os.path.exists('../out/'):
  130. os.makedirs('../out/')
  131.  
  132. i = 0
  133.  
  134. for it in range(1000000):
  135. if it % 100 == 0:
  136. samples = sess.run(G_sample, feed_dict={Z: sample_Z(16, Z_dim)})
  137.  
  138. fig = plot(samples)
  139. plt.savefig('../out/{}.png'.format(str(i).zfill(3)), bbox_inches='tight')
  140. i += 1
  141. plt.close(fig)
  142.  
  143. X_mb, _ = mnist.train.next_batch(mb_size)
  144.  
  145. _, D_loss_curr = sess.run([D_solver, D_loss], feed_dict={X: X_mb, Z: sample_Z(mb_size, Z_dim)})
  146. _, G_loss_curr = sess.run([G_solver, G_loss], feed_dict={Z: sample_Z(mb_size, Z_dim)})
  147.  
  148. if it % 100 == 0:
  149. print('Iter: {}'.format(it))
  150. print('D loss: {:.4}'. format(D_loss_curr))
  151. print('G_loss: {:.4}'.format(G_loss_curr))
  152. print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement