-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathautoencoder.py
More file actions
85 lines (74 loc) · 3.69 KB
/
autoencoder.py
File metadata and controls
85 lines (74 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import tensorflow as tf
import numpy as np
def get_batch(X, size):
a = np.random.choice(len(X), size, replace=False)
return X[a]
class Autoencoder:
def __init__(self, input_dim, hidden_dim, epoch=1000, batch_size=50, learning_rate=0.001):
self.epoch = epoch
self.batch_size = batch_size
self.learning_rate = learning_rate
x = tf.placeholder(dtype=tf.float32, shape=[None, input_dim])
with tf.name_scope('encode'):
weights = tf.Variable(tf.random_normal([input_dim, hidden_dim], dtype=tf.float32), name='weights')
biases = tf.Variable(tf.zeros([hidden_dim]), name='biases')
encoded = tf.nn.sigmoid(tf.matmul(x, weights) + biases)
with tf.name_scope('decode'):
weights = tf.Variable(tf.random_normal([hidden_dim, input_dim], dtype=tf.float32), name='weights')
biases = tf.Variable(tf.zeros([input_dim]), name='biases')
decoded = tf.matmul(encoded, weights) + biases
self.x = x
self.encoded = encoded
self.decoded = decoded
self.loss = tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(self.x, self.decoded))))
self.train_op = tf.train.RMSPropOptimizer(self.learning_rate).minimize(self.loss)
self.saver = tf.train.Saver()
def train(self, data):
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(self.epoch):
for j in range(np.shape(data)[0] // self.batch_size):
batch_data = get_batch(data, self.batch_size)
l, _ = sess.run([self.loss, self.train_op], feed_dict={self.x: batch_data})
if i % 10 == 0:
print('epoch {0}: loss = {1}'.format(i, l))
self.saver.save(sess, './model.ckpt')
self.saver.save(sess, './model.ckpt')
def test(self, data):
with tf.Session() as sess:
self.saver.restore(sess, './model.ckpt')
hidden, reconstructed = sess.run([self.encoded, self.decoded], feed_dict={self.x: data})
print('input', data)
print('compressed', hidden)
print('reconstructed', reconstructed)
return reconstructed
def get_params(self):
with tf.Session() as sess:
self.saver.restore(sess, './model.ckpt')
weights, biases = sess.run([self.weights1, self.biases1])
return weights, biases
def classify(self, data, labels):
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
self.saver.restore(sess, './model.ckpt')
hidden, reconstructed = sess.run([self.encoded, self.decoded], feed_dict={self.x: data})
reconstructed = reconstructed[0]
# loss = sess.run(self.all_loss, feed_dict={self.x: data})
print('data', np.shape(data))
print('reconstructed', np.shape(reconstructed))
loss = np.sqrt(np.mean(np.square(data - reconstructed), axis=1))
print('loss', np.shape(loss))
horse_indices = np.where(labels == 7)[0]
not_horse_indices = np.where(labels != 7)[0]
horse_loss = np.mean(loss[horse_indices])
not_horse_loss = np.mean(loss[not_horse_indices])
print('horse', horse_loss)
print('not horse', not_horse_loss)
return hidden
def decode(self, encoding):
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
self.saver.restore(sess, './model.ckpt')
reconstructed = sess.run(self.decoded, feed_dict={self.encoded: encoding})
img = np.reshape(reconstructed, (32, 32))
return img