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

Exercise 9 - Advanced Neural Networks

There are many factors that influence how well a neural network might perform. AI practitioners tend to play around with the structure of the hidden layers, the activation functions used, and the optimisation function.

In this exercise we will look at how changing these parameters impacts the accuracy performance of our network.

Step 1

In this exercise we will use the same dog dataset as in exercise 8, building on what we learnt before and trying different parameters for a network to try and improve performance.

Let's start by opening up our data set and setting up our train and test sets.

Run the code below.
# Run this!

# Here we set a randomisation seed for replicatability.
import os
os.environ['PYTHONHASHSEED'] = '0'
seed = 6
import random as rn
rn.seed(seed)
import numpy as np
np.random.seed(seed)

import warnings
warnings.filterwarnings("ignore")

from keras import backend as K
import keras

print('keras using %s backend'%keras.backend.backend())
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
# Sets up the graphing configuration
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
# Run this too!
# This gets our data ready

# Load the data
dataset = pd.read_csv('Data/dog_data.csv')

# Separate out the features
features = dataset.drop(['breed'], axis = 1)

# Sets the target one-hot vectors
target = OneHotEncoder(sparse = False).fit_transform(np.transpose([dataset['breed']]))

# Take the first 4/5 of the data and assign it to training
train_X = features.values[:160]
train_Y = target[:160]

# Take the last 1/5 of the data and assign it to testing
test_X = features.values[160:]
test_Y = target[160:]

Step 2

The box below contains methods to help us quickly change the structure. Don't edit them - just run the box.

The train_network method allows us to change:

  • the number of layers
  • the activation functions the layers use
  • the optimizer of the model
  • the number of training cycles for the model (epochs)

The plot_acc and bar_acc just plot our models so we can easily see how well they do.

Don't worry about the code - it is simply to make the next steps easier.

Run the code below.