Francis Banville / Mar 05 2020
Training neural networks using Flux.jl
1. Load packages
using Flux
using CSV
using DataFrames
using Random
using Statistics
using StatsPlots
Shift+Enter to run
Julia
2. Download and clean data (seeds dataset)
# Generate a temporary file path
Shift+Enter to run
Julia
# Download data in the temporary file path from the UCI website
# https://archive.ics.uci.edu/ml/machine-learning-databases/00236/seeds_dataset.txt
Shift+Enter to run
Julia
# Read the seeds dataset
# Values are separated by one or more tabulation
# There are no missing values
# There are no column names
Shift+Enter to run
Julia
# Name the variables (measures of wheat kernels = grains)
# 1 = area (A)
# 2 = perimeter (P)
# 3 = compactness (C = 4*pi*A/P^2)
# 4 = length of kernel
# 5 = width of kernel
# 6 = asymmetry coefficient
# 7 = length of kernel groove
# 8 = cultivar (1, 2 or 3) : variety of wheat
rename!(seeds,
[:Column1 => :area, :Column2 => :perimeter,
:Column3 => :compactness, :Column4 => :kernel_length,
:Column5 => :kernel_width, :Column6 => :asymmetry,
:Column7 => :kernel_groove, :Column8 => :cultivar]
)
Shift+Enter to run
Julia
3. Split dataset into testing and training sets
# Set seed for replicability
Shift+Enter to run
Julia
# Number of samples in training set
# Around 70% of data
Shift+Enter to run
Julia
# Indices of training and testing sets
# Training set: n unique random indices
# Testing set: other indices
Shift+Enter to run
Julia
# Training sets
Shift+Enter to run
Julia
# Testing sets
Shift+Enter to run
Julia
# Build training set for predictors (features)
Shift+Enter to run
Julia
# Build testing set for predictors (feautures)
Shift+Enter to run
Julia
# 1. Build training set for the predicted variable (cultivars)
# 2. Transform the cultivar variable into 3 columns (one-hot encoded)
# Rows are types of cultivar
# Columns are training samples
# Sorting labels allows corresponding rows to refer to the same cultivar
Shift+Enter to run
Julia
# 1. Build testing set for the predicted variable (cultivars)
# 2. Transform the cultivar variable into 3 columns (one-hot encoded)
# Rows are types of cultivar
# Columns are testing samples
# Sorting labels allows corresponding rows to refer to the same cultivar
Shift+Enter to run
Julia
4. Single-layer neural network
Build and train model
# Simple model
# Fully collected layer of 7 features and 3 possible outputs
# Result: output node with the highest score (softmax)
# Untrained model
Shift+Enter to run
Julia
# Train the model with a gradient descent optimiser (to find local minimum)
# Low learning rate of 0.01
Shift+Enter to run
Julia
# Loss function (cross entropy)
Shift+Enter to run
Julia
# Data iterator to handle training epochs
# Every element in data_e represent one epoch
Shift+Enter to run
Julia
# Train model
Shift+Enter to run
Julia
Accuracy
# Accuracy
Shift+Enter to run
Julia
# Confusion matrix
# Predicted in rows, reference in columns
# Most of the values are on the diagonal (which is good)
Shift+Enter to run
Julia
5. Deep neural network
Build and train model
# Add one hidden layer with 14 nodes
# Sigmoid activation in the input layer
Shift+Enter to run
Julia
# Define loss function
Shift+Enter to run
Julia
# Data iterator to handle training epochs rather than looping
# Every element in data_e represent one epoch
Shift+Enter to run
Julia
# Train model
Shift+Enter to run
Julia
Accuracy
# Accuracy
Shift+Enter to run
Julia
# Confusion matrix
# Worse than previous model
Shift+Enter to run
Julia
Acknowledgment
This example was taken from Timothée Poisot's blog (Armchair Ecology: Training a neural network on the seeds dataset using Flux.jl).