Skip to content

Commit aef3582

Browse files
joshthowardnealwu
authored andcommitted
Removed external dependencies from autoencoder models
1 parent 79d2ecb commit aef3582

8 files changed

+27
-41
lines changed

autoencoder/AdditiveGaussianNoiseAutoencoderRunner.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import tensorflow as tf
55
from tensorflow.examples.tutorials.mnist import input_data
66

7-
from autoencoder.autoencoder_models.DenoisingAutoencoder import AdditiveGaussianNoiseAutoencoder
7+
from autoencoder_models.DenoisingAutoencoder import AdditiveGaussianNoiseAutoencoder
88

99
mnist = input_data.read_data_sets('MNIST_data', one_hot = True)
1010

@@ -45,7 +45,6 @@ def get_random_block_from_data(data, batch_size):
4545

4646
# Display logs per epoch step
4747
if epoch % display_step == 0:
48-
print "Epoch:", '%04d' % (epoch + 1), \
49-
"cost=", "{:.9f}".format(avg_cost)
48+
print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(avg_cost))
5049

51-
print "Total cost: " + str(autoencoder.calc_total_cost(X_test))
50+
print("Total cost: " + str(autoencoder.calc_total_cost(X_test)))

autoencoder/AutoencoderRunner.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import tensorflow as tf
55
from tensorflow.examples.tutorials.mnist import input_data
66

7-
from autoencoder.autoencoder_models.Autoencoder import Autoencoder
7+
from autoencoder_models.Autoencoder import Autoencoder
88

99
mnist = input_data.read_data_sets('MNIST_data', one_hot = True)
1010

@@ -44,7 +44,6 @@ def get_random_block_from_data(data, batch_size):
4444

4545
# Display logs per epoch step
4646
if epoch % display_step == 0:
47-
print "Epoch:", '%04d' % (epoch + 1), \
48-
"cost=", "{:.9f}".format(avg_cost)
47+
print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(avg_cost))
4948

50-
print "Total cost: " + str(autoencoder.calc_total_cost(X_test))
49+
print("Total cost: " + str(autoencoder.calc_total_cost(X_test)))

autoencoder/MaskingNoiseAutoencoderRunner.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import tensorflow as tf
55
from tensorflow.examples.tutorials.mnist import input_data
66

7-
from autoencoder.autoencoder_models.DenoisingAutoencoder import MaskingNoiseAutoencoder
7+
from autoencoder_models.DenoisingAutoencoder import MaskingNoiseAutoencoder
88

99
mnist = input_data.read_data_sets('MNIST_data', one_hot = True)
1010

@@ -43,7 +43,6 @@ def get_random_block_from_data(data, batch_size):
4343
avg_cost += cost / n_samples * batch_size
4444

4545
if epoch % display_step == 0:
46-
print "Epoch:", '%04d' % (epoch + 1), \
47-
"cost=", "{:.9f}".format(avg_cost)
46+
print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(avg_cost))
4847

49-
print "Total cost: " + str(autoencoder.calc_total_cost(X_test))
48+
print("Total cost: " + str(autoencoder.calc_total_cost(X_test)))

autoencoder/Utils.py

-9
This file was deleted.

autoencoder/VariationalAutoencoderRunner.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import tensorflow as tf
55
from tensorflow.examples.tutorials.mnist import input_data
66

7-
from autoencoder.autoencoder_models.VariationalAutoencoder import VariationalAutoencoder
7+
from autoencoder_models.VariationalAutoencoder import VariationalAutoencoder
88

99
mnist = input_data.read_data_sets('MNIST_data', one_hot = True)
1010

@@ -47,7 +47,6 @@ def get_random_block_from_data(data, batch_size):
4747

4848
# Display logs per epoch step
4949
if epoch % display_step == 0:
50-
print "Epoch:", '%04d' % (epoch + 1), \
51-
"cost=", "{:.9f}".format(avg_cost)
50+
print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(avg_cost))
5251

53-
print "Total cost: " + str(autoencoder.calc_total_cost(X_test))
52+
print("Total cost: " + str(autoencoder.calc_total_cost(X_test)))

autoencoder/autoencoder_models/Autoencoder.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import tensorflow as tf
2-
import numpy as np
3-
import autoencoder.Utils
42

53
class Autoencoder(object):
64

