-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLSTM_Predict_Disc.m
More file actions
148 lines (114 loc) · 4.13 KB
/
LSTM_Predict_Disc.m
File metadata and controls
148 lines (114 loc) · 4.13 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
%% 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']));
%% Load Data
% Ask user for site folder
%myDir = uigetdir( cd, 'Select the folder for the site');
myDir = 'C:\Users\stefa\Desktop\Masterarbeit\Code\sites\SeestadtAspern'
%% Settings
% LoadNet describes if the a trained net should be used or if the network
% should be trained on the training data
LoadNet = true;
% SaveNet describes if the trained network should be saved or not
SaveNet = false;
timelist = [12, 12, 12, 15, 15, 15, 18, 18, 18, 21, 21, 3, 3, 3, 3, 3, 3, 6, 6, 6, 9, 9, 9]
% Define how long the predicted sequences should be
for k=1:length(timelist)
TimeStep = timelist(k);
% 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
LossThreshDepth = 0;
LossThreshDisc = 0;
% Discontinuity defines wheter the discontinuity data should be used for the
% prediction or not
Discontinuity = true;
% The pahase defines wich phase should be analysed. 1 = compaction phase, 2 =
% penetration phase.
phase = 2;
if Discontinuity == true
LossThresh = LossThreshDisc;
else
LossThresh = LossThreshDepth;
end
% Call generatePredInput to load the train data
[XTrain, YTrain] = generatePredInputDisc(phase, myDir, TimeStep, Discontinuity)
%% Define LSTM Network Architecture
% An LSTM regression is used to predict a sequence of timesteps based on
% previous timesteps
inputSize = 1;
numHiddenUnits = 1500;
numResponses = 1;
layers = [ ...
sequenceInputLayer(inputSize)
bilstmLayer(numHiddenUnits)
%bilstmLayer(numHiddenUnits)
%bilstmLayer(numHiddenUnits)
%fullyConnectedLayer(10)
fullyConnectedLayer(numResponses)
regressionLayer]
%% Specify the training options.
maxEpochs = 1;
miniBatchSize = 8;
options = trainingOptions('adam', ...
'MaxEpochs',maxEpochs, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'MiniBatchSize',miniBatchSize, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',10, ...
'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
s1 = int2str(TimeStep);
s2 = int2str(numHiddenUnits);
s3 = int2str(maxEpochs);
name = strcat('PredDisc_highdrop','_',s1,'_',s2,'_',s3);
name = convertCharsToStrings(name);
name = string(name);
ePath = 'C:\Users\stefa\Desktop\Masterarbeit\Code\KellerVibroV4-1_1\mcodeKeller\prednets';
Files = dir(fullfile(ePath,'*.mat'));
FileName = Files(k).name;
% %dFileName = dFiles(k).name;
path = strcat(ePath, '\', FileName);
% %dpath = strcat(dPath, '\', dFileName);
load(path)
% %load(dpath)
if SaveNet == true;
save(name, 'prednet');
end
%% Test LSTM Network
% call makePrediction to test the trained or loaded LSTM network
makePredictionDisc(phase, myDir, TimeStep, prednet, LossThresh, Discontinuity, FileName);
%makePrediction(phase, myDir, TimeStep, prednet, LossThresh);
end