Numbers prediction using advanced matlab code

5 ビュー (過去 30 日間)
Mahmoud Gamal Eldeen
Mahmoud Gamal Eldeen 2023 年 8 月 26 日
I have a matlab code which it's input is 10 series numbers which have a certain sequance, and it's output is predectid next fife numbers, but it has many problems, first if I run the code many times with the same 10 inputs, the output is changed while it should be constant, the next problem is that the output numbers should not be less than 1, but it predicts less than 1, what is the problem ?
Code
function predicted = predict_next_five_nn(series)
% Assuming series is a column vector
if length(series) ~= 10
error('Input series must have 10 elements');
end
% Prepare data using a rolling window approach
inputs = [];
targets = [];
for i = 1:(length(series) - 1)
inputs = [inputs; series(i:(i+8))'];
targets = [targets; series(i+9)];
end
% Define and train the neural network
net = feedforwardnet(10);
net.trainParam.epochs = 1000;
net = train(net, inputs', targets');
% Predict the next five values
current_input = series(end-9:end);
predicted = [];
for i = 1:5
next_val = net(current_input');
predicted = [predicted; next_val];
% Ensure the predicted number is not less than 1
if next_val < 1
next_val = 1;
end
current_input = [current_input(2:end); next_val];
end
end
Calling ( For exambe )
series = [2, 4, 7, 11, 16, 22, 29, 37, 46, 56]';
predicted_numbers = predict_next_five_nn(series);
disp(predicted_numbers);

回答 (2 件)

the cyclist
the cyclist 2023 年 8 月 26 日
The fact that you do not reproduce the same output every time is probably because the algorithm you are using has some random component to it. Put the line
rng default
at the beginning of your code, to ensure the same pseudorandom number draws each time. See the documentation for rng for details.
  1 件のコメント
Mahmoud Gamal Eldeen
Mahmoud Gamal Eldeen 2023 年 8 月 26 日
I use it, the results become constant, but there is a big problem, results are wrongly predicted, can you help me to find the write formula to do this task please ?

サインインしてコメントする。


John D'Errico
John D'Errico 2023 年 8 月 26 日
編集済み: John D'Errico 2023 年 8 月 26 日
A neural net will use a random start point in the training. Start from a different set of parameters for the net, and you will get different results. Hopefully, if your data is good, and you have sufficient data, AND you are not overfitting the data, then the results will be qualitatively similar.
HOWEVER, you are using this to extrapolate. SIGH. Extrapolation is one of the more ill-posed a problems you can choose. And that means when you extrapolate, expect random garbage. Since you are not giving this net very much data, what do you expect?
Finally, you are getting sometimes numbers less than 1. Yet, I don't see anything in what you did that tells the computer the prediction should never go less than 1. (Ok, besides a check at the end to replace numbers less than 1 with a 1.) Perhaps you were thinking you wanted them not to go less than 1. Concentrate REALLY HARD. But the mind reading toolbox is still pretty buggy. Computers just don't do that well. Except in the movies, of course, where computers can do anything. And again, you are using this to extrapolate. So again in the real world, expect random garbage as an extrapolant.
Just because you are using an "advanced" code to solve the problem does not mean you should expect anything intelligent as a result. Computers are just not very intelligent. At least not until that blasted mind reading toolbox starts to work properly. I fear that day, but it seems to be arriving faster than I want.
Anyway, I might add that a simple, totally unintelligent finite differnce would suggest this series is following a linear trend, AND therefore something as dumb as polyfit would give you better answers.
series = [2, 4, 7, 11, 16, 22, 29, 37, 46, 56]';
diff(series)
ans = 9×1
2 3 4 5 6 7 8 9 10
diff(diff(series))
ans = 8×1
1 1 1 1 1 1 1 1
So we know the model is a purely quadratic polynomial, since a second difference is constant. So we could do this:
p2 = polyfit(1:length(series),series,2)
p2 = 1×3
0.5000 0.5000 1.0000
And then use that polynomial to predict future values.
Using that neural net to predict here is the mathematical equivalent of using a train to carry a Mack truck, which in turn is carrying a pea to Boston.

カテゴリ

Help Center および File ExchangeDeep Learning Toolbox についてさらに検索

製品


リリース

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by