function RegressionCNN()
clc; clear; close all;
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;
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,:);
layers = [ ...
imageInputLayer([size(Inputs,2) 1],'Name','rinput');
convolution2dLayer(1,25);
reluLayer();
fullyConnectedLayer(1);
regressionLayer('Name','routput')];
maxEpochs = 200;
shuffle = 'never';
miniBatchSize = 10;
options = trainingOptions('sgdm', ...
'MaxEpochs',maxEpochs, ...
'Shuffle', shuffle,...
'MiniBatchSize',miniBatchSize,...
'Verbose',1,...
'Plots','training-progress',...
'SequenceLength','longest',...
'ExecutionEnvironment','cpu');
Y = TrainTargets;
X = TrainInputs';
X = build4DData(X);
net = trainNetwork(X,Y,layers,options);
XTest = TestInputs';
TestTargets = TestTargets;
XTest = build4DData(XTest);
YPred = predict(net,XTest, ...
'MiniBatchSize',miniBatchSize,...
'ExecutionEnvironment','cpu');
close all
format long
YPred = double(YPred);
whos('TestTargets')
whos('YPred')
Errors = TestTargets - YPred;
MSE = mean(Errors.^2);
RMSE = sqrt(MSE);
ErrorMean = mean(Errors);
ErrorStd = std(Errors);
subplot(2,2,[1 2]);
plot(TestTargets);
hold on;
plot(YPred);
legend('Targets','Outputs');
ylabel('Targets and Outputs');
grid on;
subplot(2,2,3);
plot(Errors);
title(['MSE = ' num2str(MSE) ', RMSE = ' num2str(RMSE)]);
ylabel('Errors');
grid on;
subplot(2,2,4);
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 (view profile)
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/368043-preparation-data-for-convolutional-neural-network-in-regression-approach#comment_520870
Sign in to comment.