About LSTM model error modifying

1 view (last 30 days)
형현
형현 on 2 Feb 2024
Commented: Walter Roberson on 2 Feb 2024
% Import Excel data from outside
file_path = '한화 합본.xlsx'; %Excel file path specified
sheet_name = 'Sheet1'; name % sheet
data = readtable(file_path, 'Sheet', sheet_name);
% Specify selected variables
selectedVariables = {'E_Price', 'ror', 'Construction', 'Vacancy', 'KOSPI', 'Bond', 'CC2', 'Exchange', 'Rent'};
% Extract only the selected variables from the data frame
selectedData = data(:, selectedVariables);
% Pre-processing of training data
XTrain = cell(1, size(selectedData, 2));
TTrain = cell(1, size(selectedData, 2));
for i = 1:size(selectedData, 2)
X = selectedData.(selectedVariables{i});
XTrain{i} = (X(1:end-1) - mean(X(1:end-1))) / std(X(1:end-1));
TTrain{i} = (X(2:end) - mean(X(2:end))) / std(X(2:end));
end
% Defining LSTM Neural Network Architecture
numSelectedChannels = size(selectedData, 2);
layers = [
sequenceInputLayer (1, 'Name', 'input') % change
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
lstmLayer(128, 'Name', 'lstm')
fullyConnectedLayer(numSelectedChannels, 'Name', 'fc')
regressionLayer('Name', 'output')];
% Setting training options
options = trainingOptions("adam", ...
'MaxEpochs', 200, ...
'SequencePaddingDirection', 'left', ...
'Shuffle', 'every-epoch', ...
'Plots', 'training-progress', ...
'Verbose', 0);
% LSTM Neural Network Training
net = trainNetwork(XTrain, TTrain, layers, options);
% Keep the rest of the code the same
% LSTM Neural Network Test
XTest = XTrain; % Simple use of training data as test data
YTest = predict(net, XTest, 'SequencePaddingDirection', 'left');
% RMSE calculation
rmse = zeros(1, size(YTest, 1));
for i = 1:size(YTest, 1)
rmse(i) = sqrt(mean((YTest{i} - TTrain{i}).^2, 'all'));
end
% Visualize the RMSE histogram
figure
histogram(rmse)
xlabel("RMSE")
ylabel("Frequency")
% Calculate mean RMSE
meanRMSE = mean(rmse);
% Visualize prediction results (open loop prediction)
figure
tiledlayout(numSelectedChannels, 1)
title("Open Loop Forecasting")
for i = 1:numSelectedChannels
nexttile
plot(TTrain{i})
hold on
plot(1:size(YTest{i}, 2), YTest{i}, '--')
ylabel(selectedVariables{i})
end
xlabel("Time Step")
nexttile(1)
legend(["Input" "Forecasted"])
Based on the attached time series data, we want to create a one-year yield prediction model after the deadline for collecting data through the LSTM model... Errors continue to occur
  1 Comment
Walter Roberson
Walter Roberson on 2 Feb 2024
% Import Excel data from outside
file_path = '한화 합본.xlsx'; %Excel file path specified
sheet_name = 'Sheet1'; sheet_name % sheet
sheet_name = 'Sheet1'
data = readtable(file_path, 'Sheet', sheet_name);
% Specify selected variables
selectedVariables = {'E_Price', 'ror', 'Construction', 'Vacancy', 'KOSPI', 'Bond', 'CC2', 'Exchange', 'Rent'};
% Extract only the selected variables from the data frame
selectedData = data(:, selectedVariables);
% Pre-processing of training data
XTrain = cell(1, size(selectedData, 2));
TTrain = cell(1, size(selectedData, 2));
for i = 1:size(selectedData, 2)
X = selectedData.(selectedVariables{i});
XTrain{i} = (X(1:end-1) - mean(X(1:end-1))) / std(X(1:end-1));
TTrain{i} = (X(2:end) - mean(X(2:end))) / std(X(2:end));
end
% Defining LSTM Neural Network Architecture
numSelectedChannels = size(selectedData, 2);
layers = [
sequenceInputLayer(1, 'Name', 'input') % change
lstmLayer(128, 'Name', 'lstm')
fullyConnectedLayer(numSelectedChannels, 'Name', 'fc')
regressionLayer('Name', 'output')];
% Setting training options
options = trainingOptions("adam", ...
'MaxEpochs', 200, ...
'SequencePaddingDirection', 'left', ...
'Shuffle', 'every-epoch', ...
'Plots', 'training-progress', ...
'Verbose', 0);
% LSTM Neural Network Training
net = trainNetwork(XTrain, TTrain, layers, options);
Error using trainNetwork
The training sequences are of feature dimension 304 but the input layer expects sequences of feature dimension 1.
% Keep the rest of the code the same
% LSTM Neural Network Test
XTest = XTrain; % Simple use of training data as test data
YTest = predict(net, XTest, 'SequencePaddingDirection', 'left');
% RMSE calculation
rmse = zeros(1, size(YTest, 1));
for i = 1:size(YTest, 1)
rmse(i) = sqrt(mean((YTest{i} - TTrain{i}).^2, 'all'));
end
% Visualize the RMSE histogram
figure
histogram(rmse)
xlabel("RMSE")
ylabel("Frequency")
% Calculate mean RMSE
meanRMSE = mean(rmse);
% Visualize prediction results (open loop prediction)
figure
tiledlayout(numSelectedChannels, 1)
title("Open Loop Forecasting")
for i = 1:numSelectedChannels
nexttile
plot(TTrain{i})
hold on
plot(1:size(YTest{i}, 2), YTest{i}, '--')
ylabel(selectedVariables{i})
end
xlabel("Time Step")
nexttile(1)
legend(["Input" "Forecasted"])

Sign in to comment.

Answers (0)

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!