Finally i wrote this code for wind speed prediction with 3 parameters, why does my code has different prediction for the same dataset on each run?

5 views (last 30 days)
load('input.mat');
X = tonndata(inputData(:,(1:3)),false,false);
T = tonndata(inputData(:,4),false,false);
N = 100; % Multi-step ahead prediction
inputSeries = X(1:end);
targetSeries = T(1:end);
inputSeriesVal = X(end-N+1:end);
targetSeriesVal = T(end-N+1:end);
delay = 1; %one hour
neuronsHiddenLayer = 10;
% Network Creation
net = narxnet(1:delay,1:delay,neuronsHiddenLayer);
[Xs,Xi,Ai,Ts] = preparets(net,inputSeries,{},targetSeries);
net = train(net,Xs,Ts,Xi,Ai);
view(net)
Y = net(Xs,Xi,Ai);
% Performance for the series-parallel implementation, only
% one-step-ahead prediction
perf = perform(net,Ts,Y);
[Xs1,Xio,Aio] = preparets(net,inputSeries(1:end-delay),{},targetSeries(1:end-delay));
[Y1,Xfo,Afo] = net(Xs1,Xio,Aio);
[netc,Xic,Aic] = closeloop(net,Xfo,Afo);
[yPred,Xfc,Afc] = netc(inputSeriesVal,Xic,Aic);
multiStepPerformance = perform(net,yPred,targetSeriesVal);
view(netc)
figure;
plot([cell2mat(targetSeries),nan(1,N);
nan(1,length(targetSeries)),cell2mat(yPred);]')
legend('Original Targets','Network Predictions')

Answers (1)

Jayanti
Jayanti on 9 Oct 2024
Hi Morteza,
I understand that the predicted values are changing every time the code is executed.
The reason behind this can be random initialization of weights in the neural network. To fix this issue initialize the random number generator to a fixed seed before training the network. This will make sure that initial weight assignment remains same each time you run the code.
You can fix this by using
rng(seed_value)
You can specify any seed value you want.
I also reproduced this scenario using custom dataset and by fixing the seed value, the code resulted in the same prediction values for each run.
I am also attaching official MathWorks documentation on rng for your reference:
Hope it helps!

Categories

Find more on Time Series 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!