File Exchange

image thumbnail

Backpropagation for training an MLP

version 1.6.0 (2 MB) by BERGHOUT Tarek
this code returns a fully trained MLP for regression using back propagation of the gradient. I dedicate this work to my son :"Lokmane ".


Updated 31 Aug 2019

View License

I dedicate this work to my son :"Lokmane BERGHOUT"
BP algorithm is one of the most famous algorithms for training a feed forward neural net , it allows to update weights by moving forward and backword until the error function stuck at its local minimum.

in this code we explain step by step in comments how we can train a neural net with BP algorithm with additional illustrative features.

please leave your comments and tell us about your thoughts and share your experiences.
my email : berghouttarek@gmail;com

Cite As

BERGHOUT Tarek (2019). Backpropagation for training an MLP (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (20)

thank you Toshi Sinha.

Toshi Sinha

Wonderful work Sir.

guohan yin

if we only add W = W + Learning_Rate*deltaW;
this may cause the error increase, and only the error < E , the upadte is still continues as same direction. The result is output value will stay at 1 , since the activate function is sigmoid and it runs into a dead loop of error< E.
I added part to judge error changing direction to modify this situation.
if u like to discuss, we can work together on this.
Thank you again !

Thank you Berghout for sharing code. But It does not work for xor operation prediction.
i.e. for x = [0 1; 1 0; 1 1; 0 0;]; y=[1;1;0;0;];
It keeps on iteratng for smaller desired_error
and if I increase the desired_error it outputs wrong answers at the end.
Someone please help. I am quite new to machine learning and this is the first algorithm I am trying to implement.

yes, correct, thanks , i will update the code.

guohan yin

Thank you BERGHOUT for sharing this code. This is a very straight forward sample code for BP menthod.
I have one question, there is no use of Learning_Rate as a parameter in the weight update.
if i==1
W=W+(input2'*H); % update weights step1
Z=hidden(i-1).F; % load the appropreate hidden layer
W=W+(Z'*H); % update weights step2
so this is the fixed learning rate update form. Is that correct?



Toolbox cannot be installed. Installation path is not accessible.

the above error is throughing, can someone please kindly help me in this regards

the code is updated , and prediction function is added.

This is really a nice work and helpful. Thank you BERGHOUT for sharing this code.

contact me :

second : after trainig add this:
for i=1:Number_W
W=weights(i).F;% load weights
Hidden_layer=logsig(xts*W);% calculate the hidden layer
xts=Hidden_layer;% set the hidden as the input of next hidden layer

I am sorry BERGHOUT Tarek, it is already mentioned in the code, so where and how to give the new input value after training the data, I want to predict output for any new input value which is not included in the data. I didn't understand that how are you predicting outputs for new set of input. there isn't a function which is taking any input to predict the output.

for i=1:Number_W
W=weights(i).F;% load weights
Hidden_layer=logsig(yourtraininset*W);% calculate the hidden layer
yourtraininset=Hidden_layer;% set the hidden as the input of next hidden layer

how to use the updated weights? can you please help that how can i predict the unknown for a new sample ?

after traing the Algorithm will gives the final updated weights ; use them to test or predict unknown new samples

Sorry if its a basic question. How can I use this to get predictions ?

Moe Moe

This is really nice work, thank you !!

I am wondering, how can I make the code accept more than 1 input to give one out, please? (Multi Input Single Output)

Amin Azari

Can it be used for one-step training or it will overfil? I want to implement reinforcement learning, and hence, need to update the weights just based on the latest observation.



Notations are updated according to attached pdf document


a prediction function is added to meet some student requests


youtube link


very simple code with many comments to make easy to be understood


new very simple code with comments to be very easy to understand.


new version

MATLAB Release Compatibility
Created with R2013b
Compatible with any release
Platform Compatibility
Windows macOS Linux