Commit 54817481 authored by Martin Wicke's avatar Martin Wicke
Browse files

Merge pull request #26 from daviddao/master

Fixing issues #23 and #25
parents 2ac5f73c a472ac95
...@@ -9,6 +9,7 @@ from autoencoder.autoencoder_models.VariationalAutoencoder import VariationalAut ...@@ -9,6 +9,7 @@ from autoencoder.autoencoder_models.VariationalAutoencoder import VariationalAut
mnist = input_data.read_data_sets('MNIST_data', one_hot = True) mnist = input_data.read_data_sets('MNIST_data', one_hot = True)
def min_max_scale(X_train, X_test): def min_max_scale(X_train, X_test):
preprocessor = prep.MinMaxScaler().fit(X_train) preprocessor = prep.MinMaxScaler().fit(X_train)
X_train = preprocessor.transform(X_train) X_train = preprocessor.transform(X_train)
...@@ -30,8 +31,7 @@ display_step = 1 ...@@ -30,8 +31,7 @@ display_step = 1
autoencoder = VariationalAutoencoder(n_input = 784, autoencoder = VariationalAutoencoder(n_input = 784,
n_hidden = 200, n_hidden = 200,
optimizer = tf.train.AdamOptimizer(learning_rate = 0.001), optimizer = tf.train.AdamOptimizer(learning_rate = 0.001))
gaussian_sample_size = 128)
for epoch in range(training_epochs): for epoch in range(training_epochs):
avg_cost = 0. avg_cost = 0.
......
...@@ -4,11 +4,9 @@ import autoencoder.Utils ...@@ -4,11 +4,9 @@ import autoencoder.Utils
class VariationalAutoencoder(object): class VariationalAutoencoder(object):
def __init__(self, n_input, n_hidden, optimizer = tf.train.AdamOptimizer(), def __init__(self, n_input, n_hidden, optimizer = tf.train.AdamOptimizer()):
gaussian_sample_size = 128):
self.n_input = n_input self.n_input = n_input
self.n_hidden = n_hidden self.n_hidden = n_hidden
self.gaussian_sample_size = gaussian_sample_size
network_weights = self._initialize_weights() network_weights = self._initialize_weights()
self.weights = network_weights self.weights = network_weights
...@@ -18,14 +16,12 @@ class VariationalAutoencoder(object): ...@@ -18,14 +16,12 @@ class VariationalAutoencoder(object):
self.z_mean = tf.add(tf.matmul(self.x, self.weights['w1']), self.weights['b1']) self.z_mean = tf.add(tf.matmul(self.x, self.weights['w1']), self.weights['b1'])
self.z_log_sigma_sq = tf.add(tf.matmul(self.x, self.weights['log_sigma_w1']), self.weights['log_sigma_b1']) self.z_log_sigma_sq = tf.add(tf.matmul(self.x, self.weights['log_sigma_w1']), self.weights['log_sigma_b1'])
# sample from gaussian distribution # sample from gaussian distribution
eps = tf.random_normal((self.gaussian_sample_size, n_hidden), 0, 1, dtype = tf.float32) eps = tf.random_normal(tf.pack([tf.shape(self.x)[0], self.n_hidden]), 0, 1, dtype = tf.float32)
self.z = tf.add(self.z_mean, tf.mul(tf.sqrt(tf.exp(self.z_log_sigma_sq)), eps)) self.z = tf.add(self.z_mean, tf.mul(tf.sqrt(tf.exp(self.z_log_sigma_sq)), eps))
self.reconstruction = tf.add(tf.matmul(self.z, self.weights['w2']), self.weights['b2']) self.reconstruction = tf.add(tf.matmul(self.z, self.weights['w2']), self.weights['b2'])
# cost # cost
reconstr_loss = 0.5 * tf.reduce_sum(tf.pow(tf.sub(self.reconstruction, self.x), 2.0)) reconstr_loss = 0.5 * tf.reduce_sum(tf.pow(tf.sub(self.reconstruction, self.x), 2.0))
latent_loss = -0.5 * tf.reduce_sum(1 + self.z_log_sigma_sq latent_loss = -0.5 * tf.reduce_sum(1 + self.z_log_sigma_sq
...@@ -38,7 +34,6 @@ class VariationalAutoencoder(object): ...@@ -38,7 +34,6 @@ class VariationalAutoencoder(object):
self.sess = tf.Session() self.sess = tf.Session()
self.sess.run(init) self.sess.run(init)
def _initialize_weights(self): def _initialize_weights(self):
all_weights = dict() all_weights = dict()
all_weights['w1'] = tf.Variable(autoencoder.Utils.xavier_init(self.n_input, self.n_hidden)) all_weights['w1'] = tf.Variable(autoencoder.Utils.xavier_init(self.n_input, self.n_hidden))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment