DL_0801CS223D04_Assignment5.ipynb - Colab
DL_0801CS223D04_Assignment5.ipynb - Colab
# Loading the dataset MNIST. This is a dataset of 60,000 28x28 grayscale images of the 10 d
# along with a test set of 10,000 images.
# Loads the MNIST dataset. Returns the (train subset) and (test subset)
# Each subset consists of (input features, outputs)
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# The shape is (28, 28), that is, the height and width of the image is 28 and 28 respective
# Morever, note that it is a grayscale image. Will discuss more in the upcoming classes
# Syntax
# ======
# array = numpy.reshape(array, newshape)
# Gives a new shape to an array without changing its data.
# [or]
# array.reshape(newshape)
# for example: arr.reshape((5,5))
keyboard_arrow_down Single-layer
The simplest possible autoencoder is a single hidden layer of n < # of input pixels nodes. The
output layer of this model mirrors the input layer in size. In the next few code cells we train just such
a simple autoencoder and demonstrate its output.
# This is the size of our encoded representations. It is also called as bottleneck size.
encoding_dim = 32 # This is a hyperparamter. Lower value means higher compression.
# 784 -> 32 (compression of factor 784/32 = 24.5, assuming the input is 784).
# Assignment 1: Figure out why the values assigned to shape is (784,) and not simply (784)?
# Creates the first layer of the network (which will be the encoder in this case)
# Dense() creates a densely-connected NN layer.
# Creates the second layer of the network (which will be the decoder in this case)
# Note that the activation here 'sigmoid'. This is because we want the output to be in the
decoded = Dense(784, activation='sigmoid')(encoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
# Arguments
# ========
# x: Input data.
# y: Target data.
# batch_size: Number of samples per gradient update.
# epochs: Number of epochs to train the model. An epoch is an iteration over the entire x an
# validation_data: Data on which to evaluate the loss and any model metrics at the end of ea
# verbose: Verbosity mode. 0 = silent, 1 = progress bar, 2 = one line per epoch.
# Returns
# ========
# A History object. Its History.history attribute is a record of training loss values and me
# as well as validation loss values and validation metrics values (if applicable).
history_100 = autoencoder.fit(x=x_train,
y=x_train,
epochs=100,
batch_size=512,
validation_data=(x_test, x_test),
verbose=1)
# Note that the x, y are same (i.e., x_train) in autoencoder, as the input and output of the
Epoch 97/100
118/118 ━━━━━━━━━━━━━━━━━━━━ 2s 16ms/step - loss: 0.6843 - val_loss: 0.6840
Epoch 98/100
118/118 ━━━━━━━━━━━━━━━━━━━━ 2s 16ms/step - loss: 0.6841 - val_loss: 0.6839
Epoch 99/100
118/118 ━━━━━━━━━━━━━━━━━━━━ 2s 17ms/step - loss: 0.6840 - val_loss: 0.6837
Epoch 100/100
118/118 ━━━━━━━━━━━━━━━━━━━━ 3s 22ms/step - loss: 0.6838 - val loss: 0.6836
# History object.
# History.history returns a dictionary
dict_keys(['loss', 'val_loss'])
# Access the values of training loss at every epoch using the key 'loss'
print(history_100.history['loss'])
print("\nlength of the list: ", len(history_100.history['loss']), ", which is equal to the n
decoded_imgs = autoencoder.predict(x_test)
plt.show()
We see here that the decoder trained in our autoencoder learns grainy (lossy) versions of the
original images.
This form of simple one-layer autoencoder learns a representation of the underlying dataset that is
very close to what is learned by PCA. You can thus think of this simple autoencoder as being a kind
of stochastic approximation of the more deterministic PCA algorithm.
These have all the benefits and tradeoffs we would expect of increasing the number of layers in our
neural networks, namely: longer training times and less easily decoded representations, but more
accurate reconstructions.
A good default choice for a deep autoencoder is to scale the image down progressively, than scale
it back up again, using the same number of nodes on each layer on either side of the "most
compressed" representation at the center of the hidden layers.
Epoch 1/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 5s 29ms/step - loss: 0.4094 - val_loss: 0.2038
Epoch 2/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 6s 35ms/step - loss: 0.1876 - val_loss: 0.1539
Epoch 3/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 4s 31ms/step - loss: 0.1505 - val_loss: 0.1371
Epoch 4/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 5s 27ms/step - loss: 0.1364 - val_loss: 0.1292
Epoch 5/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 7s 40ms/step - loss: 0.1289 - val_loss: 0.1235
Epoch 6/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 5s 39ms/step - loss: 0.1235 - val_loss: 0.1189
Epoch 7/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 4s 27ms/step - loss: 0.1196 - val_loss: 0.1154
Epoch 8/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 6s 35ms/step - loss: 0.1159 - val_loss: 0.1125
Epoch 9/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 3s 27ms/step - loss: 0.1133 - val_loss: 0.1100
Epoch 10/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 5s 27ms/step - loss: 0.1106 - val_loss: 0.1077
Epoch 11/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 4s 38ms/step - loss: 0.1084 - val_loss: 0.1053
Epoch 12/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 4s 27ms/step - loss: 0.1063 - val_loss: 0.1043
Epoch 13/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 5s 27ms/step - loss: 0.1050 - val_loss: 0.1025
Epoch 14/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 5s 39ms/step - loss: 0.1035 - val_loss: 0.1016
Epoch 15/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 3s 27ms/step - loss: 0.1026 - val_loss: 0.1011
Epoch 16/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 5s 27ms/step - loss: 0.1018 - val_loss: 0.1007
Epoch 17/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 4s 37ms/step - loss: 0.1008 - val_loss: 0.0992
Epoch 18/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 3s 28ms/step - loss: 0.0999 - val_loss: 0.0988
Epoch 19/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 5s 26ms/step - loss: 0.0992 - val_loss: 0.0975
Epoch 20/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 4s 30ms/step - loss: 0.0984 - val_loss: 0.0973
Epoch 21/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 4s 35ms/step - loss: 0.0980 - val_loss: 0.0966
Epoch 22/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 4s 27ms/step - loss: 0.0969 - val_loss: 0.0955
Epoch 23/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 5s 30ms/step - loss: 0.0967 - val_loss: 0.0952
Epoch 24/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 4s 35ms/step - loss: 0.0960 - val_loss: 0.0949
Epoch 25/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 4s 27ms/step - loss: 0.0954 - val_loss: 0.0940
Epoch 26/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 5s 28ms/step - loss: 0.0949 - val_loss: 0.0939
Epoch 27/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 4s 37ms/step - loss: 0.0944 - val_loss: 0.0935
Epoch 28/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 3s 26ms/step - loss: 0.0939 - val_loss: 0.0932
Epoch 29/200
118/118 ━━━━━━━━━━━━━━━━━━━━ 3s 26ms/step - loss: 0 0939 - val loss: 0 0924
plt.plot(history_200.history['loss'])
plt.plot(history_200.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()
Design a Deep auto encoders and use the following optimizer and explore other loss function such
as hinge loss etc.
keyboard_arrow_down Convolutional autoencoder
Since our inputs are images, it makes sense to use convolutional neural networks (convnets) as
encoders and decoders. In practical settings, autoencoders applied to images are always
convolutional autoencoders since they simply perform much better.
Let's implement one. The encoder will consist in a stack of Conv2D and MaxPooling2D layers (max
pooling being used for spatial down-sampling), while the decoder will consist in a stack of Conv2D
and UpSampling2D layers.
import keras
from keras import layers
Epoch 1/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 72s 288ms/step - loss: 0.3514 - val_loss: 0.1655
Epoch 2/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 69s 293ms/step - loss: 0.1591 - val_loss: 0.1429
Epoch 3/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 83s 299ms/step - loss: 0.1399 - val_loss: 0.1317
Epoch 4/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 70s 297ms/step - loss: 0.1309 - val_loss: 0.1259
Epoch 5/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 81s 292ms/step - loss: 0.1254 - val_loss: 0.1212
Epoch 6/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 84s 301ms/step - loss: 0.1214 - val_loss: 0.1180
Epoch 7/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 82s 302ms/step - loss: 0.1185 - val_loss: 0.1155
Epoch 8/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 80s 295ms/step - loss: 0.1162 - val_loss: 0.1142
Epoch 9/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 72s 309ms/step - loss: 0.1141 - val_loss: 0.1115
Epoch 10/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 81s 304ms/step - loss: 0.1123 - val_loss: 0.1100
Epoch 11/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 84s 312ms/step - loss: 0.1108 - val_loss: 0.1087
Epoch 12/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 71s 303ms/step - loss: 0.1097 - val_loss: 0.1075
Epoch 13/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 70s 299ms/step - loss: 0.1084 - val_loss: 0.1064
Epoch 14/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 83s 303ms/step - loss: 0.1077 - val_loss: 0.1061
Epoch 15/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 81s 299ms/step - loss: 0.1066 - val_loss: 0.1047
Epoch 16/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 81s 293ms/step - loss: 0.1056 - val_loss: 0.1040
Epoch 17/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 83s 299ms/step - loss: 0.1049 - val_loss: 0.1039
Epoch 18/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 81s 296ms/step - loss: 0.1043 - val_loss: 0.1027
Epoch 19/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 74s 314ms/step - loss: 0.1038 - val_loss: 0.1022
Epoch 20/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 77s 294ms/step - loss: 0.1034 - val_loss: 0.1017
Epoch 21/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 82s 295ms/step - loss: 0.1027 - val_loss: 0.1015
Epoch 22/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 84s 302ms/step - loss: 0.1023 - val_loss: 0.1008
Epoch 23/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 84s 311ms/step - loss: 0.1017 - val_loss: 0.1006
Epoch 24/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 78s 294ms/step - loss: 0.1013 - val_loss: 0.1000
Epoch 25/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 85s 305ms/step - loss: 0.1012 - val_loss: 0.1000
Epoch 26/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 80s 297ms/step - loss: 0.1007 - val_loss: 0.0993
Epoch 27/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 84s 306ms/step - loss: 0.1002 - val_loss: 0.0992
Epoch 28/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 70s 299ms/step - loss: 0.0999 - val_loss: 0.0987
Epoch 29/100
235/235 ━━━━━━━━━━━━━━━━━━━━ 83s 305ms/step - loss: 0.0998 - val loss: 0.0985
plt.plot(history_100.history['loss'])
plt.plot(history_100.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()
UNDERCOMPLETE AUTOENCODERS
# Undercomplete Autoencoder