Why do I get the same output from Predict function in LSTM?

6 views (last 30 days)
Hi everyone.
I have input matrix with 8 feature and 1000 samples.I want to predict price with this matrix. I train LSTM with input matrix and test LSTM with Predict function. I test trained LSTM 10 times with predict function,But I get the same output from Predict function every time.How cal I solve this problem? here is may code:
function LSTM_net(data,dataTest,filename,range,date,varargin)
%--------------80% of data for train and 20% for validation----------------
out_day=cell.empty;
index=size(data{1,1},1)*0.8;
findex=round(index,0);
dataTrain=data(1:findex,:);
dataval=data(findex+1:end,:);
%-----------------Normalization of training/validation data----------------
dataTrain(isnan(dataTrain))=0;
dataval(isnan(dataval))=0;
dataTrain=rescale(dataTrain,0,1);
dataval=rescale(dataval,0,1);
YTrain = dataTrain(:,end)';
XTrain = dataTrain(:,1:end-1)';
XTrain = num2cell(XTrain,1);
YTrain = num2cell(YTrain,1);
yval= dataval(:,end)';
xval = dataval(:,1:end-1)';
xval = num2cell(xval,1);
yval = num2cell(yval,1);
%-----------------------Define Network Architecture------------------------
numResponses = size(YTrain{1},1);
featureDimension = size(XTrain{1},1);
numHiddenUnits = 15;
layers = [ ...
sequenceInputLayer(featureDimension)
lstmLayer(numHiddenUnits,'OutputMode','sequence')
dropoutLayer(0.5) %%0.5
fullyConnectedLayer(numResponses)
regressionLayer];
maxepochs = 500;
options = trainingOptions('sgdm', ...
'MaxEpochs',maxepochs, ...
'InitialLearnRate',0.01, ...
'L2Regularization',0.001,...
'ValidationData',{xval,yval},...
'ValidationPatience',5,...
'ValidationFrequency',10);
%---------------------------------set test data----------------------------
dataTest=rescale(dataTest,0,1);
YTest = dataTest{k,1}(:,end)';
XTest = dataTest{k,1}(:,1:end-1)';
XTest = num2cell(XTest,1);
YTest = num2cell(YTest,1);
%---------------------------------Train the Network------------------------
out_net=single.empty;
%load('net_checkpoint__110__2021_11_01__10_49_03_555','net');
[net1,info] = trainNetwork(XTrain,YTrain,layers,options);
for i=1:10
YPred = predict(net1,XTest);
net1 = resetState(net1);
%figure;
%subplot(2,1,1);
y1 = (cell2mat(YPred(1:end, 1:end)));
%plot(y1);
%title('Forcasted');
%subplot(2,1,2);
y2 = (cell2mat(YTest(1:end, 1:end))');
%plot(y2);
%title('Observed');
y1(isnan(y1))=0;
y2(isnan(y2))=0;
%----------------------------calculate MAE,RMSE,MAPE-----------------------
out_net(i,1)=mean(info.TrainingRMSE(1,:),2);
out_net(i,2)=mean(abs(y1-y2)); %MAE
out_net(i,3)=mean(abs((y1-y2)/mean(y1))); %MAPE
out_net(i,4) = sqrt(mean((y1-y2).^2)); %RMSE
if size(varargin,1)==1 %for plot regression
predict_y(:,i)=y1;
end
end
end
I trained LSTM one time and predict it for 10 times and I get the same YPred answer every time.Is my code true?Please help me.

Answers (0)

Categories

Find more on Sequence and Numeric Feature Data Workflows 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!