About LSTM model error modifying
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
% 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
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 comentario
Walter Roberson
el 2 de Feb. de 2024
% Import Excel data from outside
file_path = '한화 합본.xlsx'; %Excel file path specified
sheet_name = 'Sheet1'; sheet_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
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"])
Respuestas (0)
Ver también
Categorías
Más información sobre Sequence and Numeric Feature Data Workflows en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!