Difference between fitrnet and fitnet
59 views (last 30 days)
Show older comments
Alex Liu
on 19 Dec 2021
Commented: David Willingham
on 27 Oct 2022
When I first learn to use neural networks, I usually used nnstart toolbox, which uses fitnet. However, when I started actually to build my own neural networks with more layers and optimal hyperparameters, I found that R2021a introduced fitrnet that looks more powerful. I was wondering what is the difference between these two neural network functions.
0 Comments
Accepted Answer
David Willingham
on 20 Dec 2021
Edited: David Willingham
on 18 May 2022
Hi Alex,
In R2021a, SMLT introduced the fitting functions fitcnet and fitrnet, which train shallow classification and regression neural network models. If you'd like to customize the network, there is a newer framework for building shallow and deep neural networks. Here are 2 examples of how to do this:
David
3 Comments
Alessandro Niccolai
on 26 Oct 2022
Dear David,
While working on Neural Network, I've compared the results of the two functions "fitrnet" and "fitnet". The results I've obtained shows that the "fitnet" function works much better both in terms of computational time and of performance.
Even usign the hyperparameter optimization of "fitrnet", the results are much less than the "fitnet". Here you can find the results I've obtained:
Neural Network - fitrnet
Elapsed time is 105.777746 seconds.
R2 = 0.99957
MSE = 0.028211%
Neural Network - fitnet
Elapsed time is 23.113578 seconds.
R2 = 0.99997
MSE = 0.00082949%
In both the cases, I've computed the performance on a different dataset.
Have you experienced something similar?
Attached you can find the input I've used and below the code that performs the comparison:
clc
clear
close all
load('trainingTest');
%% Train fitrnet
disp('Neural Network - fitrnet')
disp('-- Traninig --')
tic
Mdl = fitrnet(...
x, t, ...
'LayerSizes', [ 86 23 294], ...
'Activations', 'tanh', ...
'Lambda', 0.022295, ...
'IterationLimit', 2000, ...
'Standardize', true);
toc
disp('-- Running --')
yTest = predict(Mdl,xTest);
% R2
R2 = regress(yTest,tTest);
disp([' R2 = ',num2str(R2)])
% MSE
MSE = mean((tTest-yTest).^2)/mean(yTest)^2*100;
disp([' MSE = ',num2str(MSE),'%'])
%% Train fitnet
disp('Neural Network - fitnet')
hiddenLayers = [10 10];
nLayers = length(hiddenLayers)+1;
net = fitnet(hiddenLayers);
net.trainFcn = 'trainbr';
net.trainParam.epochs = 1000;
net.trainParam.showWindow = false;
net.trainParam.showCommandLine = false;
net.trainParam.show = 10;
net.trainParam.max_fail = 3;
net.trainParam.goal = 1e-9;
net.divideFcn = 'dividerand';
net.divideParam.testRatio = 0.15;
net.divideParam.valRatio = 0.15;
tic
[net,tr] = train(net,x',t');
toc
yTest = net(xTest')';
% R2
R2 = regress(yTest,tTest);
disp([' R2 = ',num2str(R2)])
% MSE
MSE = mean((tTest-yTest).^2)/mean(yTest)^2*100;
disp([' MSE = ',num2str(MSE),'%'])
David Willingham
on 27 Oct 2022
Hi Allesandro,
The two functions have different solver implementations under the hood. Also they have different number of layers and layers sizes.
My recommendation is that you can always try both and see which method works best for the given application.
More Answers (0)
See Also
Categories
Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!