File Exchange

image thumbnail

MLP Neural Network trained by backpropagation

version (2.15 KB) by Mo Chen
Multilayer Perceptron (MLP) Neural Network (NN) for regression problem trained by backpropagation (backprop)


Updated 21 Nov 2018

View Version History

View License

Very compact implementation of backpropagation for MLP regression. Mean to be read and learn.

This package is a part of the PRML toolbox (

Comments and Ratings (21)

Dan Walker

I am having the hardest time understanding your code, it seams easy enough but on feed forward why do you require Z{T+1} (outside the loop) without the activation function? When you backpropagate you need the derivative of tanh (1/cosh^2x) but I don't see it in your code. is that because this df is specific to your problem? is that also why you are squaring your values there? I think this code could benefit from more comments

% feedforward
for t = 1:T-1
Z{t+1} = tanh(W{t}'*Z{t}+b{t});
Z{T+1} = W{T}'*Z{T}+b{T};

% backward
R{T} = E; % delta
for t = T-1:-1:1
df = 1-Z{t+1}.^2; % h'(a)
R{t} = df.*(W{t+1}*R{t+1}); % delta

% gradient descent
for t=1:T
dW = Z{t}*R{t}'+lambda*W{t};
db = sum(R{t},2);
W{t} = W{t}-eta*dW;
b{t} = b{t}-eta*db;

yulong zhu

it train very well。but how to predict?


mr morgan

Hussein Sharadga

Hi all,
I used this code to train a sample of 8 inputs and one output and it worked. However, I am wondering about how to use it to predict using testing data.
anybody can help, please!

Niclas Klarström

Zulkifli Hidayat

Nice, but not compatible with R2013b, it's not true compatible with any release

Zulkifli Hidayat

I ran the demo, but I got
>> mlp_demo
Error using +
Matrix dimensions must agree.

Error in mlpReg (line 33)
Z{t+1} = tanh(W{t}'*Z{t}+b{t});

Error in mlp_demo (line 8)
[model, L] = mlpReg(x,y,k);

Omer Yasin Birey

Excellent and simple

Mo Chen

@dsmalenb, [4,5] means, two hidden layer, one with 4 nodes, and one with 5 nodes


Dumb question - does "h = [4,5]" mean "4 neurons in 5 layers" or "4 layers with 5 neurons". I can't deduce this by reading the code. It is nice and compact but that point is not clear to me.

zjyedword zjyeword

hi, every body, why not do the following? please explain it, thanks
E = W{l}*dG;

Rachit Saluja


hana razak

nima safari

Ignas A.

Fayzan Zafar


Amazing - works really well and is super compact in terms of code. Great work!


Gyesoon Park

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

Inspired by: Pattern Recognition and Machine Learning Toolbox

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!