Learn practical skills, build real-world projects, and advance your career

Convolutional Neural Networks

Convolutional neural networks (CNNs) are a class of deep neural networks, most commonly used in computer vision applications.

Convolutional refers the network pre-processing data for you - traditionally this pre-processing was performed by data scientists. The neural network can learn how to do pre-processing itself by applying filters for things such as edge detection.

Step 1

In this exercise we will train a CNN to recognise handwritten digits, using the MNIST digit dataset.

This is a very common exercise and data set to learn from.

Let's start by loading our dataset and setting up our train, validation, and test sets.

Run the code below to import our required libraries and set up the graphing features.
# Run this!
import warnings
warnings.filterwarnings("ignore")
import tensorflow as tf
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Conv2D, Dense, Dropout, Flatten, MaxPooling2D
print('keras using %s backend'%keras.backend.backend())
import matplotlib.pyplot as graph
%matplotlib inline
graph.rcParams['figure.figsize'] = (15,5)
graph.rcParams["font.family"] = 'DejaVu Sans'
graph.rcParams["font.size"] = '12'
graph.rcParams['image.cmap'] = 'rainbow'
Using TensorFlow backend.
keras using tensorflow backend

In the cell below replace:

1. <addTrainX> with train_X
2. <addTrainY> with train_Y
3. <addValidX> with valid_X
4. <addValidY> with valid_Y
5. <addTextX> with test_X
6. <addTextY> with test_Y
and then run the code.
# Here we import the dataset, and split it into the training, validation, and test sets.
from keras.datasets import mnist

# This is our training data, with 6400 samples.
###
# REPLACE <addTrainX> WITH train_X AND <addTrainY> WITH train_Y
###
train_X = mnist.load_data()[0][0][:6400].astype('float32')
train_Y = mnist.load_data()[0][1][:6400]
###

# This is our validation data, with 1600 samples.
###
# REPLACE <addValidX> WITH valid_X AND <addValidY> WITH valid_Y
###
valid_X = mnist.load_data()[1][0][:1600].astype('float32')
valid_Y = mnist.load_data()[1][1][:1600]
###

# This is our test data, with 2000 samples.
###
# REPLACE <addTextX> WITH test_X AND <addTextY> WITH test_Y
###
test_X = mnist.load_data()[1][0][-2000:].astype('float32')
test_Y = mnist.load_data()[1][1][-2000:]
###

print('train_X:', train_X.shape, end = '')
print(', train_Y:', train_Y.shape)
print('valid_X:', valid_X.shape, end = '')
print(', valid_Y:', valid_Y.shape)
print('test_X:', test_X.shape, end = '')
print(', test_Y:', test_Y.shape)
Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz 11493376/11490434 [==============================] - 5s 0us/step train_X: (6400, 28, 28), train_Y: (6400,) valid_X: (1600, 28, 28), valid_Y: (1600,) test_X: (2000, 28, 28), test_Y: (2000,)