Multi-step prediction code construction

6 views (last 30 days)
  • Constructing the multi-step prediction model is my target.
  • 17 channels of input and 17 channels of output exist in my dataset.
  • Specific period (ex. 7 days) of input and output data is utilized for training the model.
  • My Q. Is the code is well constructed for multi-step prediction model for same period of input and output?
  • If the length of input and output data is different, the Code does not works. How can I modify the code? I think if I change Y_train, cell into double array, num of output layer cell should be increased in (num_channels * output target time steps). But this method is not proper.
x_train = rand(9665,17); % data at T for 17 channels (For Training)
y_test = rand(9665,17); % data at T+1 for 17 channels (For Training)
x_test = rand(2417,17); % data at T for 17 channels (For Testing)
y_test = rand(2417,17); % data at T+1 for 17 channels (For Testing)
%% Multi-step ahead prediction with adjustable input/output windows
% input_steps and output steps are same now (assumption)
input_steps = 1000; % 1주 (24시간, 시간 간격 6개)
output_steps = 1000; % 1시간 (6개 간격)
num_samples_train = numtrain - input_steps - output_steps + 1;
num_samples_test = size(x_test,1) - input_steps - output_steps + 1;
X_train = cell(num_samples_train,1);
Y_train = cell(num_samples_train,1);
X_test = cell(num_samples_test,1);
Y_test = cell(num_samples_test,1);
%% 셀 배열에 각 시퀀스를 저장
for i = 1:num_samples_train
% X: (input_steps × 모든 채널)
X_train{i} = x_train(i : i+input_steps-1, :);
% Y: (output_steps × Tension 채널 수)
Y_train{i} = Tension(i+input_steps : i+input_steps+output_steps-1, :);
end
for i = 1:num_samples_test
X_test{i} = x_test(i : i+input_steps-1, :);
Y_test{i} = Tension(i+input_steps : i+input_steps+output_steps-1, :);
end
f = figure;
hold on
plot(1:1000,X_train{44}(:,1),'--')
plot(1001:2000,Y_train{44}(:,1),'-')
%% 각 셀의 크기 확인 (예시)
disp(size(X_train)); % [8514, 1] 형태
disp(size(X_train{1})); % [1008, 36] 형태 (예시)
disp(size(Y_train{1})); % [144, 17] 형태 (예시)
%% model architecture
net = dlnetwork;
tempNet = [
sequenceInputLayer(17,"Name","sequenceinput")
batchNormalizationLayer("Name","batchnorm")
convolution1dLayer(3,32,"Name","conv1d_4","Padding","same")
reluLayer("Name","relu_5")];
net = addLayers(net,tempNet);
tempNet = identityLayer("Name","identity");
net = addLayers(net,tempNet);
tempNet = [
convolution1dLayer(4,32,"Name","conv1d","Padding","same")
maxPooling1dLayer(3,"Name","maxpool1d","Padding","same")
reluLayer("Name","relu")];
net = addLayers(net,tempNet);
tempNet = [
additionLayer(2,"Name","addition")
reluLayer("Name","relu_6")
identityLayer("Name","identity_1")];
net = addLayers(net,tempNet);
tempNet = identityLayer("Name","identity_2");
net = addLayers(net,tempNet);
tempNet = [
convolution1dLayer(32,32,"Name","conv1d_2","Padding","same")
maxPooling1dLayer(3,"Name","maxpool1d_2","Padding","same")
reluLayer("Name","relu_2")];
net = addLayers(net,tempNet);
tempNet = [
additionLayer(2,"Name","addition_2")
reluLayer("Name","relu_7")
identityLayer("Name","identity_3")];
net = addLayers(net,tempNet);
tempNet = [
convolution1dLayer(4,32,"Name","conv1d_1","Padding","same")
maxPooling1dLayer(3,"Name","maxpool1d_1","Padding","same")
reluLayer("Name","relu_1")];
net = addLayers(net,tempNet);
tempNet = [
additionLayer(2,"Name","addition_1")
reluLayer("Name","relu_8")];
net = addLayers(net,tempNet);
tempNet = [
convolution1dLayer(32,32,"Name","conv1d_3","Padding","same")
maxPooling1dLayer(3,"Name","maxpool1d_3","Padding","same")
reluLayer("Name","relu_3")];
net = addLayers(net,tempNet);
tempNet = [
additionLayer(2,"Name","addition_3")
reluLayer("Name","relu_9")];
net = addLayers(net,tempNet);
tempNet = [
additionLayer(2,"Name","addition_4")
% functionLayer(@(X) cbt2cb(X), Formattable=true, Acceleratable=true)
reluLayer("Name","relu_10")
fullyConnectedLayer(128,"Name","fc")
reluLayer("Name","relu_4")
fullyConnectedLayer(17,"Name","fc_1")];
net = addLayers(net,tempNet);
% 헬퍼 변수 정리
clear tempNet;
net = connectLayers(net,"relu_5","identity");
net = connectLayers(net,"relu_5","identity_2");
net = connectLayers(net,"identity","conv1d");
net = connectLayers(net,"identity","addition/in1");
net = connectLayers(net,"relu","addition/in2");
net = connectLayers(net,"identity_2","conv1d_2");
net = connectLayers(net,"identity_2","addition_2/in1");
net = connectLayers(net,"relu_2","addition_2/in2");
net = connectLayers(net,"identity_1","conv1d_1");
net = connectLayers(net,"identity_1","addition_1/in1");
net = connectLayers(net,"relu_1","addition_1/in2");
net = connectLayers(net,"identity_3","conv1d_3");
net = connectLayers(net,"identity_3","addition_3/in1");
net = connectLayers(net,"relu_3","addition_3/in2");
net = connectLayers(net,"relu_8","addition_4/in1");
net = connectLayers(net,"relu_9","addition_4/in2");
net = initialize(net);
analyzeNetwork(net);
%%
options = trainingOptions('adam', ...
'MaxEpochs',10, ...
'InitialLearnRate',0.005, ...
'GradientThresholdMethod','global-l2norm',...
'L2Regularization',1e-5,...
'MiniBatchSize',50,...
'GradientThreshold',1,...
'Shuffle','every-epoch',...
'LearnRateSchedule','piecewise',...
'LearnRateDropPeriod',100,...
'VerboseFrequency',50,...
'LearnRateDropFactor',0.9,...
'ExecutionEnvironment','auto',...
'Verbose',true, ...
'Plots','none');
% 모델 학습
net1 = trainnet(X_train,Y_train, net, 'mse', options);
%% Model Testing
Y_pred = minibatchpredict(net, X_test);
%%
f=figure;
hold on
plot(Y_pred(:,1,1))
plot(Y_pred(1,:,1))

Accepted Answer

TED MOSBY
TED MOSBY on 18 Mar 2025
Hi Jingoo,
Your final network layer is fullyConnectedLayer(17):
  • That means each mini‐batch sample ends up being mapped to a 17‐dimensional output vector (i.e. one time step of 17 channels).
  • However, your Y_train{i} and Y_test{i} cells each contain output_steps time steps × 17 channels.
  • So, if output_steps > 1, the network output dimension does not match the dimension of Y_train{i} any more.
Flatten the time dimension at the very end and use a single fullyConnectedLayer that outputs all (output_steps × number_of_channels) at once. After the network outputs that vector, you reshape it back to [output_steps, number_of_channels].
Make sure that whatever training function (trainnet, minibatchpredict, etc.) you are using can handle your new shape of Y_train{i} and does the MSE properly.
Hope this helps!

More Answers (0)

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!