Fitting A Neural Network Using Randomized Optimization in Python
Fitting A Neural Network Using Randomized Optimization in Python
Member-only story
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 1/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
Python’s mlrose package provides functionality for implementing some of the most
popular randomization and search algorithms, and applying them to a range of
different optimization problem domains.
In this tutorial, we will discuss how mlrose can be used to find the optimal weights
for machine learning models, such as neural networks and regression models. That
is, to solve the machine learning weight optimization problem.
This is the third in a series of three tutorials about using mlrose to solve randomized
optimization problems. Part 1 can be found here and Part 2 can be found here.
Examples of such models include neural networks, linear regression models and
logistic regression models, and the optimal model weights for such models are
typically found using methods such as gradient descent.
However, the problem of fitting the parameters (or weights) of a machine learning
model can also be viewed as a continuous-state optimization problem, where the
loss function takes the role of the fitness function, and the goal is to minimize this
function.
By framing the problem this way, we can use any of the randomized optimization
algorithms that are suited to continuous-state optimization problems to fit the
model parameters.
Each of these classes includes a fit method, which implements the three steps for
solving an optimization problem defined in the previous tutorials, for a given
training set. That is,
However, when fitting a machine learning model, finding the optimal model
weights is merely a means to an end.
We want to find the optimal model weights so that we can use our fitted model to
predict the labels of future observations as accurately as possible, not because we
are actually interested in knowing the optimal weight values.
As a result, the above mentioned classes also include a predict method, which, if
called after the fit method, will predict the labels for a given test set using the
fitted model.
The steps involved in solving a machine learning weight optimization problem with
mlrose are then, typically:
2. Find the optimal model weights for a given training set by calling the fit
3. Predict the labels for a test set by calling the predict method of the object
initialized in Step 1.
To fit the model weights, the user can choose between using either randomized hill
climbing, simulated annealing, the genetic algorithm or gradient descent.
[In mlrose, the gradient descent algorithm is only available for use in solving the
machine learning weight optimization problem and has been included primarily for
benchmarking purposes, since this is one of the most common algorithms used in
fitting neural networks and regression models.]
We will now work through an example to illustrate how mlrose can be used to fit a
neural network and a regression model to a given dataset.
Before starting with the example, you will need to import the mlrose and Numpy
Python packages.
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 3/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
import mlrose
import numpy as np
It contains 150 observations of three classes (species) of iris flowers (50 observations
of each class), with each observation providing the sepal length, sepal width, petal
length and petal width (i.e. the feature values), as well as the class label (i.e. the
target value), of each flower under consideration.
The feature values and label of the first observation in the dataset are shown below,
along with the maximum and minimum values of each of the features and the
unique label values:
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 4/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
The feature values for Obs 0 are: [5.1 3.5 1.4 0.2]
The feature names are: ['sepal length (cm)', 'sepal width (cm)',
'petal length (cm)', 'petal width (cm)']
The maximum values of the four features are: [7.9 4.4 6.9 2.5]
From this we can see that all features in the Iris data set are numeric, albeit with
different ranges, and that the class labels have been represented by integers.
In the next few sections we will show how mlrose can be used to fit a neural network
and a logistic regression model to thisOpen
dataset,
in app to predict the species of an iris flower
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 5/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
Data Pre-Processing
Before we can fit any sort of machine learning model to a dataset, it is necessary to
manipulate our data into the form expected by mlrose.
Each of the three machine learning models supported by mlrose expect to receive
feature data in the form of a Numpy array, with one row per observation and
numeric features only (any categorical features must be one-hot encoded before
passing to the machine learning models).
The models also expect to receive the target values as either: a list of numeric values
(for regression data); a list of 0–1 indicator values (for binary classification data); or
as a Numpy array of one-hot encoded labels, with one row per observation (for
multi-class classification data).
In the case of the Iris dataset, all of our features are numeric, so no one-hot
encoding is required. However, it is necessary to one-hot encode the class labels.
In keeping with standard machine learning practice, it is also necessary to split the
data into training and test subsets, and since the range of the Iris data varies
considerably from feature to feature, to standardize the values of our feature
variables.
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 6/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
Neural Networks
Once the data has been pre-processed, fitting a neural network in mlrose simply
involves following the steps listed above.
Suppose we wish to fit a neural network classifier to the Iris dataset with one hidden
layer containing 2 nodes and a ReLU activation function (mlrose supports the ReLU,
identity, sigmoid and tanh activation functions).
For this example, we will use the Randomized Hill Climbing algorithm to find the
optimal weights, with a maximum of 1000 iterations of the algorithm and 100
attempts to find a better set of weights at each step.
We will also include a bias term; use a step size (learning rate) of 0.0001 (to find
neighbors of the current set of weights); and limit our weights to being in the range
-5 to 5 (to reduce the landscape over which the algorithm must search in order to
find the optimal weights).
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 7/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
In this case, our model achieves training accuracy of 45% and test accuracy of
53.3%. These accuracy levels are better than if the labels were selected at random,
but still leave room for improvement.
We can potentially improve on the accuracy of our model by tuning the parameters
we set when initializing the neural network object. Suppose we decide to change the
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 8/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
optimization algorithm to gradient descent, but leave all other model parameters
unchanged.
❤ i
This results in a 39% increase in training accuracy to 62.5%, but a much smaller
increase in test accuracy to 56.7%.
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 9/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
As a result, we could fit either of these models to our data using the NeuralNetwork()
For example, suppose we wished to fit a logistic regression to the Iris data using the
randomized hill climbing algorithm and all other parameters set as for the example
in the previous section. We could do this by initializing a NeuralNetwork() object like
so:
In the Iris dataset example, we can, thus, initialize and fit our logistic regression
model as follows:
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 10/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
i i i i i ❤ i view raw
This model achieves 19.2% training accuracy and 6.7% test accuracy, which is worse
than if we predicted the labels by selecting values at random.
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 11/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
i i i i i ❤ i view raw
This results in significant improvements to both training and test accuracy, with
training accuracy levels now reaching 68.3% and test accuracy levels reaching 70%.
Summary
In this tutorial we discussed how mlrose can be used to find the optimal weights of
three types of machine learning models: neural networks, linear regression models
and logistic regression models.
To learn more about mlrose, visit the GitHub repository for this package, available here.
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 12/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
Dr Genevieve Hayes is a data scientist, educator and AI and analytics specialist with
Genevieve Hayes Consulting. You can follow her on LinkedIn or Twitter. She is also the host
of Value Driven Data Science, a twice-monthly podcast for businesses looking to maximise
the value of their data and data teams.
Want to unlock the value of your business’s data, but don’t know where to begin?
Download the FREE Data Science Project Discovery Guide.
Follow
Data scientist and educator with a PhD in Statistics — Helping data professionals maximize the value of data
without expensive tools — www.genevievehayes.com.
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 13/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
113
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 14/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
3.1K 25
Don’t Start Your Data Science Journey Without These 5 Must-Do Steps
From a Spotify Data Scientist
A complete guide to everything I wish I’d done before starting my Data Science journey, here’s
to acing your first year with data
2.3K 23
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 15/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
535 9
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 16/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
Gazala
11 1
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 17/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
3.1K 25
Lists
New_Reading_List
174 stories · 149 saves
Ms Aerin in IntuitionMath
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 18/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
408 3
61 1
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 19/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
Virat Patel
I applied to 230 Data science jobs during last 2 months and this is what
I’ve found.
A little bit about myself: I have been working as a Data Analyst for a little over 2 years.
Additionally, for the past year, I have been…
1.4K 35
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 20/21
10/15/23, 10:25 AM Fitting a Neural Network Using Randomized Optimization in Python | by Genevieve Hayes, PhD | Towards Data Science
51
https://towardsdatascience.com/fitting-a-neural-network-using-randomized-optimization-in-python-71595de4ad2d 21/21