MATLAB Answers

Please i need help on how to determine R squared for training, validation, testing and ALL in neural network with a simple code.

101 views (last 30 days)
Please how can I determine the correlation coefficient of training, validation, testing and ALL in neural net with a simple code.Each time I click on plotregression, I see thier R2 but would like to include it in my code and view only the values.
load ('t.mat');
load ('x.mat');
x = input';
t = Target';
% Choose a Training Function
% For a list of all training functions type: help nntrain
% 'trainlm' is usually fastest.
% 'trainbr' takes longer but may be better for challenging problems.
% 'trainscg' uses less memory. Suitable in low memory situations.
% Levenberg-Marquardt backpropagation.
trainFcn = 'trainlm';
%% Create a Fitting Network
hiddenLayerSize = 10;
net = fitnet(hiddenLayerSize,trainFcn);
%% Setup Division of Data for Training, Validation, Testing
[trainInd,valInd,testInd] = divideint(x,0.70,0.15,0.15);
%% Train the Network
net.performParam.regularization = 0.0001;
[net,tr] = train(net,x,t);
%% Number of validation checks
net.trainParam.min_grad
nntraintool
%%
% Test the Network
y = net(x);
e = gsubtract(t,y);
performance = perform(net,t,y);
perf = mse(net, t, y, 'regularization', 0.01);
perf = crossentropy(net,t,y,{1},'regularization',0.01);
%% Calculate network perfomance on test
tInd = tr.testInd
tsty = net(x(:,tInd))
tstPerform = perform(net,t(:,tInd),tsty)
%% Check train records
tr
%% View the Network diagram
view(net)
%% Plots
% Uncomment these lines to enable various plots.
trOut = y(tr.trainInd)
vOut = y(tr.valInd)
tsOut = y(tr.testInd)
trTarg = t(tr.trainInd)
vTarg = t(tr.valInd)
tsTarg = t(tr.testInd)
%% performance figure,
figure
plotperform(tr);
%% trainstate figure,
figure
plottrainstate(tr);
%% Errorhistogram figure,
figure
ploterrhist(e);
%% Regression figure,
figure
plotregression(trTarg, trOut, 'Train', vTarg, vOut, 'Validation', tsTarg, tsOut, 'Test', t,y, 'All');

  2 Comments

Behzad Navidi
Behzad Navidi on 18 Feb 2020
net.performFcn = 'mse'; % Mean Squared Error
%% Calculate network perfomance on test
tInd = tr.testInd
tsty = net(x(:,tInd))
tstPerform = perform(net,t(:,tInd),tsty)
RMSE_test=sqrt(tstPerform);
fprintf('RMSE on the test set is %f\n',RMSE_test)
% Do same for tarin and validation

Sign in to comment.

Accepted Answer

fred  ssemwogerere
fred ssemwogerere on 18 Feb 2020
"..........Each time I click on plotregression, I see thier R2......"
Hello, point of correction; regression plots of your modeled network show the correlation coefficient,"R" (a measure of the magnitude and direction of linear association between the defined variables), but not the coefficient of determination, "Rsquared / R2" (a measure of the magnitude of variability modeled by the network in your output,'y'). However, R=squareroot(R2). Added to that, a number of answers have been previously given on how to obtain this correlation coefficient you desire. Please refer to the links below:
Alternatively you could refer to the matlab documentation in the link below, with an example of obtaining the correlation coefficient from the general or "All" plot:

More Answers (0)

Community Treasure Hunt

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

Start Hunting!