-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTrainANNsinLoop.m
142 lines (123 loc) · 4.13 KB
/
TrainANNsinLoop.m
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
% ------------------ Georgios Etsias 17th March 2020 ----------------- %
%% Training ANN
max=10^10;
for ii=1:10
% Solve an Autoregression Problem with External Input with a NARX Neural Network
% Script generated by Neural Time Series app
% Created 17-Mar-2020 14:33:37
%
% This script assumes these variables are defined:
%
% TrainingInput - input time series.
% TrainingOutput - feedback time series.
X = TrainingInput;
T = TrainingOutput;
% Choose a Training Function
% For a list of all training functions type: help nntrain
% 'trainlm' is usually fastest.
% 'trainbr' takes longer but may be better for challenging problems.
% 'trainscg' uses less memory. Suitable in low memory situations.
trainFcn = 'trainbr'; % Bayesian Regularization backpropagation.
% Create a Nonlinear Autoregressive Network with External Input
inputDelays = 1:2;
feedbackDelays = 1:2;
hiddenLayerSize = 10;
net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize,'open',trainFcn);
% Prepare the Data for Training and Simulation
% The function PREPARETS prepares timeseries data for a particular network,
% shifting time by the minimum amount to fill input states and layer
% states. Using PREPARETS allows you to keep your original time series data
% unchanged, while easily customizing it for networks with differing
% numbers of delays, with open loop or closed loop feedback modes.
[x,xi,ai,t] = preparets(net,X,{},T);
% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Train the Network
[net,tr] = train(net,x,t,xi,ai);
% Test the Network
y = net(x,xi,ai);
e = gsubtract(t,y);
performance = perform(net,t,y)
% View the Network
view(net)
% Plots
% Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, ploterrhist(e)
%figure, plotregression(t,y)
%figure, plotresponse(t,y)
%figure, ploterrcorr(e)
%figure, plotinerrcorr(x,e)
% Closed Loop Network
% Use this network to do multi-step prediction.
% The function CLOSELOOP replaces the feedback input with a direct
% connection from the outout layer.
netc = closeloop(net);
netc.name = [net.name ' - Closed Loop'];
view(netc)
[xc,xic,aic,tc] = preparets(netc,X,{},T);
yc = netc(xc,xic,aic);
closedLoopPerformance = perform(net,tc,yc)
% Step-Ahead Prediction Network
% For some applications it helps to get the prediction a timestep early.
% The original network returns predicted y(t+1) at the same time it is
% given y(t+1). For some applications such as decision making, it would
% help to have predicted y(t+1) once y(t) is available, but before the
% actual y(t+1) occurs. The network can be made to return its output a
% timestep early by removing one delay so that its minimal tap delay is now
% 0 instead of 1. The new network returns the same outputs as the original
% network, but outputs are shifted left one timestep.
nets = removedelay(net);
nets.name = [net.name ' - Predict One Step Ahead'];
view(nets)
[xs,xis,ais,ts] = preparets(nets,X,{},T);
ys = nets(xs,xis,ais);
stepAheadPerformance = perform(nets,ts,ys)
%% Prediction
% Creates a prediction using an already trained closed NARX
%X = tonndata(InputT5,false,false);
%T = tonndata(OutputT5,false,false);
%% Drive Cycle 1
X2=TestInput2;
T2=TestOutput2;
[x,xi,ai,t] = preparets(netc,X2,{},T2);
yc2 = netc(x,xi,ai);
yc2=yc2';
a=(cell2mat(T2))';
b=cell2mat(yc2);
error2 = immse(a(3:end),b)
clear a b
%% Drive Cycle 2
X11=TestInput11;
T11=TestOutput11;
[x,xi,ai,t] = preparets(netc,X11,{},T11);
yc11 = netc(x,xi,ai);
yc11=yc11';
a=(cell2mat(T11))';
b=cell2mat(yc11);
error11 = immse(a(3:end),b)
clear a b
%% Drive Cycle 3
X19=TestInput19;
T19=TestOutput19;
[x,xi,ai,t] = preparets(netc,X19,{},T19);
yc19 = netc(x,xi,ai);
yc19=yc19';
a=(cell2mat(T19))';
b=cell2mat(yc19);
error19 = immse(a(3:end),b)
clear a b
T2=T2';
T11=T11';
T19=T19';
%clearvars -except yc2 yc11 yc19 T2 T11 T19
error=error2+error11+error19;
if error<=max
max=error;
save('netc','netc')
save('net','net')
end
end