66References:
77 - Auto-Encoding Variational Bayes The International Conference on Learning
88 Representations (ICLR), Banff, 2014. D.P. Kingma, M. Welling
9+ - Understanding the difficulty of training deep feedforward neural networks.
10+ X Glorot, Y Bengio. Aistats 9, 249-256
911 - Y. LeCun, L. Bottou, Y. Bengio, and P. Haffner. "Gradient-based
1012 learning applied to document recognition." Proceedings of the IEEE,
1113 86(11):2278-2324, November 1998.
1214
1315Links:
1416 - [VAE Paper] https://arxiv.org/abs/1312.6114
17+ - [Xavier Glorot Init](www.cs.cmu.edu/~bhiksha/courses/deeplearning/Fall.../AISTATS2010_Glorot.pdf).
1518 - [MNIST Dataset] http://yann.lecun.com/exdb/mnist/
1619
1720Author: Aymeric Damien
3033
3134# Parameters
3235learning_rate = 0.001
33- n_steps = 20000
34- batch_size = 32
36+ num_steps = 30000
37+ batch_size = 64
3538
3639# Network Parameters
3740image_dim = 784 # MNIST images are 28x28 pixels
38- hidden_dim = 256
41+ hidden_dim = 512
3942latent_dim = 2
4043
41- # A custom initialization (see Xavier glorot init)
44+ # A custom initialization (see Xavier Glorot init)
4245def glorot_init (shape ):
4346 return tf .random_normal (shape = shape , stddev = 1. / tf .sqrt (shape [0 ] / 2. ))
4447
@@ -61,7 +64,7 @@ def glorot_init(shape):
6164# Building the encoder
6265input_image = tf .placeholder (tf .float32 , shape = [None , image_dim ])
6366encoder = tf .matmul (input_image , weights ['encoder_h1' ]) + biases ['encoder_b1' ]
64- encoder = tf .nn .relu (encoder )
67+ encoder = tf .nn .tanh (encoder )
6568z_mean = tf .matmul (encoder , weights ['z_mean' ]) + biases ['z_mean' ]
6669z_std = tf .matmul (encoder , weights ['z_std' ]) + biases ['z_std' ]
6770
@@ -72,7 +75,7 @@ def glorot_init(shape):
7275
7376# Building the decoder (with scope to re-use these layers later)
7477decoder = tf .matmul (z , weights ['decoder_h1' ]) + biases ['decoder_b1' ]
75- decoder = tf .nn .relu (decoder )
78+ decoder = tf .nn .tanh (decoder )
7679decoder = tf .matmul (decoder , weights ['decoder_out' ]) + biases ['decoder_out' ]
7780decoder = tf .nn .sigmoid (decoder )
7881
@@ -99,7 +102,7 @@ def vae_loss(x_reconstructed, x_true):
99102 sess .run (init )
100103
101104 # Training
102- for i in range (1 , n_steps + 1 ):
105+ for i in range (1 , num_steps + 1 ):
103106 # Prepare Data
104107 # Get the next batch of MNIST data (only images are needed, not labels)
105108 batch_x , _ = mnist .train .next_batch (batch_size )
@@ -115,24 +118,24 @@ def vae_loss(x_reconstructed, x_true):
115118 noise_input = tf .placeholder (tf .float32 , shape = [None , latent_dim ])
116119 # Rebuild the decoder to create image from noise
117120 decoder = tf .matmul (noise_input , weights ['decoder_h1' ]) + biases ['decoder_b1' ]
118- decoder = tf .nn .relu (decoder )
121+ decoder = tf .nn .tanh (decoder )
119122 decoder = tf .matmul (decoder , weights ['decoder_out' ]) + biases ['decoder_out' ]
120123 decoder = tf .nn .sigmoid (decoder )
121124
122125 # Building a manifold of generated digits
123- n = 15 # Figure row size
124- figure = np .zeros (( 28 * n , 28 * n ) )
125- # Random normal distributions to feed network with
126- x_axis = norm . ppf ( np . linspace ( 0. , 1. , n ))
127- y_axis = norm . ppf ( np .linspace ( 0. , 1. , n ))
128-
129- for i , x in enumerate (x_axis ):
130- for j , y in enumerate ( y_axis ):
131- samples = np . array ([[ x , y ]] )
132- x_reconstructed = sess . run ( decoder , feed_dict = { noise_input : samples })
133- digit = np . array ( x_reconstructed [0 ]) .reshape (28 , 28 )
134- figure [ i * 28 : ( i + 1 ) * 28 , j * 28 : ( j + 1 ) * 28 ] = digit
135-
136- plt . figure ( figsize = ( 10 , 10 ) )
137- plt .imshow (figure , cmap = 'Greys_r' )
126+ n = 20
127+ x_axis = np .linspace ( - 3 , 3 , n )
128+ y_axis = np . linspace ( - 3 , 3 , n )
129+
130+ canvas = np .empty (( 28 * n , 28 * n ))
131+ for i , yi in enumerate ( x_axis ):
132+ for j , xi in enumerate (y_axis ):
133+ z_mu = np . array ([[ xi , yi ]] * batch_size )
134+ x_mean = sess . run ( decoder , feed_dict = { noise_input : z_mu } )
135+ canvas [( n - i - 1 ) * 28 :( n - i ) * 28 , j * 28 :( j + 1 ) * 28 ] = \
136+ x_mean [0 ].reshape (28 , 28 )
137+
138+ plt . figure ( figsize = ( 8 , 10 ))
139+ Xi , Yi = np . meshgrid ( x_axis , y_axis )
140+ plt .imshow (canvas , origin = "upper" , cmap = "gray" )
138141 plt .show ()
0 commit comments