About LSTM model error modifying
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
% 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 Kommentar
Walter Roberson
am 2 Feb. 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"])
Antworten (0)
Siehe auch
Kategorien
Mehr zu Sequence and Numeric Feature Data Workflows finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!