In [10]:
%matplotlib inline
%load_ext autoreload
%autoreload 2
from importlib import reload
In [8]:
###########################
# AE 모델링
###########################
from keras import layers, models # (Input, Dense), (Model)
class AE(models.Model):
def __init__(self, x_nodes=784, z_dim=36):
x_shape = (x_nodes,)
x = layers.Input(shape=x_shape)
z = layers.Dense(z_dim, activation='relu')(x)
y = layers.Dense(x_nodes, activation='sigmoid')(z)
super().__init__(x, y)
self.x = x
self.z = z
self.z_dim = z_dim
# Encoder, Decoder ??
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)
###########################
# 데이터 준비
###########################
from ann_mnist_cl import Data_func
###########################
# 학습 효과 분석
###########################
from ann_mnist_cl import plot_loss, plot_acc
import matplotlib.pyplot as plt
###########################
# AE 동작 확인
###########################
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))
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()
###########################
# 학습
###########################
def main(epochs=20):
x_nodes = 784
z_dim = 36
(X_train, Y_train), (X_test, Y_test) = Data_func()
autoencoder = AE(x_nodes, z_dim)
history = autoencoder.fit(x_train, x_train,
epochs=epochs,
batch_size=256,
shuffle=True,
validation_data=(x_test, x_test))
plot_acc(history)
plt.show()
plot_loss(history)
plt.show()
show_ae(autoencoder)
plt.show()
In [9]:
main(2)
In [11]:
epochs = 20
x_nodes = 784
z_dim = 36
(X_train, Y_train), (X_test, Y_test) = Data_func()
autoencoder = AE(x_nodes, z_dim)
history = autoencoder.fit(x_train, x_train,
epochs=epochs,
batch_size=256,
shuffle=True,
validation_data=(x_test, x_test))
plot_acc(history)
plt.show()
plot_loss(history)
plt.show()
show_ae(autoencoder)
plt.show()
In [ ]: