Browsing Tag

layers

b13
Deep Learning, Machine Learning, Python,

Basic Example of a Neural Network with TensorFlow and Keras

This blog post covers basic example of a Neural Network, using TensorFlow and Keras in Python. The notebook can be also viewed on Github.

TensorFlow and Keras

TensorFlow was developed at Google to use internally for machine learning tasks, and applied to the applications like speech recognition, Search, Gmail, etc. It was made public in 2015 as an open source application. The library is in C++, used with Python API. TensorFlow can be used for various problems like image recognition, language processing, implementation in self-driving cars, etc. There are various alternatives available to TensorFlow such as Theano, and Torch.

We are going to use Keras in this notebook, with Tensorflow as a backend engine. Keras is a high-level wrapper, which can be used both with TensorFlow and Theano. It simplifies common operations. The code is similar to scikit-learn, making it easier to get used to it, while in the background TensorFlow or Theano is used for processing.

The data

In this example we will be looking at MNIST database (a subset of a larger set by National Institute of Standards and Technology). This is a classic dataset containing 60000 training images, 10000 test images, and corresponding training and test labels. The images are handwritten digits, in the shape of 28 x 28 pixels, and divided into 10 categories (from 0 to 9).

The versions

In this example I am using Keras v.2.1.4 and TensorFlow v.1.5.0 with GPU (using NVIDIA CUDA). Running examples on a GPU can speed up the training process.

In [1]:
# To avoid warnings
import warnings
warnings.filterwarnings('ignore')

# Importing keras and tensorflow, and printing the versions
import keras
print('Keras: {}'.format(keras.__version__))

import tensorflow as tf
print('TensorFlow: {}'.format(tf.__version__))
Using TensorFlow backend.
Keras: 2.1.4
TensorFlow: 1.5.0