Neural Networks in Python with Keras and TensorFlow

In the advent of distributed computing and GPUs and the release of TensorFlow, machine learning and deep learning solutions are becoming more accessible to the ordinary developer.

With the help of some tutorials on Lynda.com, I set off to learn how to create a neural network of my own using a Python package called Keras. Keras provides a high-level interface which makes it easy to build, train, test, and use neural networks.

In addition to providing model creation abilities, Keras also provides example datasets, like the MNIST database of hand-written numbers, which I used for my first model. The goal of this model was to process any image from the dataset and predict which digit (0-9) the image represents.

Keras provides an easy way to load this dataset, and split it into different batches for training and testing purposes, respectively.

While developing the model, I learned how important it is to pre-process the data into a format the model expects. For this specific MNIST use case, each input image represents a grid of pixels, each with its own grayscale value from 0 to 255. We need to re-scale these values so they fall between 0 and 1 (the model’s preferred input range), and we need to flatten each input from a two-dimensional grid into a one-dimensional list. Finally, we need to separate the output values into categories representing each of the ten possible outcomes (digits from zero to nine).

# flatten each 28x28 pixel grid into a single layer
h,w=28,28x_train=x_train.reshape(60000,h*w)#> 6000 entries of 784 size items
x_test=x_test.reshape(10000,h*w)#> 10000 entries of 784 size items
# re-scale numbers from between 0 and 255 to between 0 and 1
x_train=x_train.astype("float32")x_test=x_test.astype("float32")x_train=x_train/255.0x_test=x_test/255.0# separate each output number into one of ten bins (digits 0-9)
fromkeras.utils.np_utilsimportto_categoricaly_train=to_categorical(y_train,10)y_test=to_categorical(y_test,10)