<jupyter_start><jupyter_text>AutoEncoders to regenerate images by <NAME>(122021601001)<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf


from tensorflow.keras import layers, losses
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.models import Model
(x_train, _), (x_test, _) = fashion_mnist.load_data() # declaring a variable with ( _ ) as we dont require labels from dataset ( _ )is a dummy variable

x_train = x_train.astype('float32') / 255.            # type casting the data and rescaling it will 255 to 0-1 values
x_test = x_test.astype('float32') / 255.

print(x_train.shape)
print(x_test.shape)
latent_dim = 64                                                                 # input dimension for the model

class Autoencoder(Model):                                                       # creating a class for Autoencoder
  def __init__(self, latent_dim):                                               # initilizing the class with required objects of the model
    super(Autoencoder, self).__init__()                                         # creating a super class object to directly initilize the object of parent class 
    self.latent_dim = latent_dim                                                # creating aclass attribute for future reference and local utilization of variable
                                                                                # here the model is split into 2 pares of encoder and decoder for easy tuning and future proofing the model
    self.encoder = tf.keras.Sequential([
                                        layers.Flatten(),
                                        layers.Dense(latent_dim, activation='relu')
    ])                                                                          # creating an attribute to store encoder part of the model using the sequential api from keras 
                                                                                # flatern the input and then pass it to a dense node and output the data to decoder
    self.decoder = tf.keras.Sequential([
                                        layers.Dense(784, activation='sigmoid'),
                                        layers.Reshape((28,28))
    ])                                                                          # creating an attribute to store decoder part of the model using the sequential api from keras 
                                                                                # connect the input from encoder to the same no one nodes when flattern and reshape the data back to origiinal dimensions

  def call(self, x):                                                            # defining the class function of the class 
    encoded = self.encoder(x)                                                   # calling the encoder part and sending the input into it  ::: i/p(encoder) <- dataset
    decoded = self.decoder(encoded)                                             # calling the decoder part and sending the encoder output as input of decoder  :: i/p(decoder) <- o/p(encoder)
    return decoded                                                              # return only the decoder output as it contains the reconstructed part of the images

autoencoder = Autoencoder(latent_dim)                                           # initilizing the class object
autoencoder.compile(optimizer='adam', loss=losses.MeanSquaredError())           # compiling the model with required loss function and optimizer
# here we train the model with same data for input and target as we need to reconstruct 
# the same image so X_train,Y_train = X_train and same follows for the validation set also

autoencoder.fit(x_train, x_train, epochs=10, shuffle=True, validation_data=(x_test, x_test))
# applying the model to new data
encoded_imgs = autoencoder.encoder(x_test).numpy()                              # calling the attributes of model to encode new data
decoded_imgs = autoencoder.decoder(encoded_imgs).numpy()                        # calling the attributes of model to decode new encoded data
# plotting the images for comparision between original and reconstructed data
n = 10                                                                          # plotting only first 10 images
plt.figure(figsize=(20, 4))                                                     # initilizing the figure size for plot
for i in range(n):
  ax = plt.subplot(2, n, i + 1)                                                 # creating subplot rules for 1st row in the plot of original images
  plt.imshow(x_test[i])                                                         # plotting the original images 
  plt.title("Original")
  plt.gray()                                                                    # The gray() function is used to set the colormap to “gray”.
  ax.get_xaxis().set_visible(False)                                             # removing the visiblity of grid lines in plot
  ax.get_yaxis().set_visible(False)

  ax = plt.subplot(2, n, i + 1 + n)                                             # creating subplot rules for 2nd row in the plot of original images
  plt.imshow(decoded_imgs[i])                                                   # plotting the original images
  plt.title("Reconstructed")
  plt.gray()                                                                    # The gray() function is used to set the colormap to “gray”.
  ax.get_xaxis().set_visible(False)                                             # removing the visiblity of grid lines in plot
  ax.get_yaxis().set_visible(False)

plt.show()

autoencoder.summary()                                                           # print the model summary to understand the layers present 
tf.keras.utils.plot_model(autoencoder.encoder, show_shapes=True)                # plotting the encoder part of the model 
tf.keras.utils.plot_model(autoencoder.decoder, show_shapes=True)                # plotting the decoder part of the model<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.05517013544763792, 'fraction_numerical': 0.008589362405021473, 'mean_word_length': 1.4249098918702443, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 66, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '580198', 'license': 'MIT', 'n_tokens_mistral': 1329, 'n_tokens_neox': 1234, 'n_words': 511, 'path': 'Exp_7_AutoEncoder.ipynb', 'repo_name': 'prudhvi-somisetty/DL-Lab', 'size': '6074'}