How To Code A Neural Network With Backpropagation in Python
How To Code A Neural Network With Backpropagation in Python
Navigation
Search...
The backpropagation algorithm is used in the classical feed-forward artificial neural network.
In this tutorial, you will discover how to implement the backpropagation algorithm for a neural network from
scratch with Python.
Discover how to code ML algorithms from scratch including kNN, decision trees, neural nets, ensembles
and much more in my new book, with full Python code and no fancy libraries.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 1/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Email Address
Description
This section provides a brief introduction to the Backpropagation Algorithm and the Wheat Seeds dataset
that we will be using in this tutorial.
Backpropagation Algorithm
The Backpropagation algorithm is a supervised learning method for multilayer feed-forward networks from
the field of Artificial Neural Networks.
Your Start in Machine Learning
Feed-forward neural networks are inspired by the information processing of one or more neural cells, called
a neuron. A neuron accepts input signals via its dendrites, which pass the electrical signal down to the cell
body. The axon carries the signal out to synapses, which are the connections of a cell’s axon to other cell’s
dendrites.
The principle of the backpropagation approach is to model a given function by modifying internal
weightings of input signals to produce an expected output signal. The system is trained using a supervised
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 2/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
learning method, where the error between the system’s output and a known expected output is presented
to the system and used to modify its internal state.
Technically, the backpropagation algorithm is a method for training the weights in a multilayer feed-forward
neural network. As such, it requires a network structure to be defined of one or more layers where one
layer is fully connected to the next layer. A standard network structure is one input layer, one hidden layer,
and one output layer.
Backpropagation can be used for both classification and regression problems, but we will focus on
classification in this tutorial.
In classification problems, best results are achieved when the network has one neuron in the output layer
for each class value. For example, a 2-class or binary classification problem with the class values of A and
B. These expected outputs would have to be transformedYour Start
into binary in Machine
vectors with one column for each ×
class value. Such as [1, 0] and [0, 1] for A and B respectively. This is called a one hot encoding.
Learning
Wheat Seeds Dataset You can master applied Machine Learning
without math or fancy degrees.
The seeds dataset involves the prediction of species given measurements
Find seeds
out how in this free andfrom different
practical course.varieties of
wheat.
Email Address
There are 201 records and 7 numerical input variables. It is a classification problem with 3 output classes.
The scale for each numeric input value vary, so some data normalization may be required for use with
algorithms that weight inputs like the backpropagation algorithm.
START MY EMAIL COURSE
1 15.26,14.84,0.871,5.763,3.312,2.221,5.22,1
2 14.88,14.57,0.8811,5.554,3.333,1.018,4.956,1
3 14.29,14.09,0.905,5.291,3.337,2.699,4.825,1
4 13.84,13.94,0.8955,5.324,3.379,2.259,4.805,1
5 16.14,14.99,0.9034,5.658,3.562,1.355,5.175,1
Using the Zero Rule algorithm that predicts the most common class value, the baseline accuracy for the
problem is 28.095%.
You can learn more and download the seeds dataset from the UCI Machine Learning Repository.
Download the seeds dataset and place it into your current working directory with the filename
Your Start in Machine Learning
seeds_dataset.csv.
The dataset is in tab-separated format, so you must convert it to CSV using a text editor or a spreadsheet
program.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 3/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Tutorial
This tutorial is broken down into 6 parts:
1. Initialize Network.
2. Forward Propagate.
3. Back Propagate Error.
4. Train Network.
5. Predict.
6. Seeds Dataset Case Study.
These steps will provide the foundation that you need to implement the backpropagation algorithm from
scratch and apply it to your own predictive modeling problems.
Your Start in Machine ×
1. Initialize Network Learning
Let’s start with something easy, the creation of a new network ready
You can forapplied
master training.
Machine Learning
without math or fancy degrees.
Each neuron has a set of weights that need to be maintained. One
Find out howweight for each
in this free input connection
and practical course. and an
additional weight for the bias. We will need to store additional properties for a neuron during training,
therefore we will use a dictionary to represent each neuron and store properties by names such as
Email Address
‘weights‘ for the weights.
We will organize layers as arrays of dictionaries and treat the whole network as an array of layers.
It is good practice to initialize the network weights to small random numbers. In this case, will we use
random numbers in the range of 0 to 1.
Below is a function named initialize_network() that creates a new neural network ready for training. It
accepts three parameters, the number of inputs, the number of neurons to have in the hidden layer and the
number of outputs.
You can see that for the hidden layer we create n_hidden neurons and each neuron in the hidden layer
has n_inputs + 1 weights, one for each input column in a dataset and an additional one for the bias.
Your Start in Machine Learning
You can also see that the output layer that connects to the hidden layer has n_outputs neurons, each with
n_hidden + 1 weights. This means that each neuron in the output layer connects to (has a weight for) each
neuron in the hidden layer.
1 # Initialize a network
2 def initialize_network(n_inputs, n_hidden, n_outputs):
3 network = list()
4 hidden_layer = [{'weights':[random() for i in range(n_inputs + 1)]} for i in range(n_hidden)
5 network.append(hidden_layer)
6 output_layer = [{'weights':[random() for i in range(n_hidden + 1)]} for i in range(n_outputs
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 4/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
7 network.append(output_layer)
8 return network
Let’s test out this function. Below is a complete example that creates a small network.
Now that we know how to create and initialize a network, let’s see how we can use it to calculate an output.
START MY EMAIL COURSE
2. Forward Propagate
We can calculate an output from a neural network by propagating an input signal through each layer until
the output layer outputs its values.
It is the technique we will need to generate predictions during training that will need to be corrected, and it
is the method we will need after the network is trained to make predictions on new data.
1. Neuron Activation.
2. Neuron Transfer. Your Start in Machine Learning
3. Forward Propagation.
The input could be a row from our training dataset, as in the case of the hidden layer. It may also be the
outputs from each neuron in the hidden layer, in the case of the output layer.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 5/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Neuron activation is calculated as the weighted sum of the inputs. Much like linear regression.
Where weight is a network weight, input is an input, i is the index of a weight or an input and bias is a
special weight that has no input to multiply with (or you can think of the input as always being 1.0).
Below is an implementation of this in a function named activate(). You can see that the function assumes
that the bias is the last weight in the list of weights. This helps here and later to make the code easier to
read.
The sigmoid activation function looks like an S shape, it’s also called the logistic function. It can take any
input value and produce a number between 0 and 1 on an S-curve. It is also a function of which we can
easily calculate the derivative (slope) that we will need later when backpropagating error.
1 output = 1 / (1 + e^(-activation))
Now that we have the pieces, let’s see how they are used.
We work through each layer of our network calculating the outputs for each neuron. All of the outputs from
one layer become inputs to the neurons on the next layer.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 6/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Below is a function named forward_propagate() that implements the forward propagation for a row of data
from our dataset with our neural network.
You can see that a neuron’s output value is stored in the neuron with the name ‘output‘. You can also see
that we collect the outputs for a layer in an array named new_inputs that becomes the array inputs and is
used as inputs for the following layer.
The function returns the outputs from the last layer also called the output layer.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 7/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Running the example propagates the input pattern [1, 0] and produces an output value that is printed.
Because the output layer has two neurons, we get a list of two numbers as output.
The actual output values are just nonsense for now, but next, we will start to learn how to make the weights
in the neurons more useful.
1 [0.6629970129852887, 0.7253160725279748]
Error is calculated between the expected outputs and the outputs forward propagated from the network.
These errors are then propagated backward through the network from the output layer to the hidden layer,
Your
assigning blame for the error and updating weights as they go. Start in Machine
×
Learning
The math for backpropagating error is rooted in calculus, but we will remain high level in this section and
focus on what is calculated and how rather than why the You can mastertake
calculations applied
thisMachine Learning
particular form.
without math or fancy degrees.
This part is broken down into two sections. Find out how in this free and practical course.
We are using the sigmoid transfer function, the derivative of which can be calculated as follows:
Where expected is the expected output value for the neuron, output is the output value for the neuron and
transfer_derivative() calculates the slope of the neuron’s output value, as shown above.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 8/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
This error calculation is used for neurons in the output layer. The expected value is the class value itself. In
the hidden layer, things are a little more complicated.
The error signal for a neuron in the hidden layer is calculated as the weighted error of each neuron in the
output layer. Think of the error traveling back along the weights of the output layer to the neurons in the
hidden layer.
The back-propagated error signal is accumulated and then used to determine the error for the neuron in
the hidden layer, as follows:
Where error_j is the error signal from the jth neuron in the output layer, weight_k is the weight that
connects the kth neuron to the current neuron and output is the output for the current neuron.
Your Start in Machine ×
Below is a function named backward_propagate_error()Learning
that implements this procedure.
You can see that the error signal calculated for each neuron
You is stored
can masterwith the Machine
applied name ‘delta’.
LearningYou can see
that the layers of the network are iterated in reverse order, starting
without at the
math output
or fancy and working backwards.
degrees.
Find out
This ensures that the neurons in the output layer have ‘delta’ how calculated
values in this free and
firstpractical course. in the
that neurons
hidden layer can use in the subsequent iteration. I chose the name ‘delta’ to reflect the change the error
implies on the neuron (e.g. the weight delta). Email Address
You can see that the error signal for neurons in the hidden layer is accumulated from neurons in the output
layer where the hidden neuron number j is also the index ofSTART MY EMAIL COURSE
the neuron’s weight in the output layer
neuron[‘weights’][j].
Let’s put all of the pieces together and see how it works.
We define a fixed neural network with output values and backpropagate an expected output pattern. The
complete example is listed below.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 9/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Email Address
Running the example prints the network after the backpropagation of error is complete. You can see that
error values are calculated and stored in the neurons for the output layer and the hidden layer.
START MY EMAIL COURSE
1 [{'output': 0.7105668883115941, 'weights': [0.13436424411240122, 0.8474337369372327, 0.763774618
2 [{'output': 0.6213859615555266, 'weights': [0.2550690257394217, 0.49543508709194095], 'delta': -
4. Train Network
The network is trained using stochastic gradient descent.
This involves multiple iterations of exposing a training dataset to the network and for each row of data
forward propagating the inputs, backpropagating the error and updating the network weights.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 10/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Where weight is a given weight, learning_rate is a parameter that you must specify, error is the error
calculated by the backpropagation procedure for the neuron and input is the input value that caused the
error.
The same procedure can be used for updating the bias weight, except there is no input term, or input is the
fixed value of 1.0.
Learning rate controls how much to change the weight to correct for the error. For example, a value of 0.1
will update the weight 10% of the amount that it possibly could be updated. Small learning rates are
preferred that cause slower learning over a large number of training iterations. This increases the likelihood
of the network finding a good set of weights across all layers rather than the fastest set of weights that
minimize error (called premature convergence).
Now we know how to update network weights, let’s see how we can do it repeatedly.
This involves first looping for a fixed number of epochs and within each epoch updating the network for
each row in the training dataset.
Because updates are made for each training pattern, this type of learning is called online learning. If errors
were accumulated across an epoch before updating the weights, this is called batch learning or batch
gradient descent.
Your Start in Machine Learning
Below is a function that implements the training of an already initialized neural network with a given training
dataset, learning rate, fixed number of epochs and an expected number of output values.
The expected number of output values is used to transform class values in the training data into a one hot
encoding. That is a binary vector with one column for each class value to match the output of the network.
This is required to calculate the error for the output layer.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 11/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
You can also see that the sum squared error between the expected output and the network output is
accumulated each epoch and printed. This is helpful to create a trace of how much the network is learning
and improving each epoch.
We now have all of the pieces to train the network. We can Your Start an
put together inexample
Machine that includes
×
Learning
everything we’ve seen so far including network initialization and train a network on a small dataset.
Below is the complete example. We will use 2 neurons in the hidden layer. It is a binary classification
problem (2 classes) so there will be two neurons in the output layer. The network will be trained for 20
epochs with a learning rate of 0.5, which is high because we are training for so few iterations.
Running the example first prints the sum squared error each training epoch. We can see a trend of this
error decreasing with each epoch.
5. Predict
Making predictions with a trained neural network is easy Your Start in Machine Learning
enough.
We have already seen how to forward-propagate an input pattern to get an output. This is all we need to do
to make a prediction. We can use the output values themselves directly as the probability of a pattern
belonging to each output class.
It may be more useful to turn this output back into a crisp class prediction. We can do this by selecting the
class value with the larger probability. This is also called the arg max function.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 14/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Below is a function named predict() that implements this procedure. It returns the index in the network
output that has the largest probability. It assumes that class values have been converted to integers
starting at 0.
We can put this together with our code above for forward propagating input and with our small contrived
dataset to test making predictions with an already-trained network. The example hardcodes a network
trained from the previous step.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 15/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Running the example prints the expected output for each record in the training dataset, followed by the
crisp prediction made by the network.
It shows that the network achieves 100% accuracy on this small dataset.
1 Expected=0, Got=0
2 Expected=0, Got=0
3 Expected=0, Got=0
4 Expected=0, Got=0
5 Expected=0, Got=0
6 Expected=1, Got=1
7 Expected=1, Got=1
8 Expected=1, Got=1
9 Expected=1, Got=1
10 Expected=1, Got=1
Now we are ready to apply our backpropagation algorithm to a real world dataset.
Your Start in Machine ×
6. Wheat Seeds Dataset Learning
This section applies the Backpropagation algorithm to theYou can master
wheat seedsapplied Machine Learning
dataset.
without math or fancy degrees.
Find
The first step is to load the dataset and convert the loaded out to
data how in this free
numbers and
that wepractical
can use course.
in our neural
network. For this we will use the helper function load_csv() to load the file, str_column_to_float() to
convert string numbers to floats and str_column_to_int()Email Address
to convert the class column to integer values.
Input values vary in scale and need to be normalized to the range of 0 and 1. It is generally good practice
START MY EMAIL COURSE
to normalize input values to the range of the chosen transfer function, in this case, the sigmoid function that
outputs values between 0 and 1. The dataset_minmax() and normalize_dataset() helper functions were
used to normalize the input values.
We will evaluate the algorithm using k-fold cross-validation with 5 folds. This means that 201/5=40.2 or 40
records will be in each fold. We will use the helper functions evaluate_algorithm() to evaluate the
algorithm with cross-validation and accuracy_metric() to calculate the accuracy of predictions.
A new function named back_propagation() was developed to manage the application of the
Backpropagation algorithm, first initializing a network, training it on the training dataset and then using the
trained network to make predictions on a test dataset.
A network with 5 neurons in the hidden layer and 3 neurons in the output layer was constructed. The
network was trained for 500 epochs with a learning rate of 0.3. These parameters were found with a little
trial and error, but you may be able to do much better.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 19/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Running the example prints the average classification accuracy on each fold as well as the average
performance across all folds.
You can see that backpropagation and the chosen configuration achieved a mean classification accuracy of
about 93% which is dramatically better than the Zero Rule algorithm that did slightly better than 28%
accuracy.
Extensions
This section lists extensions to the tutorial that you may wish to explore.
Review
In this tutorial, you discovered how to implement the Backpropagation algorithm from scratch.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 20/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
It covers 18 tutorials with all the code for 12 top algorithms, like:
Linear Regression, k-Nearest Neighbors, Stochastic Gradient Descent and much
more…
Email Address
Tweet Share Share
Jason Brownlee, PhD is a machine learning specialist who teaches developers how to get results with
modern machine learning methods via hands-on tutorials.
View all posts by Jason Brownlee →
How To Implement Learning Vector Quantization (LVQ) From Scratch With Python
How To Implement The Decision Tree Algorithm From Scratch In Python
REPLY
Talk Data To Me November 7, 2016 at 9:28 pm #
That’s what I was looking for. Write a neural network without any libraries (scikit, keras etc.) Thnak
you very much!
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 21/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee November 8, 2016 at 9:51 am #
REPLY
sari dewi August 16, 2019 at 11:55 am #
Hy Mr. jason , i try your code to make a neural network with backpropagation method, I
using jupyter notebook anaconda and pyhton 3.7 64 bit, when i try this code
seed(1)
# load and prepare data
filename =’datalatih.csv’
dataset = load_csv(filename) Your Start in Machine ×
for i in range(len(dataset[0])-1): Learning
str_column_to_float(dataset, i)
# convert class column to integers You can master applied Machine Learning
str_column_to_int(dataset, len(dataset[0])-1) without math or fancy degrees.
# normalize input variables Find out how in this free and practical course.
minmax = dataset_minmax(dataset)
normalize_dataset(dataset, minmax) Email Address
# evaluate algorithm
n_folds =5
l_rate =0.3 START MY EMAIL COURSE
n_epoch =500
n_hidden =5
scores = evaluate_algorithm(dataset, back_propagation, n_folds, l_rate, n_epoch, n_hidden)
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 22/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
WB February 20, 2018 at 3:07 pm #
I experienced the following applying the Backpropagation algorithm to the wheat seeds dataset.
I am wondering how to resolve the errors? Thank you
—————————————————————————
ValueError Traceback (most recent call last)
in ()
184 dataset = load_csv(filename)
185 for i in range(len(dataset[0])-1):
–> 186 str_column_to_float(dataset, i)
187 # convert class column to integers
188 str_column_to_int(dataset, len(dataset[0])-1) Your Start in Machine Learning
in str_column_to_float(dataset, column)
20 def str_column_to_float(dataset, column):
21 for row in dataset:
—> 22 row[column] = float(row[column].strip())
23
24 # Convert string column to integer
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 23/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee February 21, 2018 at 6:35 am #
REPLY
wb February 21, 2018 at 2:51 pm #
Yes I am
Learning
hi bro whass up
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY
Mike Harney March 5, 2018 at 9:53 am #
Email Address
Hi wb, I’m on 3.6 and I found the same issue. Maybe you can answer this Jason, but it
looks like the some of the data is misaligned in the sample. When opened in Excel, there are many
open spaces followed by data jutted out to an extra START
column.
MYI assume this is unintentional, and when
EMAIL COURSE
I corrected the spacing, it appeared to work for me.
REPLY
Jason Brownlee March 6, 2018 at 6:08 am #
Mike is right – the dataset from the UCI website is slightly defective: It has two tabs
in some places where there should be only one.Start
Your Thisin
needs to beLearning
Machine corrected during the
conversion to CSV. In Excel the easiest way is to use the text importer and then click the
“Treat consecutive delimiters as one” checkbox.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 24/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Alexis Batyk August 29, 2018 at 6:22 am #
[SOLVED]
i have the same issue with
https://raw.githubusercontent.com/jbrownlee/Datasets/master/wheat-seeds.csv
use a text editor -> select search and replace tool -> search ‘,,’ replace ‘,’ and it works
REPLY
Deng October 14, 2018 at 5:50 pm #
The data in the seeds_dataset file contains the backspace key, and it is ok to reset the data
REPLY
George Dong May 12, 2019 at 6:14 pm #
Just one question please! In your code below, I could not understand why multiplication is used instead
of division in the last line. Though division caused divide by zero problem.
Your Start in Machine Learning
1 # Backpropagate error and store in neurons
2 def backward_propagate_error(network, expected):
3 for i in reversed(range(len(network))):
4 layer = network[i]
5 errors = list()
6 if i != len(network)-1:
7 for j in range(len(layer)):
8 error = 0.0
9 for neuron in network[i + 1]:
10 error += (neuron['weights'][j] * neuron['delta'])
11
12 errors.append(error)
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 25/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
13 for j in range(len(layer)):
14 layer[j]['error'] = errors[j]
15 else:
16 for j in range(len(layer)):
17 neuron = layer[j]
18 errors.append(expected[j] - neuron['output'])
19 neuron['error']=expected[j] - neuron['output']
20 for j in range(len(layer)):
21 neuron = layer[j]
22 neuron['delta'] = errors[j] * transfer_derivative(neuron['output'])
23 ## neuron['delta'] = errors[j] / transfer_derivative(neuron['output'])
Did we somehow make changes here, for calculation reasons, to use arctan instead of tan for gradient?
Email Address
REPLY
Jason Brownlee November 8, 2016 at 10:01 am #
START MY EMAIL COURSE
Hi MO.
The small contrived dataset used for testing is listed inline in the post in section 4.2
The dataset used for the full example is on the UCI ML repository, linked in the section titled “Wheat
Seeds Dataset”. Here is the direct link:
http://archive.ics.uci.edu/ml/datasets/seeds
REPLY
prakash November 11, 2016 at 12:40 am #
in two class classification for 0 the expected value is [1,0] for 1 its is [0,1].
how will be the output vectors for more than two class??
Your Start in Machine Learning
REPLY
Jason Brownlee November 11, 2016 at 10:02 am #
Hi prakash,
Three class values for “red”, “green” “blue” can be represented as an output vector like:
1, 0, 0 for red
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 26/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
0, 1, 0 for green
0, 0, 1 for blue
REPLY
Rakesh November 13, 2016 at 3:41 pm #
Hi, Jason.
You’ve mentioned that there are 3 output classes.
How do we check the values which come under the 3 classes / clusters?
Could we print the data which fall under each class?
I’m confused why the activation method iterates from 0 to len(inputs) – 1 instead of from 0 to
len(weights) – 1. Am I missing something?
REPLY
Jason Brownlee November 17, 2016 at 9:47 am #
Hi Alex,
The length of weights is the length of the input + 1 (to accommodate the bias term).
We add the bias term first, then we add the weighted inputs. This is why we iterate over input values.
REPLY
Alex November 17, 2016 at 12:29 pm #
When I step through the code above for the ‘forward_propagate’ test case, I see the code
correctly generate the output for the single hidden node but that output doesn’t get correctly
processed when determining the outputs for the output layer. As written above in the activate
function ‘for i in range(len(inputs)-1):’, when the calculation gets to the activate function for the
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 27/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
output node for class=0, since ‘inputs’ has a single element in it (the output from the single hidden
node), ‘len(inputs) – 1’ equals 0 so the for loop never executes. I’m assuming the code is supposed
to read ‘for i in range(len(weights) -1):’ Does that make sense?
I’m just trying to make sure I don’t fundamentally misunderstand something and improve this post for
other readers. This site has been really, really helpful for me.
REPLY
Jason Brownlee November 18, 2016 at 8:27 am #
In this fragment:
for j in range(len(inputs)-1):
neuron[‘weights’][j] += l_rate * neuron[‘delta’] * inputs[j]
neuron[‘weights’][-1] += l_rate * neuron[‘delta’]
If inputs length = 1, you are not updating weights, it’s correct? You are updating only bias, because in hidden
layer is only one neuron.
REPLY
Tomasz November 21, 2016 at 1:34 am #
Hello. In method update_weight you are doing for j in range(len(inputs) – 1). If inputs lenght = 1,
you aren’t updating weights. It’s correct? Hidden layer have one neuron so in output layer weights aren’t
updated
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 28/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee November 22, 2016 at 6:54 am #
Hi Tomasz,
The assumption here is that the input vector always contains at least one input value and an output
value, even if the output is set to None.
You may have found a bug though when updating the layers. I’ll investigate and get back to you.
REPLY
Jason Brownlee January 3, 2017 at 10:17 am #
I don’t understand how update_weights updates the NN. There is no global variable or
return from the function. What am I missing? Email Address
REPLY
Michael December 13, 2016 at 4:15 am #
Hi, Thanks for the tutorial, I’m doing a backpropagation project at the moment so its been really
useful.
I was a little confused on the back-propagation error calculation function. Does “if i != len(network)-1:” mean
Your Start in Machine Learning
that if the current layer isn’t the output layer then this following code is run or does it mean that the current
layer is an output layer?
REPLY
Jason Brownlee December 13, 2016 at 8:08 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 29/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
The line means if the index i is not equal to the index of the last layer of the network (the output layer),
then run code inside the block.
REPLY
Michael January 5, 2017 at 7:53 am #
I have a project where I have to implement the Backpropagation algorithm with possibly the MNIST
handwritten digit training set.
Your Start in Machine ×
I hope my question makes sense!
Learning
You can master applied Machine Learning
Jason Brownlee January 5, 2017 at 9:42 am # without math or fancy degrees. REPLY
REPLY
Calin January 6, 2017 at 10:40 pm #
Hi Jason,
Great post!
REPLY
Jason Brownlee January 7, 2017 at 8:37 am #
Hi Calin,
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 30/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
If I understand you correctly, No. The n_outputs var is the length of the number of possible output
values.
Maybe put some print() statements in to help you better understand what values variables have.
REPLY
Calin January 7, 2017 at 9:48 pm #
Hmm..I ran the entire code (with the csv file downloaded from
http://archive.ics.uci.edu/ml/datasets/seeds), added some breakpoints and this is what I got after a
few iterations:
n_outputs = 168
row[-1] = 201
Your Start in Machine ×
which is causing IndexError: list assignment index out of range.
Learning
You can master applied Machine Learning
without math or fancy degrees.
REPLY
Adriaan January 11, 2017 at 4:27 am # Find out how in this free and practical course.
I’ve got the same error, That my list assignment index is out of range
Email Address
Sorry to hear that, did you try running the updated code?
This is error of csv read. Try to reformat it with commas. For me it worked
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 31/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
updated code
Email Address
http://superuser.com/questions/783060/excel-save-as-csv-options-possible-to-change-comma-to-pipe-
or-tab-instead
REPLY
Stanley January 8, 2017 at 3:15 pm #
Just one question: in the equation “weight = weight + learning_rate * error * input”, why there is an “input”?
IMO it should be: “weight = weight + learning_rate * error”?
REPLY
Jason Brownlee January 9, 2017 at 7:47 am #
For the input layer the input are the input data, for hidden layers the input is the output of the prior layer.
REPLY
Madwadasa January 13, 2017 at 3:31 am #
Jason,
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 32/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee January 13, 2017 at 9:16 am #
Hi Madwadasa,
Expected is a one-hot encoding. All classes are “0” expect the actual class for the row which is marked
as a “1” on the next line.
REPLY
Jason Brownlee January 19, 2017 at 7:38 am #
Hi Michael,
The epoch error does capture how wrong the algorithm is on all training data. This may or may not be a
distance depending on the error measure used. RMSE is technically not a distance measure, you could
use Euclidean distance if you like, but I would not recommend it.
Yes, in generally when the model makes predictions your understanding is correct.
REPLY
Bernardo Galvão January 24, 2017 at 3:51 am #
Your Start in Machine Learning
Hi Jason,
“Where error_j is the error signal from the jth neuron in the output layer, weight_k is the weight that connects
the kth neuron to the current neuron and output is the output for the current neuron.”
is the k-th neuron a neuron in the output layer or a neuron in the hidden layer we’re “on”? What about the
current neuron, are you referring to the neuron in the output layer? Sorry, english is not my native tongue.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 33/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Bernardo
REPLY
anonymous February 1, 2017 at 1:42 am #
It would have been better if recall and precision were printed. Can somebody tell me how to print
them in the above code.
REPLY
Jason Brownlee February 1, 2017 at 10:51 am #
REPLY
Jason Brownlee February 7, 2017 at 10:14 am #
Sorry, I don’t have the capacity to write or spell out this change for you.
My advice would be to read a good book on the topic, such as Neural Smithing: http://amzn.to/2ld9ds0
REPLY
ibrahim February 18, 2017 at 2:21 am #
Hi Jason,
I have my own code written in C++, which works similar to your code. My intention is to extend my code to
convolutional deep neural nets, and i have actually written the convolution, Relu and pooling functions
Your used
however i could not begin to apply the backpropagation i have Startininmy
Machine
shallowLearning
neural net, to the
convolutional deep net, cause i really cant imagine the transition of the backpropagation calculation between
the convolutional layers and the standard shallow layers existing in the same system. I hoped to find a
source for this issue however i always come to the point that there is a standard backpropagation algorithm
given for shallow nets that i applied already. Can you please guide me on this problem?
REPLY
Jason Brownlee February 18, 2017 at 8:42 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 34/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
I”d love to guide you but I don’t have my own from scratch implementation of CNNs, sorry. I’m
not best placed to help at the moment.
REPLY
matias February 22, 2017 at 3:34 pm #
Thank you, I was looking for exactly this kind of ann algorith. A simple thank won’t be enough tho
lol
Great one! .. I have one doubt .. the dataset seeds contains missing features/fields for some rows..
how you are handling that …
REPLY
Jason Brownlee February 27, 2017 at 5:49 am #
You could set the missing values to 0, you could remove the rows with missing values, you
could impute the missing values with mean column values, etc.
Try a few different methods and see what results in the best performing models.
REPLY
Manohar Katam March 1, 2017 at 2:59 pm #
What if I have canonical forms like “male” or “female” in my dataset… Will this program
work even with string data..
REPLY
Jason Brownlee March 2, 2017 at 8:11 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 35/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Hi Manohar,
No, you will need to convert them to integers (integer encoding) or similar.
REPLY
Wissal ARGOUBI February 27, 2017 at 11:12 pm #
Great job! this is what i was looking for ! thank you very much .
However i already have a data base and i didn’t know how to make it work with this code how can i adapt it
on my data
Thank you
REPLY
Jason Brownlee March 6, 2017 at 10:55 am #
You can learn more about the math in the book on the topic.
REPLY
Sittha March 13, 2017 at 1:23 pm #
Your Start in Machine Learning
Thanks for a good tutorial.
I have some IndexError: list assignment index out of range. And I cannot fix it with comma or full-stop
separator.
REPLY
Jason Brownlee March 14, 2017 at 8:11 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 36/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Did you copy-paste the full final example and run it on the same dataset?
REPLY
Sittha March 24, 2017 at 3:36 am #
line 151 :
expected[row[-1]] = 1
IndexError : list assignment index out of range
REPLY
Jason Brownlee March 24, 2017 at 8:00 am #
Email Address
REPLY
Karan March 16, 2017 at 6:26 pm # START MY EMAIL COURSE
The dataset that was given was for training the network. Now how do we test the network by
providing the 7 features without giving the class label(1,2 or 3) ?
REPLY
Jason Brownlee March 17, 2017 at 8:27 am #
You will have to adapt the example to fit the model on all of the training data, then you can call
predict() to make predictions on new data.
REPLY
Karan March 19, 2017 at 7:43 pm # Your Start in Machine Learning
Ok Jason, i’ll try that and get back to you! Thank you!
REPLY
Karan March 19, 2017 at 7:48 pm #
Just a suggestion for the people who would be using their own dataset(not the seeds_dataset) for
training their network, make sure you add an IF loop as follows before the 45th line :
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 37/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
if minmax[i][1]!=minmax[i][0]
This is because your own dataset might contain same values in the same column and that might cause a
divide by zero error.
REPLY
Jason Brownlee March 20, 2017 at 8:16 am #
REPLY
Li Qun March 25, 2017 at 5:45 pm #
Your Start in Machine ×
Thanks jason for the amazing posts of your from scratch pyhton implementations! i have learned so
much from you! Learning
Youposts,
I have followed through both your naive bayes and backprop can master
and Iapplied
have aMachine Learning
(perhaps quite naive)
question: without math or fancy degrees.
Find out how in this free and practical course.
what is the relationship between the two? did backprop actually implement bayesian inference (after all,
what i understand is that bayesian = weights being updated every cycle) already? perhaps just non-
Email Address
gaussian? so.. are non-gaussian PDF weight updates not bayesian inference?
i guess to put it simply : is backpropagation essentially a bayesian inference loop for an n number of
epochs? START MY EMAIL COURSE
I came from the naive bayes tutorial wanting to implement backpropagation together with your naive bayes
implementation but got a bit lost along the way.
sorry if i was going around in circles, i sincerely hope someone would be able to at least point me on the
right direction.
REPLY
Jason Brownlee March 26, 2017 at 6:11 am #
Great question.
No, they are both very different. Naive bayes is a direct use of the probabilities and bayes theorem. The
neural net is approximating a mapping function from inputs and outputs
Your Start – a very
in Machine different approach that
Learning
does not directly use the joint probability.
REPLY
Chiraag March 26, 2017 at 10:10 pm #
How did you decide that the number of folds will be 5 ? Could you please explain the significance of
this number. Thank You.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 38/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee March 27, 2017 at 7:54 am #
Generally, you want to split the data so that each fold is representative of the dataset. The objective
measure is how closely the mean performance reflect the actual performance of the model on unseen
data. We can only estimate this in practice (standard error?).
REPLY
Li Qun March 27, 2017 at 10:19 pm #
Dear Jason,
Your between
thank you for the reply! I read up a bit more about the differences Start in Machine
Naive Bayes (or Bayesian Nets in
×
Learning
general) and Neural Networks and found this Quora answer that i thought was very clear. I’ll put it up here to
give other readers a good point to go from:
You can master applied Machine Learning
https://www.quora.com/What-is-the-difference-between-a-Bayesian-network-and-an-artificial-neural-network
without math or fancy degrees.
TL:DR : Find out how in this free and practical course.
– they look the same, but every node in a Bayesian Network has meaning, in that you can read a Bayesian
network structure (like a mind map) and see what’s happening
Emailwhere and why.
Address
– a Neural Network structure doesn’t have explicit meaning, its just dots that link previous dots.
– there are more reasons, but the above two highlighted the biggest difference.
START MY EMAIL COURSE
Just a quick guess after playing around with backpropagation a little: the way NB and backprop NN would
work together is by running Naive Bayes to get a good ‘first guess’ of initial weights that are then run through
and Neural Network and Backpropagated?
REPLY
Jason Brownlee March 28, 2017 at 8:23 am #
Please note that a Bayesian network and naive bayes are very different algorithms.
REPLY
Melissa March 27, 2017 at 10:54 pm #
Your Start in Machine Learning
Hi Jason,
Further to this update:
I’m still having this same problem whilst using python 3, on both the seeds data set and my own. It returns
an error at line 75 saying ‘list object has no attribute ‘sum” and also saying than ‘an integer is required.’
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 39/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee March 28, 2017 at 8:24 am #
Sorry to hear that, did you try copy-paste the complete working example from the end of the
post and run it on the same dataset from the command line?
REPLY
Melissa March 28, 2017 at 9:29 am #
Your
Yes I’ve done that, but still the same problem! Start in Machine ×
Learning
You can master applied Machine Learning
REPLY
david March 29, 2017 at 6:16 am # without math or fancy degrees.
Find out how in this free and practical course.
Hello jason,
please i need help on how to pass the output of the trainedEmail Address
network into a fuzzy logic system if possible a
code or link which can help understand better. Thank you
REPLY
Aditya April 2, 2017 at 3:57 pm #
Awesome Explanation
REPLY
Jason Brownlee April 4, 2017 at 9:05 am #
Thanks!
Raunak Jain April 6, 2017 at 5:20 pm # Your Start in Machine Learning REPLY
Hello Jason
I m getting list assignment index out or range error. How to handle this error?
REPLY
Jason Brownlee April 9, 2017 at 2:37 pm #
The example was developed for Python 2, perhaps this is Python version issue?
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 40/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Marco April 6, 2017 at 9:37 pm #
REPLY
Jason Brownlee April 9, 2017 at 2:40 pm #
REPLY
Jason Brownlee April 9, 2017 at 2:40 pm #
This process will help you work through your modeling problem:
http://machinelearningmastery.com/start-here/#process
REPLY
Jack April 7, 2017 at 3:42 pm #
Could you please convert this iterative implementation into matrix implementation?
REPLY
Jk April 12, 2017 at 5:04 am #
Hi Jason,
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 41/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
In section 4.1 , may you please explain why you used ### inputs = row[:-1] ### ?
Thanks
REPLY
Jason Brownlee April 12, 2017 at 7:58 am #
Yes. By default we are back-propagating the error of the expected output vs the network output
(inputs = row[:-1]), but if we are not the output layer, propagate the error from the previous layer in the
network (inputs = [neuron[‘output’] for neuron in network[i – 1]]).
You you
Thanks for your respond. I understand what can master applied
said , the part IMachine Learning
am no understanding is the
[:-1] . why eliminating the last list item ? without math or fancy degrees.
Find out how in this free and practical course.
Email Address
REPLY
Jason Brownlee April 13, 2017 at 10:10 am #
REPLY
Amer April 6, 2018 at 7:22 am #
REPLY
Prem Puri April 12, 2017 at 8:18 pm #
REPLY
Jason Brownlee April 13, 2017 at 10:01 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 42/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
delta is set in the previous code block. It is the error signal that is being propagated backward.
REPLY
Nishu March 25, 2018 at 11:32 am #
I’m sorry, but I still can’t find the location where delta is set and hence, the code gives error.
Where is the delta set for the first time?
REPLY
Prem Puri April 14, 2017 at 3:20 am #
Email Address
REPLY
youssef oumate April 26, 2017 at 4:53 pm #
START MY EMAIL COURSE
Hi Jason
Thank you very much for this awesome implementation of neural network,
I have a question for you : I want to replace the activation function from Sigmoid
to RELU . So, what are the changes that I should perform in order to get
correct predictions?
REPLY
Jason Brownlee April 27, 2017 at 8:34 am #
I think just a change to the transfer() and transfer_derivative() functions will do the trick.
Awesome !
REPLY
Jason Brownlee April 28, 2017 at 7:26 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 43/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
You’re welcome.
REPLY
Yahya Alaa April 30, 2017 at 2:38 am #
Hi Jason,
Thank you very much for this wonderful implementation of Neural Network, it really helped me a lot to
understand neural networks concept,
n_inputs = len(dataset[0]) – 1
n_outputs = len(set([row[-1] for row in dataset]))
network = initialize_network(n_inputs, 2, n_outputs)
train_network(network, dataset, 0.5, 20, n_outputs)
Your Start in Machine ×
What do n_inputs and n_outputs refer to? According to the small dataset used in this section, is n_inputs
Learning
only 2 and n_outputs only 2 (0 or 1) or I am missing something?
Is the program training the network for 500 epochs for each one of the k-folds and then testing
the network with the testing data set?
REPLY
Jason Brownlee May 4, 2017 at 8:02 am #
Hi Yahya,
That means that 5 models are fit and evaluated on 5 different hold out sets. Each model is trained
for 500 epochs. Your Start in Machine Learning
REPLY
Yahya Alaa May 4, 2017 at 8:17 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 44/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
You can use early stopping, to save network weights when the skill on a validation
set stops improving.
Email Address
REPLY
morok April 30, 2017 at 3:56 am #
In section 3.2. Error Backpropagation, where did output numbers came from for testing
backpropagation
‘output’: 0.7105668883115941
‘output’: 0.6213859615555266
‘output’: 0.6573693455986976
Your Start in Machine Learning
Perhaps from outputs on test forward propagation [0.6629970129852887, 0.7253160725279748] taking dd -
> derivative = output * (1.0 – output), problem is they don’t match, so I’m a bit lost here…
thanks!
Awesome article!!!
REPLY
Jason Brownlee April 30, 2017 at 5:34 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 45/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
In that example, the output and weights were contrived to test back propagation of error. Note
the “delta” in those outputs.
REPLY
Massa November 25, 2017 at 7:36 am #
hello Dr Jason…
I was wondering …
but I thought it gives the number of outputs…I mean the number of neurons in the
output layer.
here it’s giving the number of the dataset ….if I have 200 input/output pairs it prints 200
If there are two class values, it should print 2. It should not print the number of
examples. Your Start in Machine Learning
REPLY
Umamaheswaran May 8, 2017 at 9:49 pm #
Hi Jason,
I am using the MNIST data set to implement a handwritten digit classifier. How many training examples will
be needed to get a perfomance above 90%.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 46/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee May 9, 2017 at 7:42 am #
REPLY
Huyen May 9, 2017 at 6:32 pm #
Hi Jason,
Your blog is totally awesome not only by this post but alsoYour Start
for the whole in Machine
series about neural network. Some ×
of them explained so much useful thing than others on Internet. They help me a lot to understand the core of
Learning
network instead of applying directly Keras or Tensorflow.
Just one question, if I would like to change the result fromYou can mastertoapplied
classification Machine
regression, Learning
which part in back
without math or fancy degrees.
propagation I need to change and how?
Find out how in this free and practical course.
Thank you in advance for your answer
Email Address
REPLY
Jason Brownlee May 10, 2017 at 8:46 am # START MY EMAIL COURSE
Thanks Huyen.
You would change the activation function in the output layer to linear (e.g. no transform).
REPLY
TGoritsky May 12, 2017 at 12:41 am #
Hi Jason,
I am playing around with your code to better understand how the ANN works. Right now I am trying to do
predictions with a NN, that is trained on my own dataset, but the program returns me one class label for all
rows in a test dataset. I understand, that normalizing dataset should help, but it doesn`t work (I am using
your minmax and normalize_dataset functions). Also, is there
YouraStart
way toinreturn prediction
Machine Learningfor one-dimensional
dataset?
Here is the code (sorry for lack of formatting):
def make_predictions():
dataset = [[29,46,107,324,56,44,121,35,1],
[29,46,109,327,51,37,123,38,1],
[28,42,107,309,55,32,124,38,1],
[40,112,287,59,35,121,36,1],
[27,43,129,306,75,41,107,38,1],
[28,38,127,289,79,40,109,37,1],
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 47/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
[29,37,126,292,77,35,100,34,1],
[30,40,87,48,77,51,272,80,2],
[26,37,88,47,84,44,250,80,2],
[29,39,91,47,84,46,247,79,2],
[28,38,85,45,80,47,249,78,2],
[28,36,81,43,76,50,337,83,2],
[28,34,75,41,83,52,344,81,2],
[30,38,80,46,71,53,347,92,2],
[28,35,72,45,64,47,360,101,2]]
network = [[{‘weights’: [0.09640510259345969, 0.37923370996257266, 0.5476265202749506,
0.9144446394025773, 0.837692750149296, 0.5343300438262426, 0.7679511829130964,
0.5325204151469501, 0.06532276962299033]}],
[{‘weights’: [0.040400453542770665, 0.13301701225112483]}, {‘weights’: [0.1665525504275246,
Your
0.5382087395561351]}, {‘weights’: [0.26800994395551214, Start in Machine
0.3322334781304659]}]] ×
# minmax = dataset_minmax(dataset)
# normalize_dataset(dataset, minmax)
Learning
for row in dataset: You can master applied Machine Learning
prediction = predict(network, row) without math or fancy degrees.
print(‘Expected=%d, Got=%d’ % (row[-1], prediction)) Find out how in this free and practical course.
Email Address
REPLY
Jason Brownlee May 12, 2017 at 7:43 am #
START MY EMAIL COURSE
I would suggest exploring your problem with the Keras framework:
http://machinelearningmastery.com/start-here/#deeplearning
REPLY
Tomo May 18, 2017 at 6:22 pm #
Hi Jason!
In the function “backward_propagate_error”, when you do this:
The derivative should be applied on the activation of that neuron, not to the output . Am I right??
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 48/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Tina May 26, 2017 at 3:49 am #
Hello Jason!
REPLY
Jason Brownlee June 2, 2017 at 11:49 am #
Your Start in Machine ×
Sigmoid was the defacto standard for many years because it performs well on many different
problems.
Learning
Now the defacto standard is ReLU. You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY
Manu June 6, 2017 at 8:50 pm # Email Address
REPLY
Jason Brownlee June 7, 2017 at 7:12 am #
You are correct, but in some frameworks, transfer functions are called activation
functions:
https://keras.io/activations/
REPLY
vishwanathan May 27, 2017 at 8:08 pm #
REPLY
vishwanathan May 27, 2017 at 8:14 pm #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 49/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
I understand you do not want to take in the bias weight hence the exclusion of the last weight in
neuron. I kind of get stumped on bias.
REPLY
vishwanathan May 27, 2017 at 9:12 pm #
Thanks for the great article. In the backward propagate, the delta value is applied for each weight
across the neuron and the error is summed. I am curious why is the delta not applied to individual weights of
the neuron and the error summed for that neuron. Can you please clarify?
Learning
Why don’t you split the data into TrainData and TestData, like 80% of the dataset for training and
20% for testing, because if you train with 100% of rows of the dataset and then test some rows of the
dataset the accuracy will be good . But if you put new dataYou
on can
the master applied
seeds.csv the Machine Learning
model will work with less
accuracy, Right? without math or fancy degrees.
Find out how in this free and practical course.
Email Address
REPLY
Jason Brownlee June 2, 2017 at 12:16 pm #
Once we have the estimate and choose our model, we can fit the final model on all available data and
make predictions on new data:
http://machinelearningmastery.com/train-final-machine-learning-model/
REPLY
Josue May 29, 2017 at 11:08 am #
Thanks for the post! I have a question about cross-validation. The dataset of seeds is perfect for 5
folds but for a dataset of 211? I’ll have uniformly sized subset right? (211/5) Can you give me a suggestion
how I could handle that ?
Thanks in advanced.
REPLY
Jason Brownlee June 2, 2017 at 12:20 pm #
One way is that some records can be discarded to give even sized groups.
REPLY
Sebastián May 30, 2017 at 9:35 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 50/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee June 2, 2017 at 12:31 pm #
REPLY
Manu June 10, 2017 at 9:00 pm #
Hello Jason,
REPLY
Jason Brownlee June 13, 2017 at 8:23 am #
Consider focusing on one measure/metric that really matters in your domain, then try a
suite of framings of the problem and different algorithms to get a feeling for what might work
best.
Your Start in Machine Learning
REPLY
Yash June 18, 2017 at 6:21 pm #
I am not able to understand the above code.So, I request you to explain me the above code
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 51/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee June 19, 2017 at 8:43 am #
REPLY
Tathagat June 21, 2017 at 3:20 pm #
Hey Jason..am a novice in machine learning..have a small question…how can I track the timesteps
involved in the algorithm with accordance with the code?
Learning
What do you mean by time steps?
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY
bazooka June 29, 2017 at 6:52 am #
Email Address
Hi, Jason. I am so confused, in the result, why there are 4 set of [output,weight,delta]
like this:
START MY EMAIL COURSE
[{‘output’: 0.9999930495852168, ‘weights’: [0.9315463130784808, 1.0639526745114607,
0.9274685127907779], ‘delta’: -4.508489650980804e-09}, {‘output’: 0.9992087809233077, ‘weights’:
[-2.4595353900551125, 5.153506472345162, -0.5778256160239431], ‘delta’: 1.940550145482836e-06}]
[{‘output’: 0.01193860966265472, ‘weights’: [2.3512725698865053, -8.719060612965613,
1.944330467290268], ‘delta’: -0.0001408287858584854}, {‘output’: 0.988067899681387, ‘weights’:
[-2.2568526798573116, 8.720113230271012, -2.0392501730513253], ‘delta’: 0.0001406761850156443}]
after the backpropagation we find the optimal weights to get minimum error, what does these 4 group
means?
E
REPLY
Jason Brownlee June 29, 2017 at 7:48 am #
Your Start in Machine Learning
That is the internal state of the whole trained network.
REPLY
hassan June 29, 2017 at 7:30 am #
hi Jason
thanks for your code and good description here, i like it so much.
i run your example code and encounter with an error same others whom left note here
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 52/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee June 29, 2017 at 7:49 am #
The code was written for Python 2.7, confirm that this is your Python version.
Thanks
Jerome
REPLY
wddddds July 10, 2017 at 10:01 pm #
Thank you Jason, It’s a great tutorial and really helpful for me!
REPLY
Jason Brownlee July 11, 2017 at 10:32 am #
Thanks.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 53/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Victor July 17, 2017 at 7:50 pm #
Hi Jason,
Thanks for sharing your code. I’m a PhD candidate in machine learning, and I have a doubt about the
weights update in section 4.1:
Regards,
Your Start in Machine ×
Victor.
Learning
You can master applied Machine Learning
without math or fancy degrees. REPLY
Víctor August 4, 2017 at 11:07 pm #
Find out how in this free and practical course.
I didn’t say anything, my mistake in understanding.
Email Address
Thanks again for sharing your work.
REPLY
vishnu priya July 22, 2017 at 4:26 pm #
Hi..
Thanks for ur coding. It was too helpful. can u suggest me how to use this code for classifying tamil
characters. i have tried in cnn and now i need to compare the result with bpn. can u pls suggest me.
thank you
REPLY
Jason Brownlee July 23, 2017 at 6:20 am #
Perhaps this tutorial on classifying with a CNN would be more useful to you:
Your Start in Machine Learning
http://machinelearningmastery.com/handwritten-digit-recognition-using-convolutional-neural-networks-
python-keras/
REPLY
vishnu priya July 23, 2017 at 4:06 pm #
Thank you sir. With this tutorial i have implemented cnn sir. but for BPN i am getting error rate
687.203 sir. i dnt know what to do sir. can u help me sir.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 54/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Thank you
REPLY
Jason Brownlee July 24, 2017 at 6:49 am #
REPLY
Vishnupriya July 24, 2017 at 4:53 pm #
Classification of Tamil characters sir. I have 144 different classes. I have taken 7 glcm features of
each character and I need to train this features in backpropagation and predict the character to which class
it belongs. Your Start in Machine ×
Learning
You can master applied Machine Learning
REPLY
Jason Brownlee July 25, 2017 at 9:34 am # without math or fancy degrees.
Find out how in this free and practical course.
Sound like a great project!
Email Address
REPLY
codeo July 26, 2017 at 5:37 pm # START MY EMAIL COURSE
Hi, so I wasn’t following this tutorial when implementing my neural network from scratch, and mine
is in JavaScript. I just need help with the theory. How do I calculate the error for each node in the net so that
I can incrementally change the weights? Great tutorial btw
REPLY
codeo July 26, 2017 at 6:38 pm #
REPLY
PRABHAKARAN M July 31, 2017 at 4:31 pm #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 55/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee August 1, 2017 at 7:51 am #
REPLY
PRABHAKARAN M July 31, 2017 at 4:32 pm # START MY EMAIL COURSE
can i get the example code for dental caries detection using deep Convolutional Neural Network for
the given dataset as x ray images.
REPLY
Jason Brownlee August 1, 2017 at 7:52 am #
REPLY
John August 1, 2017 at 3:26 am #
Your Start in Machine Learning
Very nice explanation, thank you.
I have some questions.
3) What is exactly loss function in your example (I usually found some derivations of loss (cost ?) function (in
other explanations), not transfer function derivation)? Im actually very confused by notation which I find
around …
4) momentum and weight decay. In your example, you can implement them that you substract calculated
decay and add calculated momentum (to weight update) ? Again, I found forms which substract both and
weight update as w + deltaW, so again I’m mega confused by notation for backpropagation which I found…
Sorry for dumb questions, … math is not my strong side, so many things which can be inferred by math
sense are simply hidden for me.
REPLY
John August 1, 2017 at 3:30 am #
Your Start in Machine ×
*substract both and weight update as w + deltaW, so again
Learning
I found above sentence as nonsense, must be side effect of my confusion …
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY
Jason Brownlee August 1, 2017 at 8:12 am #
Email Address
Hang in there.
Pick one tutorial and focus on it. Jumping from place to place will make things worse for sure.
START MY EMAIL COURSE
REPLY
Jason Brownlee August 1, 2017 at 8:10 am #
Loss is prediction error. You can change this to other forms like MAE or MSE.
No decay or momentum in this example. Easy to add if you want. There are many ways to dial in the
learning process. No hard and fast rules, just some norms that people reuse.
REPLY
Parminder Kaur August 6, 2017 at 7:50 pm #
Thanks
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 57/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee August 7, 2017 at 8:41 am #
The CNN will perform feature extraction automatically, you could explore using different filters
on the data to see if it helps the network.
The number of layers and neurons/filters per layer must be found using trial and error. It is common to
copy the designs from other papers as a starting point.
REPLY
Vatandas August 15, 2017 at 3:28 am #
1. I expect that this code is deep learning (many hidden layer) but not. One sentence is easy (“you
can add more hidden layer as explained”) but to do is not as easy as you said.
I mean you use the same ‘output’ variable both error and delta. But in error it must be activated one, in delta
it must be NONactivated one.
Your Start in Machine Learning
REPLY
A Researcher May 2, 2019 at 3:10 am #
REPLY
8CG_256 August 18, 2017 at 9:02 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 58/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Nice tutorial, very clean code and beginner-friendly. Thank you very much!
REPLY
Jason Brownlee August 18, 2017 at 4:36 pm #
REPLY
8CG_256 August 18, 2017 at 9:26 pm #
I only have one slight issue: I implemented this in Ruby and I tried to train it using the IRIS
dataset, keeping the network simple (1 input layer, 1 hidden layer, 1 output layer) and after decreasing
Your
for a while the error rate keeps increasing. I tried lowering the Start
learning in
rate,Machine
even making it dynamic so it
×
decreases whenever the error increases but it doesn’tLearning
seem to help. Could you give me some advice?
P.S sorry for my bad English
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY
Jason Brownlee August 19, 2017 at 6:19 am #
Email Address
Here is an example of backprop I developed in Ruby:
http://cleveralgorithms.com/nature-inspired/neural/backpropagation.html
START MY EMAIL COURSE
REPLY
Derek Martins August 22, 2017 at 9:22 pm #
Hi Jason, I enjoy so much your tutorials. Can you do a tutorial implementing BackPropagation
Through Time? Thanks man.
REPLY
Jason Brownlee August 23, 2017 at 6:50 am #
REPLY
Anubhav Singh August 24, 2017 at 1:08 pm #
Hello Jason,
I would like to know how I can obtain the weight*input for every single neuron in the network…
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 59/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
but the activation variable here is a single value…what I understand is that if I have set n_hidden = 5
(number of hidden layers), I should get N*5 (N = number of features in the dataset) outputs if I print the
activation…
Kindly help
In this case of the example, you have chosen digital outputs [0,1]
and hence it may not have come up .. but my point is…
one is already subjected to a transfer function, and one is not.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 60/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
RealUser404 September 6, 2017 at 1:36 pm #
I have a question concerning the back-propagation : what if instead of having an error function I only have a
desired gradient for the output (in the case of an actor-critic model for example)?
How can I change your backprop function to make it work? Or can I just use the gradient as the error?
REPLY
Jason Brownlee September 7, 2017 at 12:49 pm #
Sorry, I don’t follow, perhaps you can restate your question with an example?
Your Start in Machine ×
Learning
You can master applied Machine Learning REPLY
user28 September 8, 2017 at 9:26 pm #
without math or fancy degrees.
Hi Jason , thank you for providing this tutorial. I’mFind out howofinhow
confused this can
free Iand practical the
implement course.
same
backpropagation algorithm with output not binary. Since I noticed that your example has binary output. Like
predicting for stock price given the open, high, low and close values.
Email Regards.
Address
REPLY
Lewis September 11, 2017 at 2:11 am #
Hi Jason,
Want I wanted to try was to withhold say 5 rows from the dataset and have the trained network predict the
Your
results for those rows. these is is different from what I think the Start in Machine
example Learning
does which is rolling predictions
with the learning. Removing 5 rows from the dataset is of course easy but my pitiful attempts at predicting
with unseen data like below fail ((I guess network is not in scope at the end): any help appreciated!
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 61/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
[11.84,13.21,0.8521,5.175,2.836,3.598,5.044],
[12.3,13.34,0.8684,5.243,2.974,5.637,5.063]]
REPLY
Jason Brownlee September 11, 2017 at 12:08 pm #
I would recommend starting with Keras rather than coding the algorithm from scratch.
Start here:
https://machinelearningmastery.com/start-here/#deeplearning
Your Start in Machine ×
Learning
You can master applied Machine Learning REPLY
Karim September 14, 2017 at 1:27 pm #
without math or fancy degrees.
Find outtohow
Hi Jason, I am trying to generalize your implementation in this
work withfree and practical
a variable numbercourse.
of layers
and nodes. However, whenever I try to increase the number of nodes too much it stops working (the network
freezes at one error rate and all output nodes are active, i.e. giving
Email 1). Although the code would work if I
Address
decreased the layers and the errors will go down.
Is there something I am missing when using too many layers? The concepts should be the same.
START MY EMAIL COURSE
I trained a network with 4 layers: [14,10,10,4] and it worked.
I trained a network with 4 layers [14,100,40,4] and it is stuck. Same dataset.
Thanks
REPLY
Jason Brownlee September 15, 2017 at 12:10 pm #
REPLY
Laksh October 4, 2017 at 11:11 pm #
REPLY
Jason Brownlee October 5, 2017 at 5:24 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 62/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Sure.
REPLY
dsliver33 October 9, 2017 at 1:52 pm #
I’m trying to alter the code to represent a regression problem (sigmoid on hidden layer, linear on output
layer). As far as I know, the main part of the code that would have to be modified is the FF algorithm. I’ve
rewritten the code as below:
With this code, I’m getting an “OverflowError: (34, ‘Result too large’)” error. Could you please tell what I’m
doing wrong? All the other parts of the code are as you’ve written.
REPLY
Jason Brownlee October 9, 2017 at 4:47 pm #
What did you change exactly? Can you highlight the change for me?
I got the hidden layer (network[0]), and I applied your algorithm (calculate activation, transfer the
activation to the output, append that to a new list called “new_inputs”).
After that, I get the output layer (network[-1]), I calculate the activation with the “new_inputs”, but I
do NOT apply the sigmoid transfer function (so, the outputs should be linear). The results are
appended to a new list, which is set to be the return of the function.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 63/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Would that be the best way to remove the sigmoid function from the output layer, making the code a
regression, instead of a classification?
REPLY
Jason Brownlee October 10, 2017 at 7:52 am #
Sounds good. I don’t have any good ideas, I’d recommend stepping through some
calculations to help spot where it is going wrong.
You may want to consider moving to an open source neural net library, such as Keras:
https://machinelearningmastery.com/start-here/#deeplearning
I GOT IT TO WORK!!! You have to normalize your output data. Then you can apply the transfer
START MY EMAIL COURSE
function to the output layer just the same! After that it will work!
REPLY
Steven August 20, 2019 at 8:40 pm #
REPLY
Chris October 12, 2017 at 11:27 am #
REPLY
Asad October 14, 2017 at 3:24 pm #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 64/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
please tell me how we can change the neuron in hidden layer and in output layer?
and what will be the result when we change the neuron in hidden layer and in output layer?
in this tutorial u take one hidden layer,so can we use more than one hidden layer? and how?
REPLY
Jason Brownlee October 15, 2017 at 5:19 am #
Perhaps you would be better served by starting with a neural network library such as Keras:
https://machinelearningmastery.com/tutorial-first-neural-network-python-keras/
When I try to run the code, it shows the error below. Do you have any idea why?
in backward_propagate_error(network, expected)
78 error = 0.0
79 for neuron in network[i + 1]:
—> 80 error += (neuron[‘weights’][j] * neuron[‘delta’])
81 errors.append(error)
82 else:
Your Start in Machine Learning
IndexError: list index out of range
REPLY
dna_remaps February 3, 2018 at 10:43 pm #
You need to add a conditional after your first layer to make sure your subsequent hidden layer weights
have the proper dimensions (n_hidden+1, n_hidden)
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 65/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
for i in range(n_layers):
hidden_layer = [{‘weights’:[random() for i in range(n_inputs + 1)]} for i in range(n_hidden)]
if i > 0:
hidden_layer = [[{‘weights’:[random() for i in range(n_hidden + 1)]} for i in range(n_hidden)]
network.append(hidden_layer)
REPLY
Arijit Mukherjee October 17, 2017 at 1:40 am #
Hi,
In the output/last layer when we are calculating the backprop error why are we multiplying with the transfer
derivative with the (expected-output)?? transfer derivative is already canceled out for the the last layer , the
Your
update should be only (expected-output)*previous_layer_input Start in Machine
, ??? ×
Thanks
Learning
You can master applied Machine Learning
without math or fancy degrees.
REPLY
Tanoh Henry October 18, 2017 at 8:54 pm # Find out how in this free and practical course.
REPLY
Liam October 21, 2017 at 5:41 am #
Thanks for the tutorial! I am trying to modify your code to do a regression model and I am stuck. I
have an input data set (4 columns and many rows) and a single variable output data set (in range of tens of
thousands). I fed them into the train procedure and I get an error when it reaches “expected = [0 for i in
range(n_outputs)]” in the train portion. The error reads “only length-1 arrays can be converted to Python
scalar”. Now I understand this is because of the intended purpose for the code was a categorization problem
but I am wondering what I would need to modify to get thisYour
to work?
StartAny help would
in Machine go a long way as I have
Learning
been stuck on this issue for some time now.
REPLY
Jason Brownlee October 21, 2017 at 5:45 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 66/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Sam October 26, 2017 at 11:15 pm #
Hi
I am implementing a 2 layer neural network with 100 hidden units in the first layer and 50
in the next using your code. Implement sigmoid activation function in each layer. Train/test your
model on the MNIST dataset subset.
But it is always giving same prediction.
[0.99999999986772, 0.99999999994584]
Expected=0, Got=1
Your Start in Machine ×
[0.99999999986772, 0.99999999994584]
Expected=1, Got=1 Learning
[0.99999999986772, 0.99999999994584]
You can master applied Machine Learning
Expected=1, Got=1
without math or fancy degrees.
[0.99999999986772, 0.99999999994584]
Find out how in this free and practical course.
Expected=1, Got=1
[0.99999999986772, 0.99999999994584]
Expected=1, Got=1 Email Address
[0.99999999986772, 0.99999999994584]
Expected=0, Got=1
START MY EMAIL COURSE
[0.99999999986772, 0.99999999994584]
Expected=0, Got=1
[0.99999999986772, 0.99999999994584]
Expected=1, Got=1
[0.99999999986772, 0.99999999994584]
Expected=0, Got=1
[0.99999999986772, 0.99999999994584]
Expected=0, Got=1
[0.99999999986772, 0.99999999994584]
Expected=0, Got=1
[0.99999999986772, 0.99999999994584]
Expected=0, Got=1
REPLY
Jason Brownlee October 27, 2017 at 5:20 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 67/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Matthias April 15, 2018 at 2:57 am #
Weights should be initialized with normally distributed random values. Try using random.gauss
for weight initialization.
REPLY
John October 28, 2017 at 6:52 pm #
This is helping me a lot with a college work. But in this NN, how can I set manually not the number of input
neuros, the input values?
For example, if I have 1 input neuro, I wan’t to set this value to 0.485.
Best regards!
REPLY
Jason Brownlee November 13, 2017 at 10:11 am #
REPLY
yesta November 17, 2017 at 8:21 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 68/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Hi, Jason
Thank you for this amazing tutorial!
I have a question that may be out of the topic. How do you call models or type of DL models where you feed
a model with new test data in order to make the model adaptive to the environment?
Thank you.
REPLY
Jason Brownlee November 17, 2017 at 9:31 am #
I would appreciate it if you could help with this issue so that I could get out of where I left off.
Your posts are really very good there is where I find my way in to learning in Machine Learning.
Best Regards
REPLY
Jason Brownlee December 7, 2017 at 7:51 am #
The final model (e.g. trained) only needs to perform the forward pass.
Understood.
Thank you Dr. Jason
REPLY
MohamedElshazly December 8, 2017 at 9:30 pm #
1 for i in reversed(range(len(network))):
2 layer = network[i]
3 errors = list()
4 if i != len(network)-1:
5 for j in range(len(layer)):
6 error = 0.0
7 for neuron in network[i + 1]:
wouldn’t the last line be out of range because the current ‘ i ‘ is the last one and i can’t go beyond it by 1 ?
thanks in advance
REPLY
Jason Brownlee December 9, 2017 at 5:41 am #
Thank you for your tutorial. The training for the example worked however when I try to implement the code
for the Wheat Seeds Dataset I get an error from my line 210:Email Address
Can you please explain why it is (dataset[0])? Does (dataset[0]) means the 1st column in the dataset?
REPLY
Jason Brownlee December 9, 2017 at 5:45 am #
The example was written for Python 2.7, confirm your Python version.
Hello Jason,
Fantastic stuff here. I had a question about the network configuration. This 2 input, 2 hidden and 2 output
seems a bit odd to me. I’m used to seeing 2, 2, 1 for XOR – can you explain why you have two output nodes
and how they work with the max function? I think it would better explain this line for me in train():
expected[row[-1]] = 1
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 70/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Thanks!
REPLY
Jason Brownlee December 12, 2017 at 4:12 pm #
The final model has the shape [7, 5, 3]. Perhaps check the tutorial again? It has 3 outputs, one
for each of the 3 classes in the dataset.
Configuration was chosen via trial and error. There is no analytical way to choose a configuration for a
neural network.
Finally, you can learn more about array indexing in Python here:
Your Start in Machine
https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/ ×
Learning
You can master applied Machine Learning
REPLY
Mohamed Elshazly December 17, 2017 at 9:34 pm # without math or fancy degrees.
Find out how in this free and practical course.
Hi Jason
looking forward to your response and thanks for the Great Tutorial
REPLY
Jason Brownlee December 18, 2017 at 5:22 am #
REPLY
MohamedElshazly December 18, 2017 at 4:55 pm #
HI again Jason
If I’m implementing this algorithm in python 3 what should i change in expected[row[-1]]=1 in order for it to
work because I’m having this error : list assignment index out of range
thanks in advance
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 71/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee December 19, 2017 at 5:15 am #
I don’t know off the cuff, I will look into porting the example to Py3 in the new year.
REPLY
Tushar December 19, 2017 at 5:29 pm #
You are just awesome Jason. You are adding more value to people’s ML skills than most average
graduate schools do in the US.
Thanks a ton!
Wow, thanks for your codes. I have a question, what if I want to add regularisation term like L2
START MY EMAIL COURSE
during back propagation, what should i do?
REPLY
Jason Brownlee January 6, 2018 at 5:55 am #
REPLY
mark January 7, 2018 at 5:12 pm #
Thanks for replying. I know the keras and have been using keras for a while. But in the
Your
problem I am focusing on, I need to make changes Start
on the in Machine
back Learning
propagation. That’s why I didn’t use
keras.
So let’s go back to my original question, is the error term the cost function? Thanks.
REPLY
Jason Brownlee January 8, 2018 at 5:42 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 72/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Mojo January 20, 2018 at 7:04 pm #
Hello Jeson,
Thanks for the informative tutorial. I have a question.
if i want to change the error equation and as well as the equation between input with hidden and hidden with
output layer. How can i change it?
Hope you will reply in a short time.
Regards,
Mojo
Email Address
REPLY
Aliya Anil February 16, 2018 at 9:04 pm #
START MY EMAIL COURSE
Hi Jason,
It was indeed a very informative tutorial. Could you please explain the need for seed(1) in the code?
REPLY
Jason Brownlee February 17, 2018 at 8:44 am #
I am trying to tie down the random number generator so that you get the same results as me.
Hey there,
Been following your tutorial and I’m having problems with using my dataset with it. The outputs of the hidden
neurons appear to only be exactly 1 constantly. I’m not sure what’s wrong exactly or how to fix it but its
resulting in the network not learning at all. Please let me know if you can help.
Thanks,
Raj
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 73/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee February 19, 2018 at 9:10 am #
Perhaps try to get the code and data in the tutorial working first and use that as a starting point
for your own problem.
Generally, I would recommend using a library like Keras for your own projects and only code methods
from scratch as a learning exercise.
REPLY
Aliya Anil February 20, 2018 at 4:37 pm #
Hi,
Your
I tried the first code in the tutorial with 4-parameter dataset, but it isStart in Machine
not predicting like the 2-parameter set.
×
Could you explain the reason? Learning
Thanks, You can master applied Machine Learning
Aliya without math or fancy degrees.
Find out how in this free and practical course.
If you are looking to develop a neural network for your own data, I would recommend the Keras
START MY EMAIL COURSE
library:
https://machinelearningmastery.com/start-here/#deeplearning
REPLY
Nik March 2, 2018 at 1:54 pm #
Dear Jason,
Can I use the codes for handwritten digits recognition? If yes, are there any special recommendations what
to change in the codes or I can use them with no changes?
Thanks,
Nik
Your Start in Machine Learning
REPLY
Jason Brownlee March 2, 2018 at 3:25 pm #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 74/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Nik March 2, 2018 at 7:51 pm #
Yes, I have seen that tutorial. But is there any way to use the codes from this tutorial? I just
would like to understand why they work so well for seeds and do not work for handwritten digits…
REPLY
Jason Brownlee March 3, 2018 at 8:10 am #
Yes, you can. You can develop a model with all pixels as inputs.
I cannot write the modification for you, sorry, I just don’t have the capacity.
Email Address
REPLY
Jason Brownlee March 4, 2018 at 6:04 am # START MY EMAIL COURSE
The code in this tutorial is to teach you about backprop, not for use on real problems. If you are
working through a problem, I’d recommend using Keras.
REPLY
Jean-Michel Richer March 5, 2018 at 9:27 pm #
Dear Jason,
I have tried to use your code on a simple XOR example but get a result of [0, 0, 1, 1] instead of [0,1,1,0]
Scores: [0.0]
Mean Accuracy: 0.000%
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 75/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Would you have some explanation because I can not figure out why it is not working ?
Best regards,
JM
REPLY
Jason Brownlee March 6, 2018 at 6:12 am #
Your Start in Machine ×
Perhaps the model requires tuning to your new dataset.
Learning
You can master applied Machine Learning
without math or fancy degrees. REPLY
Tanveer March 5, 2018 at 9:29 pm #
Find out how in this free and practical course.
Thank You So Much Jason !! Wonderful Tutorial. THANKS Much !!
Email Address
You’re welcome.
REPLY
Mojo March 9, 2018 at 10:06 pm #
If i want to calculate the training accuracy and F-measure and want to change the activation
function, how i can do it?
REPLY
Jason Brownlee March 10, 2018 at 6:28 am #
Your Start in Machine Learning
Perhaps you would be better off using scikit-learn and Keras instead.
REPLY
Fahad March 12, 2018 at 8:03 pm #
Is there something wrong with this code in case of using MINIST data? I tried to change the
structured of the data to be compatible with the code, but it gave me a huge error and the error did not
decrees during all training steps
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 76/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee March 13, 2018 at 6:25 am #
The code was not developed for MNIST. Here is an example of working with MNIST:
http://machinelearningmastery.com/handwritten-digit-recognition-using-convolutional-neural-networks-
python-keras/
REPLY
Fahad March 13, 2018 at 4:06 pm #
Thanks Jason for your response. I want to apply the code without keras. I tried to change the
Your
structure of the data to be each row as a vector of 784 pixel Start
followed in Machine
by a class label, but as I said it gave a ×
huge error and does not decrees at all.
Learning
I am trying to develop some algorithm for enhancing of learning, hence, I need to deal with the procedure as
step by step. So keras or any other library does not help. You can master applied Machine Learning
without math or fancy degrees.
Thanks again Jason Find out how in this free and practical course.
Email Address
REPLY
Jason Brownlee March 14, 2018 at 6:17 am #
START MY EMAIL COURSE
Perhaps update the code to use numpy, it will be much faster.
REPLY
kelvin March 15, 2018 at 2:39 am #
Hi Mr Brownlee,
Can you teach me how to plot the errors per epochs (validation error) and accuracy for both training and
validation in your scratch network?
REPLY
kelvin March 15, 2018 at 2:44 am #
Your Start in Machine Learning
I only can find the training error but not validation error in the code. For the accuracy, I plot a
graph have a straight line only.
REPLY
Jason Brownlee March 15, 2018 at 6:33 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 77/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
kelvin March 15, 2018 at 12:19 pm #
Is there any possible way to do it on your scratch network? for example which part of the
code save the training error, validation error, training accuracy and validation accuracy? So I can
plot the graph myself since your scratch model does not have “model” for me to save the history.
REPLY
Jason Brownlee March 15, 2018 at 2:50 pm #
Yes, perhaps change it from CV to a single/train test, then evaluate the model skill on
each dataset at the end of each epoch. Save Your Start
the results inandMachine
in a list return the lists. ×
Learning
You can master applied Machine Learning REPLY
Zahra May 6, 2019 at 9:37 am # without math or fancy degrees.
Find out how in this free and practical course.
Hello, I’m so confuse..
I try to run this code in command prompt. But, I use my dataset (not Wheat Seeds dataset).
Email Address
And why this happened? What’s wrong? What should I do? What should I change?
Please, help me!
START MY EMAIL COURSE
Traceback (most recent call last):
File “journal.py”, line 197, in
scores = evaluate_algorithm(dataset, back_propagation, n_folds, l_rate, n_epoch, n_hidden)
File “journal.py”, line 81, in evaluate_algorithm
predicted = algorithm(train_set, test_set, *args)
File “journal.py”, line 173, in back_propagation
train_network(network, train, l_rate, n_epoch, n_outputs)
File “journal.py”, line 150, in train_network
expected[row[-1]] = 1
IndexError: list assignment index out of range
REPLY
Jason Brownlee May 6, 2019 at 2:33 pm
Your
# Start in Machine Learning
REPLY
Jack March 15, 2018 at 12:11 pm #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 78/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Can I use this model for regression problem? For example us this model for boston house-prices
dataset?
REPLY
Jason Brownlee March 15, 2018 at 2:50 pm #
Sure, some changes would be required, such as the activation in the output layer would need
to be linear.
REPLY
Nabil March 15, 2018 at 3:49 pm #
REPLY
Olu March 19, 2018 at 11:23 pm # START MY EMAIL COURSE
Can you please explain how this expected[row[-1]] = 1 knows where to insert the 1 in the arrays of zero
created.
Good question.
expected is all zeros. row[-1] is the index of the class value. therefore we set the index of the class value
in expected to 1.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 79/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
kmillen November 16, 2018 at 11:27 am #
Jason,
This is an amazing piece of code that has been very beneficial.
Doesn’t that mean that only expected[0] and expected[1] will ever be set to 1 for this test data?
Thank you,
REPLY
Jason Brownlee November 16, 2018 at 1:58 pm #
Your
Sorry, I don’t understand your question? Start in Machine ×
Learning
You can master applied Machine Learning
kmillen November 17, 2018 at 1:44 amwithout
# math or fancy degrees.
Find out how in this free and practical course.
If I understand Python (which I may not), row[-1] represents the last item in the row.
Since the last value in each of the 10 rows is only either 0 or 1, expected[row[-1]] = 1 will
Email Address
only ever set expected[0] or expected[1] to the value of 1. Or, what am I missing?
If so:
Therefore row[-1] is an index of either 0 or 1 and we are marking the value in expected at
that index as 1.
REPLY
kmillen November 17, 2018 at 2:54 am #
Disregard my previous question; I found the answer in a previous reply. Thank you
again for this example.
REPLY
kelvin March 21, 2018 at 2:14 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 80/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Hi, I would like to use softmax as the activation function for output layer. However, I do not know
how to write the code for the derivative of softmax. Can you show me the code how to change the
sigmoid function from your code to softmax?
REPLY
kelvin March 21, 2018 at 2:20 am #
I do try few ways to change the sigmoid to softmax, however, all of them are not working. Can
you show me how to create a softmax layer?
for transfer():
first case:
def transfer(input_value):
exp_scores = np.exp(input_value) Your Start in Machine ×
return exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
Learning
second case:
You can master applied Machine Learning
def transfer(input_value):
without math or fancy degrees.
input_value -= np.max(input_value)
Find out how in this free and practical course.
return np.exp(input_value) / np.sum(np.exp(input_value))
for transfer_derivative():
first case:
def transfer_derivative(output):
s = output.reshape(-1, 1)
return np.diagflat(s) – np.dot(s, s.T)
second case:
def transfer_derivative(output):
jacobian_m = np.diag(output)
for i in range(len(jacobian_m)):
for j in range(len(jacobian_m)):
if i == j:
Your Start in Machine Learning
jacobian_m[i][j] = output[i] * (1 – output[i])
else:
jacobian_m[i][j] = -output[i] * output[j]
return jacobian_m
REPLY
Jason Brownlee March 21, 2018 at 6:39 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 81/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee March 21, 2018 at 6:38 am #
REPLY
Suede March 29, 2018 at 6:40 am #
hey Jason, this is very helpful. I have run the code but i keep on getting this error, can you please
help me out? the error is: Your Start in Machine ×
NameError Traceback (most recent call last) Learning
in ()
You can master applied Machine Learning
186 str_column_to_float(dataset, i)
without math or fancy degrees.
187 # convert class column to integers Find out how in this free and practical course.
–> 188 str_columnto_int(dataset, len(dataset[0])-1)
189 # normalize input variables
Email Address
190 minmax = dataset_minmax(dataset)
REPLY
Jason Brownlee March 29, 2018 at 6:42 am #
The code was written for Python 2.7, confirm that you are using this version of Python?
REPLY
Fahri Güreşçi April 15, 2018 at 7:03 am #
The csv file is not working. Edited csv file > bit.ly/2GYX2dF
you can use python 2 or 3
results:
python2 > Mean Accuracy: 95.238% Your Start in Machine Learning
python3 > Mean Accuracy: 93.333%
Why different?
REPLY
Jason Brownlee April 16, 2018 at 6:00 am #
Wh is the CSV file not working? The link appears to work fine.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 82/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Good question re the difference, no idea. Perhaps small differences in the API? The code was written for
Py2, so it may require changes for py3.
REPLY
Fahad April 18, 2018 at 8:58 pm #
I have altered the code to work with MNIST (digit numbers) , the problem I have faced that
forward_propagate function returns [1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ] for each instance !
Any help
Email Address
REPLY
Fahad April 19, 2018 at 7:09 am # START MY EMAIL COURSE
REPLY
Jason Brownlee April 19, 2018 at 2:45 pm #
REPLY
Fahad April 19, 2018 at 8:17 pm #
REPLY
Jason Brownlee April 20, 2018 at 5:48 am #
Perhaps tune the model to your specific problem. I have some suggestions here:
http://machinelearningmastery.com/improve-deep-learning-performance/
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 83/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Fahad April 23, 2018 at 5:34 am #
I altered the code to work with XOR problem and it was working perfectly. Then, I altered the code
to work with digit numbers MNIST, but as I told you there was a problem with the the forward_propagation
function that it returned all outputs to be [1,1,1,…] instead of a probabilities for each output.
I think it is not an optimization problem, there is something wrong with the forward_propagate function.
Here is the code after alteration [it is working but with a fixed error during all training epochs
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 84/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
activation = weights[-1]
for i in range(len(weights)-1):
activation += weights[i] * inputs[i]
return activation
return inputs
for i in range(len(network)):
inputs = row[1:]
if i != 0:
inputs = [neuron[‘output’] for neuron in network[i – 1]]
for neuron in network[i]:
for j in range(len(inputs)):
neuron[‘weights’][j] += l_rate * neuron[‘delta’] * inputs[j]
neuron[‘weights’][-1] += l_rate * neuron[‘delta’]
# Initialize a network
def initialize_network(n_inputs, n_hidden, n_outputs): Your Start in Machine Learning
global network
network = list()
hidden_layer1 = [{‘weights’:[random() for i in range(n_inputs + 1)]} for i in range(n_hidden)]
network.append(hidden_layer1)
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 86/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
# evaluate algorithm
n_folds = 5 START MY EMAIL COURSE
l_rate = 0.5
n_epoch = 100
n_hidden = 500
REPLY
Jason Brownlee April 23, 2018 at 6:26 am #
Sorry, I don’t have the capacity to debug the modified code for you.
Your Start in Machine Learning
REPLY
Rahmad ars April 26, 2018 at 3:23 am #
https://stackoverflow.com/questions/50027886/need-help-for-check-my-backprop-ann-using-python
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 87/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee April 26, 2018 at 6:37 am #
REPLY
Rahmad ars April 26, 2018 at 7:19 am #
your original code sir, only have 1 hidden layer with 2 neurons. then, I modify it, so the ANN have 3
hidden layers, each consist of (128, 64, 32). and i have my own dataset, so i change it (the dataset and input
neurons). when i run this code, everything looks fine but the error value is not changing…
REPLY
Jason Brownlee April 26, 2018 at 2:59 pm #
Email Address
If you are struggling with the code, I would recommend not coding the algorithm from scratch.
REPLY
Fahad April 28, 2018 at 9:20 pm #
The same problem occurs when you change the original code from 5 neurons in the hidden layer to 31
neuron ( the error value does not change).
I know 31 hidden neuron is not a right number of neurons for seed data set. But I would like to know what is
the wrong when you increase the number of neurons.
Logically, it should be fine and the error value decreases. But when you change the number of neurons to 30
Your Start in Machine Learning
it is still working , when change it to 31 neurons it does not decrease !
I think if this problem is fixed, then the problem of Rahmad will be fixed too.
REPLY
Jason Brownlee April 29, 2018 at 6:26 am #
Perhaps the model requires tuning to your specific problem (e.g. layers, nodes, activation
function, etc.)
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 88/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Rocha May 2, 2018 at 9:58 pm #
That line is giving me this: TypeError: list indices must be integers or slices, not str
Email Address
Should be the python version? I’m using python 3…
REPLY
Jason Brownlee May 3, 2018 at 6:33 am #
REPLY
Kamrun Nahar Nisha May 8, 2018 at 4:00 pm #
seed(1)
# load and prepare data
Your Start in Machine Learning
filename = ‘seeds_dataset.csv’
dataset = load_csv(filename)
for i in range(len(dataset[0])-1):
str_column_to_float(dataset, i)
# convert class column to integers
str_column_to_int(dataset, len(dataset[0])-1)
# normalize input variables
minmax = dataset_minmax(dataset)
normalize_dataset(dataset, minmax)
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 89/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
# evaluate algorithm
n_folds = 5
l_rate = 0.3
n_epoch = 500
n_hidden = 5
scores = evaluate_algorithm(dataset, back_propagation, n_folds, l_rate, n_epoch, n_hidden)
print(‘Scores: %s’ % scores)
print(‘Mean Accuracy: %.3f%%’ % (sum(scores)/float(len(scores))))
In this part of your code I also want to print the error and it will be like
please tell me the code . Using breast cancer dataset not Your
wheatStart
seedindataset.
Machine I am not so good in coding
Learning
that’s why I need your help immediately.
REPLY
Jason Brownlee May 9, 2018 at 6:09 am #
I’m eager to help, but I do not have the capacity to outline the changes or write the code for
you.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 90/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Akefar May 11, 2018 at 4:41 am #
hi Jason,
I tried your code in my data set ,shape of my data is (576,16) .the problem is
is there any need to change your code for (576,16) data shape .
Thanks
—————————————————————————
IndexError Traceback (most recent call last)
in ()
195 n_epoch = 500
196 n_hidden = 1 Your Start in Machine ×
–> 197 scores = evaluate_algorithm(dataset, back_propagation, n_folds, l_rate, n_epoch, n_hidden)
Learning
198 print(‘Scores: %s’ % scores)
199 print(‘Mean Accuracy: %.3f%%’ % (sum(scores)/float(len(scores))))
You can master applied Machine Learning
without math or fancy degrees.
in evaluate_algorithm(dataset, algorithm, n_folds, *args)
Find out how in this free and practical course.
79 test_set.append(row_copy)
80 row_copy[-1] = None
—> 81 predicted = algorithm(train_set, test_set, *args) Email Address
82 actual = [row[-1] for row in fold]
83 accuracy = accuracy_metric(actual, predicted)
START MY EMAIL COURSE
in back_propagation(train, test, l_rate, n_epoch, n_hidden)
171 n_outputs = len(set([row[-1] for row in train]))
172 network = initialize_network(n_inputs, n_hidden, n_outputs)
–> 173 train_network(network, train, l_rate, n_epoch, n_outputs)
174 predictions = list()
175 for row in test:
REPLY
Jason Brownlee May 11, 2018 at 6:39 am #
You may need to change your data to match the model or the model to match the data.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 91/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Hi Jason, I tried your code on the same sample dataset, i am getting the following type error in the
function activate. I am doing it in python3.6. hope to hear from you soon
Email Address
REPLY
Deepak D May 17, 2018 at 6:49 pm # START MY EMAIL COURSE
Hi Jason Brownlee,
I tried your code and experienced the some error applying the Backpropagation algorithm to the wheat
seeds dataset. I am using python 2.7.
Error type:
REPLY
Jason Brownlee May 18, 2018 at 6:22 am #
REPLY
Dhanya Hegde May 19, 2018 at 1:53 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 92/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Hey Jason! Great work. Really helpful. I didn’t understand one part of your code. On what basis
does predict function return the predicted value as 0 or 1, after taking the maximum of the two
output neuron values?
REPLY
Jason Brownlee May 19, 2018 at 7:44 am #
REPLY
Dhanya Hegde May 21, 2018 at 3:37 am #
Email Address
REPLY
Jason Brownlee May 21, 2018 at 6:35 am #
START MY EMAIL COURSE
As stated in the text above the code, it returns an integer for the class with the largest
probability.
REPLY
Ionut May 27, 2018 at 12:05 am #
Hi,
I’m a beginner in neural networks and I don’t understand the dataset from the section “4.2. Train Network”.
Can anyone explain me what x1, x2 and y means?
REPLY
Rishik Mani May 28, 2018 at 7:26 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 93/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Hi Jason, thank you for the highly informative post. But could you please clarify me upon this petty
little issue.
In section 4.2 Train network, you considered n_inputs = len(dataset[0]) – 1. Why did you put a -1 here, while
the number of the inputs should exactly be of the length of the dataset.
REPLY
Jason Brownlee May 28, 2018 at 2:32 pm #
Hi,
I guess I’m likely mistaken, so please but when i != 0, isn’t the last line updating the last weight for the
Your Start in Machine Learning
second time?
So, shouldn’t it be “inputs = [neuron[‘output’] for neuron in network[i – 1]][:-1]” (add “[:-1]]” at the end)?
If I’m wrong, I’ll read the code again more carefully, so please let me know.
REPLY
Jason Brownlee June 5, 2018 at 6:46 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 94/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
No. There are more weights than inputs and the -1 index of the weights is the bias.
REPLY
Kie Woo Nam June 5, 2018 at 7:06 pm #
REPLY
Jason Brownlee June 6, 2018 at 6:39 am #
Your Start in Machine ×
No problem.
Learning
You can master applied Machine Learning
without math or fancy degrees. REPLY
Thomas Specht July 10, 2018 at 4:46 am # Find out how in this free and practical course.
Hi Jason,
Email Address
Great tutorial to get into ML coding. I have one question:
What library would you recommend for projects and why? I want to use NN for regression problems.
START MY EMAIL COURSE
REPLY
Jason Brownlee July 10, 2018 at 6:53 am #
I recommend Keras because it is computationally efficient, fast for development and fun:
https://machinelearningmastery.com/start-here/#deeplearning
REPLY
Hugo B. August 13, 2018 at 1:29 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 95/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Hello Sir
Thank you so much for this article.
Can you please tell me how i can solve this error:
thankyou
Email Address
REPLY
STEPHEN OLADEJI August 28, 2018 at 10:35 pm #
REPLY
Jason Brownlee August 29, 2018 at 8:11 am #
REPLY
Tamara September 11, 2018 at 5:36 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 96/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee September 11, 2018 at 6:34 am #
REPLY
ritu September 23, 2018 at 9:49 am #
How should I modify to code to always run for one output neuron in the output layer?
eg. if the output class consist of only 2 output classes ‘1’ and ‘2’ , as per the above code 2 neurons will be
created within the output layer, but what if I wanted a neural network to just have one neuron in the output
layer.
REPLY
Parva September 27, 2018 at 9:04 am #
Why is there just one output coming from layer 1 though it contains 2 neurons. Shouldn’t there be 2
outputs one from each neuron?
REPLY
Brian September 28, 2018 at 1:17 am #
Thank you for this example. It has helped me get past the block I had with the
mathematical based descriptions and differential calculus related to back propagation.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 97/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
As I have been focusing on the back propagation portion of this example I have come up with an
alternative version of the ‘backward_propagate_error’ function that I think is a much more succinct
and logical way to write this function.
Cool!
Email Address
Sorry, I don’t have the capacity to review your code.
REPLY
Brian October 4, 2018 at 2:59 am #
REPLY
Jason Brownlee October 4, 2018 at 6:20 am #
REPLY
KS October 10, 2018 at 2:15 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 98/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee October 10, 2018 at 6:15 am #
Your Start in Machine ×
Thanks for the suggestion, sorry, I don’t have the capacity to make these changes for you.
Learning
You can master applied Machine Learning
without math or fancy degrees. REPLY
moe October 10, 2018 at 7:08 pm #
Find out how in this free and practical course.
Thank you so much, 1 – 5 helped me to built my own generic network. Now i have a MLP network
which i can easily adjust with a few parameter changes, wouldnt
Email have been so easy with your example.
Address
Well done!
REPLY
Anwar October 11, 2018 at 3:35 am #
I am running the same code that u have provided on python 3.6 and getting these errors please
help me:-
—————————————————————————————————————————
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 99/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee October 11, 2018 at 8:00 am #
Email Address
I have some suggestions here:
https://machinelearningmastery.com/faq/single-faq/why-does-the-code-in-the-tutorial-not-work-for-me
START MY EMAIL COURSE
REPLY
KS October 12, 2018 at 11:33 am #
How to:
“Regression. Change the network so that there is only one neuron in the output layer and that a real value is
predicted.”
REPLY
Jason Brownlee October 13, 2018 at 6:06 am #
Your Start in Machine Learning
Coding algorithms from scratch is not for beginners.
REPLY
Jerry October 16, 2018 at 9:08 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 100/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Ok, just following up. I still don’t get how the backprop is updating:
My question, are you sure this uses back-propagation? How are the weights saved and updated for each
epoch?
Email Address
REPLY
Jerry November 10, 2018 at 11:53 pm # START MY EMAIL COURSE
You learn something new everyday! I was always under the impression that values were
immutable when passing to a function in Python.
https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference
Thank you Jason! I’m really amazed on how active you are on this site. Just so you know, you and
your work are referenced often in my MSDS program.
One area that I think would also be beneficial is some work on hidden node activations and their
interpretations. The majority of our work is not so much on the output/accuracy of the NN, but more
of visualizing weights, activations, and determining the features that are causing nodes to activate.
REPLY
ben October 31, 2018 at 9:47 pm #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 101/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee November 1, 2018 at 6:11 am #
Ok the title sais ‘implement from scratch’ but I would say at some point getting
the point towards backprop is maybe more important than being able to say that
it was implemented from scratch (using nothing but plain python).
Greets
REPLY
Jason Brownlee October 20, 2018 at 5:57 am #
This tutorial is to teach how to develop a net (training and evaluation) without any dependencies other
than the standard lib.
REPLY
Vadim November 3, 2018 at 11:10 pm #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 102/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee November 4, 2018 at 6:27 am #
Thanks.
REPLY
Himanshu November 14, 2018 at 10:38 pm #
Hi Jason
how i can use forward and backward propagation in real time data. I mean data in which we have multiple
Your
field. Fields contains Numerical values , floating values , String Start
Values. in Machine ×
Learning
For an example if i want to use this technique for Titanic data how can i use it.
Email Address
REPLY
Jason Brownlee November 15, 2018 at 5:30 am # START MY EMAIL COURSE
You must use careful experimentation to get answers to each of these questions.
REPLY
Himanshu November 15, 2018 at 6:12 pm #
Hi Jason
I have few more questions on this.
First: why you assigned “activation = weights[-1]” , why not other weights or any random value.
Second: why you are looping only for two values
for i in range(len(weights) – 1) though we have three values.
Third: Why you have considered only two outputs here though we have four
output for layer one =.9643898158763548 Your Start in Machine Learning
output for layer two =.9185258960543243
output for layer three = .8094918973879515
output for layer two = .7734292563511262
why you considered only last two values why not first two or any other combination.
Four: here i guess some problem by mistake you have update wrong value
expected[row[-1]] = 1
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 103/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
after this line you have updated expected as [1,0] from [0,0]
and why we in this i have other question why we are updating this value.
REPLY
Jason Brownlee November 16, 2018 at 6:12 am #
Too many questions for one comment (I’m a simple human), one at a time and can you please
reference specific examples/lines of code otherwise I can’t help.
REPLY
Karim November 18, 2018 at 8:14 am #
Your
Hi Jason — How can i add more hidden layers? Thankx Start in Machine ×
Learning
You can master applied Machine Learning
REPLY
Jason Brownlee November 19, 2018 at 6:41 am #without math or fancy degrees.
Find out how in this free and practical course.
I recommend using Keras to develop your network:
https://machinelearningmastery.com/start-here/#deeplearning
Email Address
Hi Jason,
How to apply mathematical implementation of gradient descent and logistic regression,classification in real
time data.
For example if i want use this in survivors of Titanic data how to start with.
REPLY
Jason Brownlee November 23, 2018 at 7:44 am #
Here’s an example:
https://machinelearningmastery.com/implement-logistic-regression-stochastic-gradient-descent-scratch-
Your Start in Machine Learning
python/
REPLY
John Sald December 6, 2018 at 2:05 pm #
Hello, could you show me an example of using one of the extensions you mentioned, which can
give us a gain in performance?
Such as using matrix operations (in the weights) and vectors (inputs, intermediate signals and outputs)
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 104/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee December 7, 2018 at 5:16 am #
If you’re looking to go deeper into neural nets, I recommend using a library like Keras. You can start
here:
https://machinelearningmastery.com/start-here/#deeplearning
REPLY
Pipo December 17, 2018 at 7:25 am #
Email Address
REPLY
Brett August 24, 2019 at 4:07 am # START MY EMAIL COURSE
I’m confused about why you chose MSE for a classification problem. I was trying to use this
tutorial to discern the differences between a classification and function approximation
implementation, and the use of MSE for classification really threw me off. I know that it technically
works, but it’s probably good to mention that it’s not ideal. It would have been nice to get exposure
to taking the derivative of a different loss function, so that someone who is new to back-propagation
will start to grasp how different functions change the derivative, etc. Otherwise, the code is
understandable and could be modified slightly to make a good tutorial for function approximation.
REPLY
Brett August 24, 2019 at 4:19 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 105/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Agreed!
REPLY
Jason Brownlee August 24, 2019 at 7:58 am #
Yes, it was what we did in the 90s. Cross entropy would be preferred today, I agree.
I really need to do a series on coding neural nets from scratch to really dig into this. Thanks for
the kick!
REPLY
muhammad December 21, 2018 at 8:48 am #
REPLY
Sangeeth January 20, 2019 at 4:40 am #
Hi,
This website provides a good introduction for almost all topics in machine learning. Thanks for your work.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 106/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Could you please tell how you just multiplied 1 and 2 in backward_propagate_error (from the last layer) and
then used 3 in update_weights (from the first layer). Should we not do all steps in
backward_propagate_error and then use it to update_weights?.
REPLY
Jason Brownlee January 20, 2019 at 5:41 am #
No, we calculate the derivative of the error against the non linear activation function,
not the derivative of the loss function itself.
I think this is online learning using SGD. Would you have an implementation for offline
learning using mini Batch Gradient descent?
Correct, you can modify the above example to use batch or mini-batch gradient
descent.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 107/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Is the sum_error variable same as loss in model.fit output?..I get different loss
values when testing the same datasets on model.fit and your model. Could you tell
me why this is?
Email Address
REPLY
Gary January 22, 2019 at 8:54 am #
START MY EMAIL COURSE
Hi Jason.
In the “full” seeds example you call user defined function evaluate_algorithm(). However, the “heavy lifting”
inside it is performed by the function algorithm(). That function looks like it’s a part of some standard Python
library, but I can’t find it in any reference. Also you don’t comment at all at its use.
Thank you,
Gary
REPLY
Jason Brownlee January 22, 2019 at 11:43 am #
Your Start in Machine Learning
The “algorithm” is a reference to a function that is passed in as an argument.
REPLY
Gary January 22, 2019 at 1:24 pm #
Regards,
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 108/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Gary
REPLY
sangeeth January 28, 2019 at 8:39 am #
Hi,
For online machine learning, should we perform epochs?. Should not we update the model based only on
the present time input and then predict the next time step. If we do epochs that means the model is getting
updated for the whole data set up to the present time. Am I correct?. Thanks
Email Address
REPLY
kmillen February 12, 2019 at 10:49 am #
REPLY
Jason Brownlee February 12, 2019 at 1:59 pm #
Correct.
The mean of the scores is our estimate of the model’s performance when making predictions on unseen
data.
Thank you.
REPLY
MathewP February 20, 2019 at 3:25 am #
If we have, say 2 inputs and 1 neuron in hidden layer then only one weight is going to be updated, which is
clearly wrong. Correct me if I am wrong. The code works fine just taking row as inputs.
REPLY
Jason Brownlee February 20, 2019 at 8:11 am #
REPLY
Venkat February 22, 2019 at 4:53 am #
Hi Jason Brownlee, back propagation implementation really excellent ,because of without using any
Your
predefined library just use functions list, set, and dictionary. I needStart in Machine
a suggestion how to write a code for
×
Learning
implement activation function like a sigmoidal at hidden layer neurons and a tangent at output neurons.
could u help me.
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY
Jason Brownlee February 22, 2019 at 6:25 am #
Email Address
Yes, you can use the above as a starting point.
REPLY
Venkat February 22, 2019 at 4:49 pm #
Hi Jason Brownlee, yes , iam not asking how to write code for implementation of tanh, sigmoidial .
My request is how to modified code in forward_propagate function to implement suppose x is a activation at
hidden layer and y is another activation function at output layer.
in the above code u r calling transfer function for the hidden neurons and also output neurons . I request u to
give suggestion to call different activation functions for hidden and output neurons.
REPLY
Jason Brownlee February 23, 2019 at 6:26 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 110/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Danh Nguyen February 24, 2019 at 12:09 pm #
Example is great! The totally clean CSV wheat seed dataset is here:
https://raw.githubusercontent.com/NguyenDa18/MachineLearning_HW3/master/wheat-seeds.csv
Your
Anyway, posting this here so others won’t run into the same Start
problem in Machine
I did! Thanks ×
Learning
You can master applied Machine Learning
REPLY
Jason Brownlee February 25, 2019 at 6:37 am # without math or fancy degrees.
Find out how in this free and practical course.
Thanks for sharing.
Email Address
REPLY
vartika sharma February 27, 2019 at 3:49 am # START MY EMAIL COURSE
Hey Jason,
While I am ruuning the following code, I am getting this error
>>> scores=evaluate_algorithm(dataset,back_propagation,n_folds,l_rate,n_epoch,n_hidden)
Traceback (most recent call last):
File “”, line 1, in
File “”, line 13, in evaluate_algorithm
File “”, line 5, in back_propagation
File “”, line 6, in train_network
TypeError: list indices must be integers, not str
REPLY
Jason Brownlee February 27, 2019 at 7:34 am # Your Start in Machine Learning
Perhaps try saving code to a file and run from the command line, here’s how:
https://machinelearningmastery.com/faq/single-faq/how-do-i-run-a-script-from-the-command-line
REPLY
Andy March 5, 2019 at 12:51 pm #
Hello Jason,
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 111/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
I would like to ask, can you make the data split between training data and test data, instead of using k folds
variation, I would like to get some insight in this, thanks
REPLY
Jason Brownlee March 5, 2019 at 2:22 pm #
REPLY
Andy March 5, 2019 at 6:33 pm #
You can fit one final model on all data, then use it to make predictions (see the predict section).
START MY EMAIL COURSE
Remember, this is an example for learning only. If you want a model for your data in practice, I
recommend using a robust neural network library like Keras:
https://machinelearningmastery.com/start-here/#deeplearning
REPLY
Dini M March 11, 2019 at 12:21 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 112/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee March 11, 2019 at 6:52 am #
REPLY
Dini M March 11, 2019 at 12:28 am #
REPLY
Jason Brownlee March 15, 2019 at 2:25 pm #
REPLY
giuseppe March 28, 2019 at 8:11 pm #
Hi sorry for the late replay, actually the problem of save and loading the nn is not that
important so maybe i’ll try to solve it later. At the moment the problem is that i should reach the 99.
% on the “IrisDataTrain” set. What i’ve noticed is that the accurancy can change a lot repeating the
same training process with the same configuration. In order to get a better result I’ve tryied to repeat
Your Start in Machine Learning
the same traning process different times with the same configuration, I’ve choosen the configuration
that give me the best result in mean and variance. Now in order to improve the accurancy I’ve
modified the code so that I can connect easily the output of a nn with the input of another one so
that I can create a cascade of neural networks connected in different ways. At this point i’m stucked
at 96% in mean. To improve the accurancy I’ve implemented the relu activation function (but i’m not
sure it’s correctly implemented) and adam optimizer (but it doesn’t work at all).
I’ll link the code on pastbin (I don’t know if there is any better way to do that) in particular what I’ve
done is just insert everything in a class and modified:
1. the initialization function so that I can chose the number of neurons in each layer
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 113/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
2. the back_propagation_error function trying to add the relu and adam optimizer
3. the update weights function trying to implement adam optimizer (it doesnt’ work at all)
In the code I’m going to share I’ve just removed many parts just for a readability reason, after
cleaning it I will send it to you if you want. Sorry for the long message and thanks for you help
https://pastebin.com/RxxuVaCD
REPLY
Jason Brownlee March 29, 2019 at 8:31 am #
Nice work!
It might be time to graduate to Keras where everything is implemented for you and you can just
use it directly and focus on tuning the model.
Your Start in Machine ×
Learning
MLnovice March 21, 2019 at 10:53 pm # You can master applied Machine Learning REPLY
REPLY
Jason Brownlee March 22, 2019 at 8:28 am #
Matty March 25, 2019 at 9:55 am # Your Start in Machine Learning REPLY
Reading this post, it seems to me that I can split the process of back propagation in large networks into
multiple steps. Am I right?
I have a large network that my current GPU runs out of memory when I try to train it. I was wondering if I can
split my network into two sub-networks, and first calculate the updates for the deeper part(that has the
ground truth outputs) and obtain the error that should be passed to the other sub-network. Then, use the
provided error to calculate the updates for the second sub-network as well. Do you think it’s possible? Do
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 114/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
you have any suggestion (or source that can be helpful) for implementing this back propagation with existing
tensorflow or pytorch builtin functions?
Thanks.
REPLY
Jason Brownlee March 25, 2019 at 2:17 pm #
It might be cheaper (in time/money) to rent an AWS EC2 instance with more GPU RAM for a few hours?
I did a small sanity check with a two-layer network, and it seems both the two-step optimization and
the one-step optimization with all the trainable parameters results in the same updates.
REPLY
Jason Brownlee March 26, 2019 at 2:20 pm #
Novia Puspitasari March 28, 2019 at 12:17 am # Your Start in Machine Learning REPLY
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 115/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Kevin March 29, 2019 at 1:21 am #
Your Start
Hi there Jason, I would like to ask if I wanted to generate in Machine
random weights and bias with range -1 to ×
1, how to do it ? Since I already import random or from uniform import random, and it said AttributeError:
Learning
‘builtin_function_or_method’. Thank you very much !
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY
Jason Brownlee March 29, 2019 at 8:38 am #
Email Address
Good question, this post shows how:
https://machinelearningmastery.com/how-to-generate-random-numbers-in-python/
START MY EMAIL COURSE
You then scale them to any range you want:
REPLY
Kevin March 29, 2019 at 4:20 pm #
Thanks for your reply ! Awesome guide, I really grateful for it. Once again, thanks a lot
Jason.
REPLY
Jason Brownlee March 30, 2019 at 6:22 am #
Your Start in Machine Learning
You’re welcome.
REPLY
wancong zhang March 30, 2019 at 12:45 pm #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 116/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
If I change your “initialize network” method to initialize multiple hidden layers with arbitrary width, will your
program still work? In other words does your algorithm generalize to deeper networks?
Thanks.
REPLY
Jason Brownlee March 31, 2019 at 9:26 am #
REPLY
manoj April 19, 2019 at 7:38 am #
Your Start in Machine ×
Hi Jason!
Learning
Its really a helpful post, that you very much.
You can master applied Machine Learning
I wanted to see the plots of training error and testing error. (like how they finally converged by epochs by
without math or fancy degrees.
epochs). What would be the easiest way to plot those training and testing graphs
Find out how in this free and practical course.
regards
Manoj Goli Email Address
I’d recommend using a library like Keras which provides the history directly and ready to plot:
https://machinelearningmastery.com/display-deep-learning-model-training-history-in-keras/
REPLY
Danial April 20, 2019 at 5:46 pm #
Hi jason.
My question is how I can see my CNN code is using BP framework?
REPLY
Danial April 21, 2019 at 1:27 pm #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 117/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Yes. How I can see model weights? How cnn use BP framework if it is not shown in code?
for i in range(len(test)):
# Forecast the data
test_X, test_y = test[i, 0:-1], test[i, -1]
X_ = test_X.reshape(1, 28, 28, 1)
predict = model.predict(X_, batch_size=1)
predict = predict[0, 0]
# Inverse transform
You can master applied Machine Learning
predict = inverse_transform(scaler, test_X, predict)
without math or fancy degrees.
# Inverse the features Find out how in this free and practical course.
predict = inverse_features(data_set, predict, len(test)+1-i) – maxVal
if predict < 0:
Email Address
predict = 0
# Round the value
predict = np.round(predict, 2) START MY EMAIL COURSE
# store forecast
expected = data_set[len(train) + i + 1]
predict_data.append(predict )
real_data.append(expected )
if expected != 0:
prediction.append(predict)
real.append(expected)
REPLY
Jason Brownlee April 22, 2019 at 6:15 am #
You can get the model weights from a Keras model by calling the get_weights()
function on a give layer. Your Start in Machine Learning
Is it right that above code is using BP framework.? It’s part of CNN code that I sent
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 118/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
How keras uses BP framework.? If you have link kindly share it.
Learning
Hello Jason , You’re the best teacher.
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY
Jason Brownlee April 25, 2019 at 8:08 am #
Email Address
Thanks.
REPLY
Abarni April 30, 2019 at 12:39 pm #
Nice Post !
Here is another very nice tutorial with step by step Mathematical explanation and full coding.
http://www.adeveloperdiary.com/data-science/machine-learning/understand-and-implement-the-
backpropagation-algorithm-from-scratch-in-python/
REPLY
Jason Brownlee April 30, 2019 at 2:27 pm #
REPLY
Zahra May 6, 2019 at 4:28 pm #
And why this happened? What’s wrong? What should I do? What should I change?
Please, help me!
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 119/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
For example (in this codes), how to use my dataset (use excel file)MY
START in this codes..
EMAIL COURSEHow to import my dataset
in this codes? Can you teach me more detail, please..
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 120/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee May 9, 2019 at 6:46 am #
Perhaps you should start with Keras, it is much easier for beginners:
https://machinelearningmastery.com/tutorial-first-neural-network-python-keras/
REPLY
Aziz Ahmad July 22, 2019 at 4:42 am #
Your
Jason Brownlee July 22, 2019 at 8:28 am #
Start in Machine ×
REPLY
Learning
Thanks.
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY
Nirmala May 10, 2019 at 5:37 am #
Email Address
I got an error called->
REPLY
Jason Brownlee May 10, 2019 at 8:20 am #
REPLY
Ido Berenbaum May 11, 2019 at 9:39 pm #
Hi Jason,
Your Start in Machine Learning
thanks for the great tutorial, I learned a lot from it.
There is one thing I didn’t really understand though,
when you update the weights you add to the weight the calculated change that is needed.
but’from what I read in other sites like wikipedia, the change to the weight needs to be multiplied by -1 and
then added to ensure it
changes the weight in the opposite direction of the gradient and so getting it closer to the local minimum.
like muhammad said in December 21, 2018:
“hi, thanks for this code.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 121/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
I’m trying to understand why are u adding on the update weights, shouldnt be
wi←wi−η∂E/∂wi like this?”
and I tried to change line 141 to: neuron[‘weights’][j] -= l_rate * neuron[‘delta’] * inputs[j]
basically doing -= and not += but it just made the sum error of the network to increase after each epoch.
so, I will be grateful if you could explain to me why are you adding and not subtracting.
thanks
REPLY
Jason Brownlee May 12, 2019 at 6:43 am #
REPLY
Jason Brownlee July 16, 2019 at 2:19 pm #
Nice explanation.
REPLY
Nirmala May 16, 2019 at 4:23 pm #
In training code and testing code I want to link the onther dataset .txt file but it will not work.please
can u send a code for that..
REPLY
Jason Brownlee May 17, 2019 at 5:49 am #
Sorry, I don’t have the capacity to develop custom code for you.
REPLY
Arthur May 22, 2019 at 4:03 pm #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 122/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Hi Jason, first of all thank you very much for this post, I’m learning ML at the moment, and writing a
neural network with backpropagation in C# to help the process.
When using the wheat seeds dataset, and the same network layout as you suggest, I get very similar results
to yours in terms of accuracy.
When I scale the network however to have 2 (or more) hidden layers of 5 neurons, I sometimes get
exploding gradients (and NaN output results because of it). Is this something you’d expect given the
dataset? ie, doesn’t this dataset allow for much more than 1 hidden layer for some reason. (Some context: I
normalize the data just like you do, and I use the same dataset as you do, the reason I test with more than 1
layer, is just out of curiosity, and to see whether the accuracy improved – it doesnt)
I’m trying to understand why it happens with this particular data, or whether my implementation fails
somehow. Note that I do get good results most of the time, but with a certain weight initialization the
exploding gradients can happen.
Your Start in Machine ×
Learning
REPLY
Jason Brownlee May 23, 2019 at 5:54 am # You can master applied Machine Learning
without math or fancy degrees.
Perhaps try scaling the data prior to modeling, see
Find this
out howpost:
in this free and practical course.
https://machinelearningmastery.com/how-to-improve-neural-network-stability-and-modeling-
performance-with-data-scaling/
Email Address
Hello, I have problem. Why output must be integers, not float (decimal)? Specially in Train.. How to
change output data type to float?
REPLY
Jason Brownlee May 28, 2019 at 8:08 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 123/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Zahra May 28, 2019 at 1:09 pm #
I want to ask, how to display execution time (running rime) in Train code?
REPLY
Jason Brownlee May 28, 2019 at 2:44 pm #
Sorry, I don’t have an example of calculating clock time for code examples.
”’ Learning
Calculate the output of a recurrent neural network with tanh activation
You can master applied Machine Learning
and a linear layer on top
without math or fancy degrees.
Params: Find out how in this free and practical course.
x: input matrix [n_timesteps * n_samples * 2]
w: non-recurrent weights
Email Address
r: recurrent weights
b: biases
wo: output-layer weights START MY EMAIL COURSE
bo: output-layer biases
Returns:
h: matrix of activations (n_timesteps, n_samples, n_hiddens)
o: final predictions
”’
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 124/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee June 3, 2019 at 2:34 pm #
Sorry, I don’t have the capacity to debug your code, perhaps try posting to stackoverflow?
REPLY
Jason Brownlee June 11, 2019 at 7:54 am #
REPLY
Zahra Nabila Izdihar June 13, 2019 at 2:36 am #
But, I don’t understand How to determine the weights in forward propagation? What is the formula?
Thank you
Your Start in Machine Learning
REPLY
Jason Brownlee June 13, 2019 at 6:21 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 125/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Did you mean that “output” (in forward propagatation) is predicted result?
Email Address
REPLY
Leo July 2, 2019 at 11:25 pm #
START MY EMAIL COURSE
It is crazy that nobody complain the readability of your codes. Thanks anyway
REPLY
Jason Brownlee July 3, 2019 at 8:34 am #
Sorry that you think that the code is not readable. I thought it was very readable.
REPLY
Femi July 14, 2019 at 12:17 am #
Your Start in Machine Learning
Good day Dr. I am new to machine learning but have interest in it. My question is this, can i use the
Python 2.7 in miniconda to implement your samples?
REPLY
Jason Brownlee July 14, 2019 at 8:15 am #
Yes.
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 126/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Ravi July 19, 2019 at 9:52 pm #
Hi Dr. Jason
I have developed and trained a neural network (3 layers: 1 input, 1 hidden and 1 output) for following
situation
(The code was written step by step, as i do not want to use a tool without understanding the computations)
Using the delta rule with backpropagation algorithm, i wasYour Start error
able to achieve in Machine
= 9.39E-06 for 1000 ×
iterations
Learning
My final “input to hidden layer” weight matrix size is 200 x 5 (as i have 40 samples x 5 input neurons and 5
You can master applied Machine Learning
hidden neurons)
without math or fancy degrees.
“hidden to output layer” weight matrix size is 200 x 1 (as i Find
haveout
40how
samples
in thisxfree
5 hidden neurons
and practical and 1 output
course.
neuron)
Email
Now my question is for a given test sample having 5 elements Address
(input is 1 sample 5 elements),
I have 200 x 5 and 200 x 1 weight matrices; but i require only 5 x 5 and 5 x 1 weight matrices for testing.
Thanks in advance
Ravi
REPLY
Chrissie Li July 25, 2019 at 9:06 pm #
hi, how can i download the code? copy the tip’s script?
Your Start in Machine Learning
REPLY
Jason Brownlee July 26, 2019 at 8:22 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 127/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Prof Sir, pls i have been finding it difficult to implement your samples when i can not prepare
environment that accept all the command. Pls help
REPLY
Jason Brownlee July 26, 2019 at 8:23 am #
REPLY
Jason Brownlee July 31, 2019 at 6:53 am # Email Address
REPLY
Majed August 4, 2019 at 8:10 am #
I wrote a neural network that consists of three layers as follows:[ 4 input neurones – 5 hidden
neurones – 3 output neurones]. first, I standerdized the data using the z-score. The accuracy of my model
exceeded 67. Note: I didn’t use the regularisation terms yet.
here is my implementation of both feedforward and back prop ..
Thanks …
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 128/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Jason Brownlee August 5, 2019 at 6:43 am #
Well done!
REPLY
Ekundayo August 6, 2019 at 9:40 pm #
Your
Ca I still get an help this one time? Pls sir, my project is toStart
use 14 in Machine
features extractions for plant
×
leave classification. I need to recognize one leaf out of 36Learning
leaves all with 14 features. Sir can I use your code
to achieve this?
Thanks You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY
Jason Brownlee August 7, 2019 at 7:52 am # Email Address
REPLY
Mohammed August 14, 2019 at 12:20 pm #
Hi Dr. Jason
I have one question about backpropagation in unsupervised model, e.g. extract features.
Is it possible to apply this code for it, and only replaces loss function of unsupervised model by the loss
function of supervised?
Regards
Your Start in Machine Learning
REPLY
Jason Brownlee August 14, 2019 at 2:10 pm #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 129/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Mohammed August 15, 2019 at 5:08 pm #
Oh! many thanks,So, can help me what is the way for learning parameters in unsupervised
approach.
if i need to extract the features from data as low dimension nested of data with large dimension.
REPLY
Jason Brownlee August 16, 2019 at 7:47 am #
There are specialized techniques for unsupervised learning neural nets, perhaps start
with the SOM:
http://cleveralgorithms.com/nature-inspired/neural/som.html
Your Start in Machine ×
Learning
You can master applied Machine Learning REPLY
Mohammed August 15, 2019 at 5:10 pm #
without math or fancy degrees.
FindFiltering!
such as Unsupervised feature learning with Sparse out how in this free and practical course.
Email Address
REPLY
Jason Brownlee August 16, 2019 at 7:48 am #
START MY EMAIL COURSE
Sorry, I don’t have a tutorial on that topic, perhaps in the future.
REPLY
Mohammed August 16, 2019 at 11:20 am #
REPLY
Jason Brownlee August 16, 2019 at 2:10 pm #
You’re welcome.
Your Start in Machine Learning
REPLY
Cherinet Mores August 20, 2019 at 4:44 pm #
Jason Brownlee
Thank you for your continues help
Here I have one questions,
In case, if i want to solve the regression problem (Meaning, if I have 3 real value outputs from the input
parameters) which part of the code should be modified and How?
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 130/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
REPLY
Steven Pauly August 20, 2019 at 9:59 pm #
Hi Cherinet, I’ve changed the n_outputs to 1 and the function train_network, I’ve changed the
below. I’ve increased the n_epoch to a lot higher, because else it will give you the average. Be sure to
normalize your input & output values, though.
REPLY
Cherinet Mores August 21, 2019 at 8:06 am #
Dear Steven Pauly thank you very much for your help.
REPLY
Jason Brownlee August 21, 2019 at 6:36 am #
Change the output to be a linear activation and the loss function to mse.
REPLY
Steven Pauly August 20, 2019 at 9:56 pm # Your Start in Machine Learning
REPLY
Jason Brownlee August 21, 2019 at 6:41 am #
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 131/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
Leave a Reply
Name (required)
SUBMIT COMMENT
Email Address
Read More
More Tutorials?
Start Here
(over 800 tutorials!)
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 132/133
8/27/2019 How to Code a Neural Network with Backpropagation In Python
https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 133/133