Why the seed number of rng cause different results of accuracy in neural network?
Show older comments
Dear respected MATLAB users,
I am currently developing a regression model using neural network. To do that, I'm using a loop to select the best network with the lowest value of mse. While writing the code, I notice that the number of seed for rng plays an important role in determining the R2 value of trained and test data. As such when i put rng(0), the R2 value is lower compared to when i use rng(19). I already read the definition for rng but I still could not understand why the difference exist? Hope you can help me. Below is the part of coding I use in MATLAB.
x=input(1:35,:);
t=output(1:35,:);
[I N]=size(x) % I=4, N=35
[O N]=size(t) %O=1, N=35
trainFcn ='trainlm';
hiddenLayerSize=3;
net=fitnet(hiddenLayerSize,trainFcn);
net.performFcn='mse';
net.layers{1}.transferFcn ='tansig';
net.layers{2}.transferFcn='purelin';%change transfer function output
net.divideFcn='dividetrain';
rng(19)
numNN=20;
NN=cell(1,numNN);
perfs=zeros(1,numNN); % To make array with zero
for i=1:numNN
fprintf('Training %d/%d\n', i, numNN);
net = configure(net,x,t);
[NN{i} tr] = train(net, x, t);
y=NN{i}(x);
perfs(i)=(mse(net,t,y));
end
Results
The best R2 from 20 trials as follows:
when rng(0), R2=0.74
when rng(19), R2= 0.93
1 Comment
Bharath Attoti
on 3 Dec 2022
Interesting, shows how variable the performance can be with the random initial weights; not reliable, I suppose.
Accepted Answer
More Answers (1)
KSSV
on 31 Jan 2022
rng sets the seed of the random numbers. When you gace same seed to rng, the random numbers will always be same.
for i = 1:10
rng(1)
rand
end
When you change seed value, a different random numbers will be generated. So when you fix seed as 19, the initial random weights initialized might be close and giving you the good prediction.
You may set rng to different then 19 and increase the iterations. This might take you close to target even for other values then 19.
1 Comment
Nurliana Farhana Salehuddin
on 31 Jan 2022
Categories
Find more on Deep Learning Toolbox 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!