Effacer les filtres
Effacer les filtres

How many times should the Neural Network trained ?

7 vues (au cours des 30 derniers jours)
Shubham Lakhera
Shubham Lakhera le 7 Août 2021
I am trying to build an artificial neural network to predict water quality index.
Water Quality Index is a numerical value which I am trying to predict based on 10 variables, (Input parameters).
I have created the Neural Network, it works fine, giving me results. But the performance of the network is very poor. I want to increase R-square, & lower the RMSE.
I am using Matlab 'trainlm' algorithm for training.
Every time, I hit 'train', it runs and changes the weights and biases of the network connections giving me different R-square and RMSE value.
Sometime high, Sometime Low. If this keep happenning how am I supposed to find the highest R-squre or the train the model best so that the prediction it is making are accurate.
Also, the tool gives us Regression Plots(R-Values), which tell us about the relationship between variables in training, testing, validation phase. But the R-Values I calculated by using formula they are different from the R-values given in the graphs.
What am I doing wrong ?
NewInputsNormalized2 = NewInputsNormalized(:,[1:8]);
xt = NewInputsNormalized2';
yt = NewTargetsNormalized';
%% Train Neural Network
HiddenLayer1Neuron = 16;
% HiddenLayer2Neuron = 4;
% HiddenLayer3Neuron = 2;
net = feedforwardnet([HiddenLayer1Neuron], 'trainlm');
net.performFcn = 'mse';
net.divideFcn = 'divideind';
net.divideParam.trainInd = 1:960 ;
net.divideParam.valInd = 961:1080 ;
net.divideParam.testInd = 1080:1200 ;
net.trainParam.epochs = 2000 ;
net.trainParam.goal = 0 ;
net.trainParam.max_fail = 6;
[net, tr] = train(net, xt, yt);
[net] = configure(net,xt,yt);
nntraintool
Outputs = net(xt);
errors = gsubtract(yt, Outputs);
net_performance = perform(net, yt, Outputs);
w1 = net.IW{1}; %the input-to-hidden layer weights
w2 = net.LW{2}; %the hidden-to-output layer weights
b1 = net.b{1}; %the input-to-hidden layer bias
b2 = net.b{2}; %the hidden-to-output layer bias
%
% Results
% RMSE R-Square R in Training
yTrain = (net(xt(:,tr.trainInd)));
yTrainTrue = (yt(tr.trainInd));
RMSE_Training = sqrt(mean((yTrain - yTrainTrue).^2));
R_Linear_Regression_Validation = fitlm(yTrain, yTrainTrue);
R_Squared_Training = R_Linear_Regression_Validation.Rsquared.Ordinary;
R_Correlation_Training = corr(yTrain', yTrainTrue', 'Type', 'Pearson');
%
% RMSE R-Square R in Validation
yVal = (net(xt(:,tr.valInd)));
yValTrue = (yt(tr.valInd));
RMSE_Validation = sqrt(mean((yVal - yValTrue).^2))
R_Linear_Regression_Validation = fitlm(yVal, yValTrue);
R_Squared_Validation = R_Linear_Regression_Validation.Rsquared.Ordinary
R_Correlation_Validation = corr(yVal', yValTrue', 'Type', 'Pearson');
%
% RRMSE R-Square R in Testing
yTest = (net(xt(:,tr.testInd)));
yTestTrue = (yt(tr.testInd));
RMSE_Testing = sqrt(mean((yTest - yTestTrue).^2))
R_Linear_Regression_Testing = fitlm(yTestTrue, yTest);
R_Squared_Testing = R_Linear_Regression_Testing.Rsquared.Ordinary
R_Correlation_Testing = corr(yTest', yTestTrue', 'Type', 'Pearson');
In the picture above, R_correlation for training testing i calculated from code, gives certrain value, and R from graphs gives other values, why?

Réponses (1)

Walter Roberson
Walter Roberson le 7 Août 2021
If this keep happenning
It will keep happening. It is deliberate that the weights are normally initiallzed randomly.
how am I supposed to find the highest R-squre or the train the model best so that the prediction it is making are accurate.
Training neural networks is seldom an exact science. There are some circumstances, some networks representing some functions, under which the weights will converge... but most of the time they will not. The situation is roughly like saying that you can find an algorithm that is always able to find the minima or maxima of a quadratic equation (to within floating point round-off)... but that that algorithm is not certain to work on even a cubic equation.
It is inherent to how neural networks are typically used, that you try a whole bunch of different initial conditions, and you have some strategy for when to give up, and at the end you take the best you found. Sometimes the strategy for when to give up is to try a fixed number of iterations, or a number of iterations proportional to the size of the problem; sometimes it has to do with having not seen an improvement for some number of iterations; sometimes it has to do with whether the accuracy has reached a particular goal. Or a number of decisions together.

Catégories

En savoir plus sur Sequence and Numeric Feature Data Workflows dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by