forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoencoder_tf.py
230 lines (188 loc) · 7.48 KB
/
autoencoder_tf.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# https://deeplearningcourses.com/c/unsupervised-deep-learning-in-python
# https://www.udemy.com/unsupervised-deep-learning-in-python
from __future__ import print_function, division
from builtins import range, input
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from util import error_rate, getKaggleMNIST
class AutoEncoder(object):
def __init__(self, D, M, an_id):
self.M = M
self.id = an_id
self.build(D, M)
def set_session(self, session):
self.session = session
def build(self, D, M):
self.W = tf.Variable(tf.random_normal(shape=(D, M)))
self.bh = tf.Variable(np.zeros(M).astype(np.float32))
self.bo = tf.Variable(np.zeros(D).astype(np.float32))
self.X_in = tf.placeholder(tf.float32, shape=(None, D))
self.Z = self.forward_hidden(self.X_in) # for transform() later
self.X_hat = self.forward_output(self.X_in)
# using the naive formulation for cross-entropy
# will have numerical stability issues if X_hat = 0 or 1
logits = self.forward_logits(self.X_in)
self.cost = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
labels=self.X_in,
logits=logits,
)
)
self.train_op = tf.train.AdamOptimizer(1e-1).minimize(self.cost)
# self.train_op = tf.train.MomentumOptimizer(1e-3, momentum=0.9).minimize(self.cost)
def fit(self, X, epochs=1, batch_sz=100, show_fig=False):
N, D = X.shape
n_batches = N // batch_sz
costs = []
print("training autoencoder: %s" % self.id)
for i in range(epochs):
print("epoch:", i)
X = shuffle(X)
for j in range(n_batches):
batch = X[j*batch_sz:(j*batch_sz + batch_sz)]
_, c = self.session.run((self.train_op, self.cost), feed_dict={self.X_in: batch})
if j % 10 == 0:
print("j / n_batches:", j, "/", n_batches, "cost:", c)
costs.append(c)
if show_fig:
plt.plot(costs)
plt.show()
def transform(self, X):
# accepts and returns a real numpy array
# unlike forward_hidden and forward_output
# which deal with tensorflow variables
return self.session.run(self.Z, feed_dict={self.X_in: X})
def predict(self, X):
# accepts and returns a real numpy array
# unlike forward_hidden and forward_output
# which deal with tensorflow variables
return self.session.run(self.X_hat, feed_dict={self.X_in: X})
def forward_hidden(self, X):
Z = tf.nn.sigmoid(tf.matmul(X, self.W) + self.bh)
return Z
def forward_logits(self, X):
Z = self.forward_hidden(X)
return tf.matmul(Z, tf.transpose(self.W)) + self.bo
def forward_output(self, X):
return tf.nn.sigmoid(self.forward_logits(X))
class DNN(object):
def __init__(self, D, hidden_layer_sizes, K, UnsupervisedModel=AutoEncoder):
self.hidden_layers = []
count = 0
input_size = D
for output_size in hidden_layer_sizes:
ae = UnsupervisedModel(input_size, output_size, count)
self.hidden_layers.append(ae)
count += 1
input_size = output_size
self.build_final_layer(D, hidden_layer_sizes[-1], K)
def set_session(self, session):
self.session = session
for layer in self.hidden_layers:
layer.set_session(session)
def build_final_layer(self, D, M, K):
# initialize logistic regression layer
self.W = tf.Variable(tf.random_normal(shape=(M, K)))
self.b = tf.Variable(np.zeros(K).astype(np.float32))
self.X = tf.placeholder(tf.float32, shape=(None, D))
labels = tf.placeholder(tf.int32, shape=(None,))
self.Y = labels
logits = self.forward(self.X)
self.cost = tf.reduce_mean(
tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits,
labels=labels
)
)
self.train_op = tf.train.AdamOptimizer(1e-2).minimize(self.cost)
self.prediction = tf.argmax(logits, 1)
def fit(self, X, Y, Xtest, Ytest, pretrain=True, epochs=1, batch_sz=100):
N = len(X)
# greedy layer-wise training of autoencoders
pretrain_epochs = 1
if not pretrain:
pretrain_epochs = 0
current_input = X
for ae in self.hidden_layers:
ae.fit(current_input, epochs=pretrain_epochs)
# create current_input for the next layer
current_input = ae.transform(current_input)
n_batches = N // batch_sz
costs = []
print("supervised training...")
for i in range(epochs):
print("epoch:", i)
X, Y = shuffle(X, Y)
for j in range(n_batches):
Xbatch = X[j*batch_sz:(j*batch_sz + batch_sz)]
Ybatch = Y[j*batch_sz:(j*batch_sz + batch_sz)]
self.session.run(
self.train_op,
feed_dict={self.X: Xbatch, self.Y: Ybatch}
)
c, p = self.session.run(
(self.cost, self.prediction),
feed_dict={self.X: Xtest, self.Y: Ytest
})
error = error_rate(p, Ytest)
if j % 10 == 0:
print("j / n_batches:", j, "/", n_batches, "cost:", c, "error:", error)
costs.append(c)
plt.plot(costs)
plt.show()
def forward(self, X):
current_input = X
for ae in self.hidden_layers:
Z = ae.forward_hidden(current_input)
current_input = Z
# logistic layer
logits = tf.matmul(current_input, self.W) + self.b
return logits
def test_pretraining_dnn():
Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST()
# dnn = DNN([1000, 750, 500])
# dnn.fit(Xtrain, Ytrain, Xtest, Ytest, epochs=3)
# vs
Xtrain = Xtrain.astype(np.float32)
Xtest = Xtest.astype(np.float32)
_, D = Xtrain.shape
K = len(set(Ytrain))
dnn = DNN(D, [1000, 750, 500], K)
init_op = tf.global_variables_initializer()
with tf.Session() as session:
session.run(init_op)
dnn.set_session(session)
dnn.fit(Xtrain, Ytrain, Xtest, Ytest, pretrain=True, epochs=10)
def test_single_autoencoder():
Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST()
Xtrain = Xtrain.astype(np.float32)
Xtest = Xtest.astype(np.float32)
_, D = Xtrain.shape
autoencoder = AutoEncoder(D, 300, 0)
init_op = tf.global_variables_initializer()
with tf.Session() as session:
session.run(init_op)
autoencoder.set_session(session)
autoencoder.fit(Xtrain, show_fig=True)
done = False
while not done:
i = np.random.choice(len(Xtest))
x = Xtest[i]
y = autoencoder.predict([x])
plt.subplot(1,2,1)
plt.imshow(x.reshape(28,28), cmap='gray')
plt.title('Original')
plt.subplot(1,2,2)
plt.imshow(y.reshape(28,28), cmap='gray')
plt.title('Reconstructed')
plt.show()
ans = input("Generate another?")
if ans and ans[0] in ('n' or 'N'):
done = True
if __name__ == '__main__':
# test_single_autoencoder()
test_pretraining_dnn()