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

Illustration
ritvzz_01 - 2021-05-17T15:34:28+00:00
Question: How to create a fitnet neural network with multiple hidden layers?

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 answered . 2025-11-20

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.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!