Difference between revisions of "Tutotrial 1"

From Quantitative Analysis Software Courses
Jump to navigation Jump to search
Line 34: Line 34:
 
input_img = Input(shape=(784,))
 
input_img = Input(shape=(784,))
 
</PRE>
 
</PRE>
Now we have an input layer with 784 neurons.
+
Now we have an input layer with 784 neurons. Now we proceed to construct the hidden layers.
 +
 
 +
Let us choose the architecture where from the input layer, the autoencoder gradually decreases in neurons through 128, 64 and at the "waist" of the autoencoder, we have 32 neurons. Since we also need to decode the image from 32 pixels back to 784, let it progress through the same number of neurons. Thus, the architecture is <tt>784 ---> 128 ---> 64 ---> 32(Waist) ---> 64 ---> 128 ---> 784</tt>. The dense function takes as input the number of neurons in the new layer, the activation function for all the neurons and accounts for what the previous layer was.
 +
 
 +
<PRE>
 +
encoded = Dense(128, activation='relu')(input_img)
 +
encoded = Dense(64, activation='relu')(encoded)
 +
encoded = Dense(32, activation='relu')(encoded)
 +
 
 +
decoded = Dense(64, activation='relu')(encoded)
 +
decoded = Dense(128, activation='relu')(decoded)
 +
decoded = Dense(784, activation='sigmoid')(decoded)
 +
</PRE>

Revision as of 12:40, 7 February 2018

Autoencoders using Keras

This is a tutorial for developing a basic autoencoder using python, and keras. The tutorial mentioned on this page follows from: https://blog.keras.io/building-autoencoders-in-keras.html

Overview

(From the link mentioned above:)"Autoencoding" is a data compression algorithm where the compression and decompression functions are

  • Data-specific
  • Lossy, and
  • Learned automatically from examples rather than engineered by a human

Additionally, in almost all contexts where the term "autoencoder" is used, the compression and decompression functions are implemented with neural networks. This tutorial will focus on building two types of Autoencoders,

  • A deep autoencoder
  • A convolutional autoencoder

Deep autoencoder for grayscale images

In order to build a deep autoencoder for grayscale images, here are the steps we should be following:

  • Import required packages
  • Create the structure of the autoencoder
  • Import data and fit it to our newly built autoencoder
  • Plot our test data

Importing the required packages

This tutorial only requires one package and that is keras. From keras, we will be importing the Input, Dense and the Model modules.

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

The Input function takes a shape tuple as it's argument and creates a tensor based on that. The Dense function is used to specify a layer of neurons, and the activation function they posses. The Model function combines different layers and allows us to address a couple of layers together, as a model.

Creating the structure of the Autoencoder

We will be using the aforementioned Dense and Model functions to create the structure of the Neural net. Let us create an autoencoder for the MNIST dataset (http://yann.lecun.com/exdb/mnist/). This is a dataset of thounsands of 28x28 grayscale pictures of handwritten digits. Since we have 28x28 pixels, our autoencoder must have 784 neurons in it's input layer.

input_img = Input(shape=(784,))

Now we have an input layer with 784 neurons. Now we proceed to construct the hidden layers.

Let us choose the architecture where from the input layer, the autoencoder gradually decreases in neurons through 128, 64 and at the "waist" of the autoencoder, we have 32 neurons. Since we also need to decode the image from 32 pixels back to 784, let it progress through the same number of neurons. Thus, the architecture is 784 ---> 128 ---> 64 ---> 32(Waist) ---> 64 ---> 128 ---> 784. The dense function takes as input the number of neurons in the new layer, the activation function for all the neurons and accounts for what the previous layer was.

encoded = Dense(128, activation='relu')(input_img)
encoded = Dense(64, activation='relu')(encoded)
encoded = Dense(32, activation='relu')(encoded)

decoded = Dense(64, activation='relu')(encoded)
decoded = Dense(128, activation='relu')(decoded)
decoded = Dense(784, activation='sigmoid')(decoded)