How to create a fitnet neural network with multiple hidden layers?

R
ritvzz_01 · May 17, 2021 · 2.4K views
Question
I am new to using the machine learning toolboxes of MATLAB (but loving it so far!) From a large data set I want to fit a neural network, to approximate the underlying unknown function. I have used the "Neural Net Fitting" app and generated a script with it which builds and trains my network. It all works, however the results are not good enough. I think the network is not complex enough to cover the non-linearities. So, I figued I'd add another hidden layer, but I can't get it to work. The current code to produce the network is the following (which is the default):     trainFcn = 'trainlm'; % Levenberg-Marquardt backpropagation. % Create a Fitting Network hiddenLayerSize = 10; net = fitnet(hiddenLayerSize, trainFcn); How would I modify this to add more hidden layers?   I am looking to get the classical Multi-Layer Perceptron (MLP) network, with potentially even more hidden layers:
Expert Answer
Profile picture of Prashant Kumar
Prashant Kumar PhD Expert
Answered Aug 25, 2026
You can add more hidden layers as shown below:
 
 
trainFcn = 'trainlm';  % Levenberg-Marquardt backpropagation.
% Create a Fitting Network
hiddenLayer1Size = 10;
hiddenLayer2Size = 10;
net = fitnet([hiddenLayer1Size hiddenLayer2Size],  trainFcn);
This creates network of 2 hidden layers of size 10 each.
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!