-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLSTM_Predict.m
More file actions
97 lines (80 loc) · 2.58 KB
/
LSTM_Predict.m
File metadata and controls
97 lines (80 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
%% LSTM Prediction
%
% Description : This script is made to predict the future timesteps step by
% step. The computed loss is then used to have an "outlierness" of every
% point
% For every time stamp n the next points n to n+t are predicted depending
% on a specific number of previous datapoints n-t, where t is an empirical
% number and the number of the previous time stamps that influence
% the prediction
%
% Author :
% Stefan Herdy
% m01610562
%
% Date: 30.04.2020
% --------------------------------------------------
% (c) 2020, Stefan Herdy
% Chair of Automation, University of Leoben, Austria
% email: stefan.herdy@stud.unileoben.ac.at
% --------------------------------------------------
%
%% Prepare Workspace
close all;
clear;
% Add the path to the used functions
addpath(genpath(['..',filesep,'mcodeKellerLib']));
%% Settings
% LoadNet describes if the a trained net should be used or if the network
% should be trained on the training data
LoadNet = false;
% SaveNet describes if the trained network should be saved or not
SaveNet = true;
% Define how long the predicted sequences should be
TimeStep = 10;
% Define a Threshold for the maximum loss. If the max Loss for a point is
% above this Threshold, the data gets plotted for visual inspection
LossThresh = 0.8;
%% Load Data
% Ask user for site folder
myDir = uigetdir( cd, 'Select the folder for the site');
% Call generatePredInput to load the train data
[XTrain, YTrain] = generatePredInput(2, myDir, TimeStep)
%% Define LSTM Network Architecture
% An LSTM regression is used to predict a sequence of timesteps based on
% previous timesteps
inputSize = 1;
numHiddenUnits = 1000;
numResponses = 1;
layers = [ ...
sequenceInputLayer(inputSize)
bilstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer]
%% Specify the training options.
maxEpochs = 5;
miniBatchSize = 32;
options = trainingOptions('adam', ...
'MaxEpochs',maxEpochs, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'MiniBatchSize',miniBatchSize, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',125, ...
'LearnRateDropFactor',0.1, ...
'Verbose',0, ...
'Plots','training-progress');
%% Train the network
% Train the network based on the above defined settings
if LoadNet == false;
prednet = trainNetwork(XTrain,YTrain,layers,options);
end
if LoadNet == true;
load prednet;
end
if SaveNet == true;
save prednet;
end
%% Test LSTM Network
% call makePrediction to test the trained or loaded LSTM network
makePrediction(2, myDir, TimeStep, prednet, LossThresh);