Last active
April 25, 2021 18:11
-
-
Save MinaGabriel/86b75944d24d11dc81e899c3b65dcfbd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.datasets import mnist | |
from keras.layers import Input, Dense | |
from keras.models import Model | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
(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:])) | |
print(X_train.shape) | |
print(X_test.shape) | |
input_img= Input(shape=(784,)) | |
encoded = Dense(units=128, activation='relu')(input_img) | |
encoded = Dense(units=64, activation='relu')(encoded) | |
encoded = Dense(units=32, activation='relu')(encoded) | |
decoded = Dense(units=64, activation='relu')(encoded) | |
decoded = Dense(units=128, activation='relu')(decoded) | |
decoded = Dense(units=784, activation='sigmoid')(decoded) | |
autoencoder=Model(input_img, decoded) | |
encoder = Model(input_img, encoded) | |
autoencoder.summary() | |
encoder.summary() | |
autoencoder.compile(optimizer='adam', loss='mse', metrics=['accuracy']) | |
autoencoder.fit(X_train, X_train, | |
epochs=50, | |
batch_size=256, | |
shuffle=True, | |
validation_data=(X_test, X_test)) | |
encoded_imgs = encoder.predict(X_test) | |
predicted = autoencoder.predict(X_test) | |
plt.figure(figsize=(40, 4)) | |
for i in range(10): | |
# display original images | |
ax = plt.subplot(3, 20, i + 1) | |
plt.imshow(X_test[i].reshape(28, 28)) | |
plt.gray() | |
ax.get_xaxis().set_visible(False) | |
ax.get_yaxis().set_visible(False) | |
# display encoded images | |
ax = plt.subplot(3, 20, i + 1 + 20) | |
plt.imshow(encoded_imgs[i].reshape(8, 4)) | |
plt.gray() | |
ax.get_xaxis().set_visible(False) | |
ax.get_yaxis().set_visible(False) | |
# display reconstructed images | |
ax = plt.subplot(3, 20, 2 * 20 + i + 1) | |
plt.imshow(predicted[i].reshape(28, 28)) | |
plt.gray() | |
ax.get_xaxis().set_visible(False) | |
ax.get_yaxis().set_visible(False) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment