Exercise 6. Add expected data range and normalize().

Input normalization

In a perfect world, we would be able to use our autoencoder with any image, regardless of its size and format. That's a pretty bold aspiration, but there are a couple of things we can do to approach it. The first we have already done. In run_framework.py we calculated the number of elements in the input and output layers , based on the properties of the image data set. This means that our autoencoder will automatically be the right size for the number of pixels we feed it.

A second step we can take is to make sure all our pixel values fall within the same range. When representing a pixel's value in a 2D array, we can use values from 0 to 1, from -1 to 1, from 0 to 255. from 0 to 65536, or any number of other arrangements. The interpretation is the same each time. The bottom of the range is the darkest possible value for the pixel and the top of the range is the brightest. The image doesn't care. Unfortunately neural networks do. They are sensitive to the range of the inputs. Not only do all the images need to have values within the same range, but anecdotal experience dictates that for best results that range should be somewhere in the neighborhood between -1 and 1.

For our autoencoder, we'll drive the inputs so that they all fall on or between -.5 and .5 (in math speak, on [-.5, .5]). This gives the autoencoder room to maneuver.

Coding challenge

  • In run_framework.py declare the pixel value range for the data set. It's OK to hard code this. Pass it in during the creation of ANN.
  • In framework.py write a normalize() function. Use the input range you provided it to transform any set of input pixel values to fall on [-.5, .5].
  • In both train() and evaluate(), add in a normalization step to preprocess the inputs.

My solution

Here is all the code we've written up to this point.

Complete and Continue  
Discussion

9 comments