Autoencoders — Escape the curse of dimensionality.

Autoencoders falls under the class of unsupervised learning where the Function tries to mimic itself with some constraints such as pushing the input towards the bottleneck such that it just learns enough significant features of the input data to reconstruct it back with minimal loss

Image for post
Image for post

Audience

People who are new to the space of deep learning

Prerequisite

Basic understanding of convolutional neural networks

Auto-encoders have three components

First the encoding unit

Second the latent space

Third the decoder unit

In the encoder part, the image is loosing its free dimensions and tries to learn a significant part of the underlying data.

The latent space is the bottleneck layer when the whole image is compressed and represented in minimal dimensions. In the below example conv2d_3 is the bottleneck layer

The decoder unit tries to reconstruct the image which it has learned in the previous layers using upsampling.

Installing Dependencies

Keras is an open-source neural-network library written in Python. It is capable of running on top of TensorFlow, Microsoft Cognitive Toolkit, Theano, or PlaidML. Designed to enable fast experimentation with deep neural networks, it focuses on being user-friendly, modular, and extensible

NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like Tkinter, wxPython, Qt, or GTK+

The acutal coding starts here

Importing Libraries

Loads the training and test data sets ignoring class labels since we are using autoencoder we don't need the class labels

Normalization of the input data between to scale it between 0 and 1

Dimension of train and test data

Output: (10000, 28, 28) (60000, 28, 28)

Changing the train and test data to a 4-dimensional tensor as keras expects 4-Dimensional tensor as input

The first dimension is for the number of images

The Second and third is for the width and height for the image

The fourth dimension is for the number of channels

Output: (10000, 28, 28,1) (60000, 28, 28,1)

Defining the Dimension of the image using Input function in keras

Architecture of Encoder

ReLU layer will apply the function f(x)=max(0,x) in all elements on an input tensor, without changing it’s spatial or depth information and brings nonlinearity to the networks

Architecture of Decoder

For the decoder, we use Upsampling from keras instead, as we have to reconstruct the image to its original dimensions.

Defining the model

Compiling and Fitting the model

As this is a regression problem I choose to use mse-error as my loss function and Adam is the optimizer most commonly used

Image for post
Image for post

In conclusion, autoencoder is forced to form a representation at the intermediate hidden layer that has a smaller number of variables than the input. This forces the autoencoder to keep only the components that are useful for reconstructing the common features of the inputs and to reject any components that are not common features. As a result, an autoencoder will tend to learn a representation in the hidden layer that rejects noise from the input.

About me

I am an intern at Wavelabs.ai. We at Wavelabs help you leverage Artificial Intelligence (AI) to revolutionize user experiences and reduce costs. We uniquely enhance your products using AI to reach your full market potential. We try to bring cutting edge research into your applications. Have a look at us.

You can reach me out at LinkedIn

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store