This tasks the model to find weights that efficiently (through finding patterns in the data) represent data in such a manner that it can effectively compress and reconstruct the data
import sys
import warnings
if not sys.warnoptions:
warnings.simplefilter("ignore")
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, Dense, MaxPool2D, Dropout, Flatten, Reshape, Conv2DTranspose
from tensorflow.keras import models
from sklearn.datasets import load_digits
from sklearn.metrics import mean_squared_error
%matplotlib inline
data = load_digits()
X_train = data.images[:1500] / 16
y_train = data.target[:1500]
X_test = data.images[1500:] / 16
y_test = data.target[1500:]
plt.imshow(X_train[0], cmap=plt.cm.gray_r, interpolation='nearest')
plt.title('Label: ' + str(y_train[0]))
plt.show()
data.images[0].shape
model = Sequential()
model.add(Flatten(input_shape = (8, 8)))
model.add(Dense(10, activation='relu'))
model.add(Dense(64, activation='linear'))
model.add(Reshape(target_shape=(8,8)))
model.compile('adam', loss='mse')
model.summary()
history = model.fit(X_train, X_train, batch_size=64, epochs=128, verbose=0, validation_split=0.1)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
pred_X_test = model.predict(X_test)
for i in range(5):
fig, axs = plt.subplots(1, 2)
# actual test example
axs[0].imshow(X_test[i], cmap=plt.cm.gray_r, interpolation='nearest')
axs[0].title.set_text('Actual example of a ' + str(y_test[i]))
# predicted test example
axs[1].imshow(pred_X_test[i].reshape((8, 8)), cmap=plt.cm.gray_r, interpolation='nearest')
axs[1].title.set_text('Reconstruction')
plt.show()
print('Reconstruction MSE for the above example was:',
mean_squared_error(
X_test[i].reshape((8, 8)),
pred_X_test[i].reshape((8, 8))
)
)
# examples are currently 8x8 = 64 data points
model = Sequential()
model.add(Conv2D(filters=4, kernel_size=(4,4), padding='valid', activation='relu', input_shape = (8, 8, 1)))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Conv2DTranspose(filters=1, kernel_size=(7,7), activation='linear'))
model.compile('adam', loss='mse')
model.summary()
history = model.fit(X_train.reshape(-1,8,8,1), X_train.reshape(-1,8,8,1), batch_size=64, epochs=256, verbose=0, validation_split=0.1)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
pred_X_test = model.predict(X_test.reshape(-1, 8, 8, 1))
for i in range(5):
fig, axs = plt.subplots(1, 2)
# actual test example
axs[0].imshow(X_test[i], cmap=plt.cm.gray_r, interpolation='nearest')
axs[0].title.set_text('Actual example of a ' + str(y_test[i]))
# predicted test example
axs[1].imshow(pred_X_test[i].reshape((8, 8)), cmap=plt.cm.gray_r, interpolation='nearest')
axs[1].title.set_text('Reconstruction')
plt.show()
print('Reconstruction MSE for the above example was:',
mean_squared_error(
X_test[i].reshape((8, 8)),
pred_X_test[i].reshape((8, 8))
)
)
n_layers = 2
layer_outputs = [layer.output for layer in model.layers[:n_layers]]
activation_model = models.Model(
inputs = model.input,
outputs=layer_outputs
)
activations = activation_model.predict(X_test.reshape(-1, 8, 8, 1))
for i in range(100, 103):
plt.imshow(X_test[i], cmap=plt.cm.gray_r, interpolation='nearest')
plt.title('Actual data ' + str(y_test[i]))
plt.show()
print('\nOutput of the convolutional layer:\n')
fig, axs = plt.subplots(2, 2)
axs[0,0].imshow(activations[0][i,:,:,0].reshape(5,5), cmap=plt.cm.gray_r, interpolation='nearest')
axs[0,1].imshow(activations[0][i,:,:,1].reshape(5,5), cmap=plt.cm.gray_r, interpolation='nearest')
axs[1,0].imshow(activations[0][i,:,:,2].reshape(5,5), cmap=plt.cm.gray_r, interpolation='nearest')
axs[1,1].imshow(activations[0][i,:,:,3].reshape(5,5), cmap=plt.cm.gray_r, interpolation='nearest')
plt.show()
print('\nOutput of the max pooling layer:\n')
fig, axs = plt.subplots(2, 2)
axs[0,0].imshow(activations[1][i,:,:,0].reshape(2,2), cmap=plt.cm.gray_r, interpolation='nearest')
axs[0,1].imshow(activations[1][i,:,:,1].reshape(2,2), cmap=plt.cm.gray_r, interpolation='nearest')
axs[1,0].imshow(activations[1][i,:,:,2].reshape(2,2), cmap=plt.cm.gray_r, interpolation='nearest')
axs[1,1].imshow(activations[1][i,:,:,3].reshape(2,2), cmap=plt.cm.gray_r, interpolation='nearest')
plt.show()
# predicted test example
plt.imshow(pred_X_test[i].reshape(8,8), cmap=plt.cm.gray_r, interpolation='nearest')
plt.title('Attempted reconstruction of the above example')
plt.show()