-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathae_mnist_relu_adadelta.py
80 lines (62 loc) · 2.34 KB
/
ae_mnist_relu_adadelta.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
import numpy as np
from keraspp.skeras import plot_history
import matplotlib.pyplot as plt
from keras.datasets import mnist
from keras import layers, models
class AE(models.Model):
def __init__(self, x_nodes=784, z_dim=36):
x_shape = (x_nodes,)
self.x = layers.Input(shape=x_shape)
self.z = layers.Dense(z_dim, activation='relu')(self.x)
self.y = layers.Dense(x_nodes, activation='sigmoid')(self.z)
super().__init__(self.x, self.y)
self.z_dim = z_dim
print("Compiling model...")
self.compile(optimizer='adadelta', loss='binary_crossentropy', metrics=['accuracy'])
def Encoder(self):
return models.Model(self.x, self.z)
def Decoder(self):
z_shape = (self.z_dim,)
z = layers.Input(shape=z_shape)
y_layer = self.layers[-1]
y = y_layer(z)
return models.Model(z, y)
def show_ae(autoencoder):
encoder = autoencoder.Encoder()
decoder = autoencoder.Decoder()
encoded_imgs = encoder.predict(X_test)
decoded_imgs = decoder.predict(encoded_imgs)
n = 10
plt.figure(figsize=(20, 6), dpi=100)
for i in range(n):
ax = plt.subplot(3, n, i + 1)
plt.imshow(X_test[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax = plt.subplot(3, n, i + 1 + n)
plt.stem(encoded_imgs[i].reshape(-1))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax = plt.subplot(3, n, i + 1 + n + n)
plt.imshow(decoded_imgs[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
if __name__ == '__main__':
(X_train, _), (X_test, _) = mnist.load_data()
X_train = X_train.astype('float32') / 255.
X_test = X_test.astype('float32') / 255.
X_train = X_train.reshape((len(X_train), np.prod(X_train.shape[1:])))
X_test = X_test.reshape((len(X_test), np.prod(X_test.shape[1:])))
x_nodes = 784
z_dim = 36
autoencoder = AE(x_nodes, z_dim)
history = autoencoder.fit(X_train, X_train,
epochs=10, batch_size=256,
shuffle=True, validation_data=(X_test, X_test))
plot_history(history)
show_ae(autoencoder)
plt.show()