# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import tensorflow as tf

from keras.datasets import mnist
from keras.models import Model
from keras.layers import Dense, Input

import matplotlib.pyplot as plt
# -

(X_train, y_train),(X_test, y_test) = mnist.load_data()
X_train.shape

# Visulaize
plt.imshow(X_train[0])

plt.imshow(X_test[0])

# convert into vector
X_train = X_train.reshape(X_train.shape[0], 28*28)
X_test = X_test.reshape(X_test.shape[0], 28*28)
X_train.shape

# rescale
X_train.max()

X_train = X_train / 255.0
X_test = X_test / 255.0
X_train.max()

# # Model

input_layer = Input(shape = (X_train.shape[1]))
bottleneck = Dense(32, activation = 'relu', name = 'bottleneck')(input_layer)
output = Dense(X_train.shape[1], activation = 'sigmoid', name = 'output')(bottleneck)

autoencoder = Model(input_layer, output)
autoencoder.summary()

encoder = Model(input_layer, bottleneck)
encoder.summary()

encoded_layer = Input(shape = (32,), name = 'encoded_layer')
decoder_layer = autoencoder.layers[-1]
decoder = Model(encoded_layer, decoder_layer(encoded_layer))
decoder.summary()

autoencoder.compile(loss = 'mse', optimizer = 'adam')
history = autoencoder.fit(X_train, X_train, epochs=20, batch_size = 128)

plt.plot(history.history['loss'])

encoded_data = encoder.predict(X_test, batch_size=512)
encoded_data.shape

output_data = decoder.predict(encoded_data, batch_size=512)
output_data.shape

output_data = output_data.reshape(output_data.shape[0], 28,28)
output_data.shape

plt.imshow(output_data[0], cmap ='gray')

X_test = X_test.reshape(10000, 28,28)
X_test.shape

plt.imshow(X_test[0], cmap = 'gray')



StarcoderdataJupyterScriptsDedupFiltered