@@ -28,7 +26,8 @@ def __init__(self, n_input, n_hidden, transfer_function=tf.nn.softplus, optimize
2826

2927
def _initialize_weights(self):
3028
all_weights = dict()
31-
all_weights['w1'] = tf.Variable(autoencoder.Utils.xavier_init(self.n_input, self.n_hidden))
29+
all_weights['w1'] = tf.get_variable("w1", shape=[self.n_input, self.n_hidden],
30+
initializer=tf.contrib.layers.xavier_initializer())
3231
all_weights['b1'] = tf.Variable(tf.zeros([self.n_hidden], dtype=tf.float32))
3332
all_weights['w2'] = tf.Variable(tf.zeros([self.n_hidden, self.n_input], dtype=tf.float32))
3433
all_weights['b2'] = tf.Variable(tf.zeros([self.n_input], dtype=tf.float32))
@@ -46,7 +45,7 @@ def transform(self, X):
4645

4746
def generate(self, hidden = None):
4847
if hidden is None:
49-
hidden = np.random.normal(size=self.weights["b1"])
48+
hidden = self.sess.run(tf.random_normal([1, self.n_hidden]))
5049
return self.sess.run(self.reconstruction, feed_dict={self.hidden: hidden})
5150

5251
def reconstruct(self, X):

autoencoder/autoencoder_models/DenoisingAutoencoder.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import tensorflow as tf
2-
import numpy as np
3-
import autoencoder.Utils
4-
52

63
class AdditiveGaussianNoiseAutoencoder(object):
74
def __init__(self, n_input, n_hidden, transfer_function = tf.nn.softplus, optimizer = tf.train.AdamOptimizer(),
@@ -31,7 +28,8 @@ def __init__(self, n_input, n_hidden, transfer_function = tf.nn.softplus, optimi
3128

3229
def _initialize_weights(self):
3330
all_weights = dict()
34-
all_weights['w1'] = tf.Variable(autoencoder.Utils.xavier_init(self.n_input, self.n_hidden))
31+
all_weights['w1'] = tf.get_variable("w1", shape=[self.n_input, self.n_hidden],
32+
initializer=tf.contrib.layers.xavier_initializer())
3533
all_weights['b1'] = tf.Variable(tf.zeros([self.n_hidden], dtype = tf.float32))
3634
all_weights['w2'] = tf.Variable(tf.zeros([self.n_hidden, self.n_input], dtype = tf.float32))
3735
all_weights['b2'] = tf.Variable(tf.zeros([self.n_input], dtype = tf.float32))
@@ -53,9 +51,9 @@ def transform(self, X):
5351
self.scale: self.training_scale
5452
})
5553

56-
def generate(self, hidden = None):
54+
def generate(self, hidden=None):
5755
if hidden is None:
58-
hidden = np.random.normal(size = self.weights["b1"])
56+
hidden = self.sess.run(tf.random_normal([1, self.n_hidden]))
5957
return self.sess.run(self.reconstruction, feed_dict = {self.hidden: hidden})
6058

6159
def reconstruct(self, X):
@@ -98,7 +96,8 @@ def __init__(self, n_input, n_hidden, transfer_function = tf.nn.softplus, optimi
9896

9997
def _initialize_weights(self):
10098
all_weights = dict()
101-
all_weights['w1'] = tf.Variable(autoencoder.Utils.xavier_init(self.n_input, self.n_hidden))
99+
all_weights['w1'] = tf.get_variable("w1", shape=[self.n_input, self.n_hidden],
100+
initializer=tf.contrib.layers.xavier_initializer())
102101
all_weights['b1'] = tf.Variable(tf.zeros([self.n_hidden], dtype = tf.float32))
103102
all_weights['w2'] = tf.Variable(tf.zeros([self.n_hidden, self.n_input], dtype = tf.float32))
104103
all_weights['b2'] = tf.Variable(tf.zeros([self.n_input], dtype = tf.float32))
@@ -115,9 +114,9 @@ def calc_total_cost(self, X):
115114
def transform(self, X):
116115
return self.sess.run(self.hidden, feed_dict = {self.x: X, self.keep_prob: 1.0})
117116

118-
def generate(self, hidden = None):
117+
def generate(self, hidden=None):
119118
if hidden is None:
120-
hidden = np.random.normal(size = self.weights["b1"])
119+
hidden = self.sess.run(tf.random_normal([1, self.n_hidden]))
121120
return self.sess.run(self.reconstruction, feed_dict = {self.hidden: hidden})
122121

123122
def reconstruct(self, X):

autoencoder/autoencoder_models/VariationalAutoencoder.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import tensorflow as tf
22
import numpy as np
3-
import autoencoder.Utils
43

54
class VariationalAutoencoder(object):
65

@@ -36,8 +35,10 @@ def __init__(self, n_input, n_hidden, optimizer = tf.train.AdamOptimizer()):
3635

3736
def _initialize_weights(self):
3837
all_weights = dict()
39-
all_weights['w1'] = tf.Variable(autoencoder.Utils.xavier_init(self.n_input, self.n_hidden))
40-
all_weights['log_sigma_w1'] = tf.Variable(autoencoder.Utils.xavier_init(self.n_input, self.n_hidden))
38+
all_weights['w1'] = tf.get_variable("w1", shape=[self.n_input, self.n_hidden],
39+
initializer=tf.contrib.layers.xavier_initializer())
40+
all_weights['log_sigma_w1'] = tf.get_variable("log_sigma_w1", shape=[self.n_input, self.n_hidden],
41+
initializer=tf.contrib.layers.xavier_initializer())
4142
all_weights['b1'] = tf.Variable(tf.zeros([self.n_hidden], dtype=tf.float32))
4243
all_weights['log_sigma_b1'] = tf.Variable(tf.zeros([self.n_hidden], dtype=tf.float32))
4344
all_weights['w2'] = tf.Variable(tf.zeros([self.n_hidden, self.n_input], dtype=tf.float32))

0 commit comments

Comments
 (0)