preparation data for Convolutional Neural Network in regression approach

11 views (last 30 days)
Abolfazl Nejatian
Abolfazl Nejatian on 20 Nov 2017
Commented: John Albert on 2 Jan 2018
dear my friends Thanks for your guidance.
I've another question. after that, I was disappointed with LSTM Regression Layer in Matlab, i tried to use a Normal CNN for my Time series prediction. in my codes initially, i changed the dimensional of data from [1*1000] to [3*997] (with consideration time delays).
because it should be similar to an image input then changed this 2-dimensional matrix to 4-dimensional matrices, to be a proper Input data for CNN layer. and after that trained a CNN network and then finally check the prediction results with the real targets, the strange point is prediction scales is much less than the real targets, I don't have any idea Why this happened?
Many Thanks Abolfazl Nejatian
Here is my code
function RegressionCNN()
clc; clear; close all;
%%load data
X = xlsread('forexTest.xlsx');
NumDelay = 4;
Delays = setDelays(NumDelay);
[Inputs, Targets] = CreateTimeSeriesData(X', Delays);
Inputs = Inputs';
Targets = Targets';
nData = size(Inputs,1);
PERM = 1:nData; % Permutation to Shuffle Data
pTrain = .8;
nTrainData = round(pTrain*nData);
TrainInd = PERM(1:nTrainData);
TrainInputs = Inputs(TrainInd,:);
TrainTargets = Targets(TrainInd,:);
pTest = 1-pTrain;
nTestData = nData-nTrainData;
TestInd = PERM(nTrainData+1:end);
TestInputs = Inputs(TestInd,:);
TestTargets = Targets(TestInd,:);
%%Creat LSTM Layers
layers = [ ...
imageInputLayer([size(Inputs,2) 1],'Name','rinput');
%%Train LSTM network
maxEpochs = 200;
shuffle = 'never';
miniBatchSize = 10;
options = trainingOptions('sgdm', ...
'MaxEpochs',maxEpochs, ...
'Shuffle', shuffle,...
Y = TrainTargets;
X = TrainInputs';
X = build4DData(X);
net = trainNetwork(X,Y,layers,options);
%%Test Network
XTest = TestInputs';
TestTargets = TestTargets;
XTest = build4DData(XTest);
YPred = predict(net,XTest, ...
%%plot the results
close all
format long
YPred = double(YPred);
Errors = TestTargets - YPred;
MSE = mean(Errors.^2);
RMSE = sqrt(MSE);
ErrorMean = mean(Errors);
ErrorStd = std(Errors);
subplot(2,2,[1 2]);
hold on;
ylabel('Targets and Outputs');
grid on;
title(['MSE = ' num2str(MSE) ', RMSE = ' num2str(RMSE)]);
grid on;
histfit(Errors, 50);
title(['Error Mean = ' num2str(ErrorMean) ', Error StD = ' num2str(ErrorStd)]);
function XNew = build4DData(x)
XNew = zeros(size(x,1),1,1,size(x,2));
XNew(:,1,1,:) = x(:,:);
  1 Comment
John Albert
John Albert on 2 Jan 2018
Dear Abolfazl Nejatian, Did you find any solution to this problem. I have a problem similar to yours. It will be very helpful to me as well. Thanks.

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!

Translated by