Why my code is not predicting values in matlab

3 views (last 30 days)
Muhammad Usman Saleem
Muhammad Usman Saleem on 19 Jun 2022
I'm using NN to predict my missing values in dataset. I've prepared this code, its trainig and testing giving me less RMSE but when I try to predict values its just predict the value till continous data. My missing data vector is
2.97000000000000
3.09000000000000
2.86000000000000
3.25000000000000
3.30000000000000
3.29000000000000
3.34000000000000
3.29000000000000
3.12000000000000
3.02000000000000
2.93000000000000
2.55000000000000
NaN
NaN
NaN
3.03000000000000
3.04000000000000
NaN
NaN
NaN
I'm reading this vector in excel file using this code:
clear
file=xlsread('Myvector.xlsx'); % you can delete first column as its date column
data=cell(size(file,2),1); %features by variables
for i=1:size(data,1)
data{i}=file(:,i).'; % feature wise timeseries dataset of variables
end
Ec=data{2};
id=find(~isnan(data{2})); % finding the non missing dataset
numObservations = numel (id);
idxTraining=ceil(85/100*numObservations); % getting training length
idxTesting=floor(15/100*numObservations);
%
idxTrain=id(1:idxTraining);
idxTest=id(idxTraining+1:end);
%
dataTrain = Ec(idxTrain); %getting the training data
dataTest = Ec(idxTest); % getting the testing data
% Trainging the dataset
X=dataTrain;
XTrain=X(:,1:end-1);
TTrain=X(:,2:end);
%%%
% normalization======
muX = mean(XTrain(:));
sigmaX = std(XTrain);
%
muT = mean(TTrain);
sigmaT = std(TTrain);
XTrainn = (XTrain - muX) ./ sigmaX;
TTrainn = (TTrain - muT) ./ sigmaT;
%%%%%===================
%Define LSTM Network Architecture
numChannels = size(data{1},1);
layers = [
sequenceInputLayer(numChannels)
lstmLayer(128)
fullyConnectedLayer(numChannels)
regressionLayer];
%%Specify Training Options
maxEpochs = 100; ?
miniBatchSize = 27;
%setting option
options = trainingOptions('adam', ...
'ExecutionEnvironment','cpu', ...
'MaxEpochs',maxEpochs, ...
'MiniBatchSize',miniBatchSize, ...
'GradientThreshold',1, ...
'Verbose',false, ...
'Plots','training-progress');
%%Train Neural Network
net = trainNetwork(XTrainn,TTrainn,layers,options);
X=dataTest;
%XTest = (X(:,1:end-1) - muX) ./ sigmaX
%TTest = (X(:,2:end) - muT) ./ sigmaT;
XTest = (X - muX) ./ sigmaX
TTest = (X - muT) ./ sigmaT;
%%Make predictions using the test data.
YTest = predict(net,XTest)%,SequencePaddingDirection="left");
for i = 1:size(YTest,1)
rmse(i) = sqrt(mean((YTest(i) - TTest(i)).^2));
end
numTimeSteps = size(Ec,2);
numPredictionTimeSteps = numTimeSteps;
Y = zeros(numChannels,numPredictionTimeSteps);
for t = 1:numPredictionTimeSteps
Xt = Ec(:,t);
[net,Y(:,t)] = predictAndUpdateState(net,Xt);
end
My Y vector is not predicting the NaN values, hence I am posting this question to dicuscc with you experts. I am not to NN and just google and youtubing I am utilizing this Matlab facility. Please correct last portion of my code sothat I able to get predicted value of my missing vector? My version of Matlab is 2019a
Many thanks in advance
  1 Comment
Muhammad Usman Saleem
Muhammad Usman Saleem on 19 Jun 2022
it's looks to me problem is with this portion of this code, here prediction is getting wrong by my hands
numTimeSteps = size(Ec,2);
numPredictionTimeSteps = numTimeSteps;
Y = zeros(numChannels,numPredictionTimeSteps);
for t = 1:numPredictionTimeSteps
Xt = Ec(:,t);
[net,Y(:,t)] = predictAndUpdateState(net,Xt);
end

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!