How many times should the Neural Network trained ?
6 次查看(过去 30 天)
显示 更早的评论
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');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/705102/image.png)
In the picture above, R_correlation for training testing i calculated from code, gives certrain value, and R from graphs gives other values, why?
0 个评论
回答(1 个)
Walter Roberson
2021-8-7
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Sequence and Numeric Feature Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!