-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeepC4.m
143 lines (122 loc) · 4.95 KB
/
DeepC4.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
%% Initialize
clear, clc, close
cd '/Users/joshuadimasaka/Desktop/PhD/GitHub/DeepC4'
%% Load Full Country Data
[ mask, maskR,...
label2rasterID, sub_label2rasterID,...
s1vv, s1vh, rgb, red1, red2, red3, red4, swir1, swir2, nir,...
dynProb, dynLabel, btype_label, label_height, bldgftprnt,...
Q,data...
] = loadCountryData();
%% Run [1] or Load [2] Training Data (30 sectors)
optloadTrainData = 2;
[ X_batch, tau_batch, tauH_batch, tauW_batch, ...
btype_label, label_height, ind_batch, nelem] = ...
loadTrainData(optloadTrainData, ...
mask, label2rasterID, sub_label2rasterID,...
s1vv, s1vh, rgb, red1, red2, red3, red4, swir1, swir2, nir,...
dynProb, dynLabel, btype_label, label_height, bldgftprnt,...
Q,data);
%% Deep Representation Learning
% Learning Parameters
learnRate = 1e-3;
numEpochs = 500;
% removed upon inspection to see if, at the sector level, learning exists
select_iter = [2:12 14:15 18:21 24 26 28];
nBatch = length(select_iter);
% Trailing Variables
trailingAvgE = [];
trailingAvgSqE = [];
trailingAvgD = [];
trailingAvgSqD = [];
gradientsE_prev = [];
gradientsD_prev = [];
% Enable Monitor Window
monitor = trainingProgressMonitor;
monitor.Metrics = [ "ReconstructionLoss", ...
"PredictionLoss", ...
"IterationTPpropR", ...
"IterationTPpropH", ...
"IterationTPpropW",...
"IterationTPpropR2", ...
"IterationTPpropH2", ...
"IterationTPpropW2"];
monitor.XLabel = "Iteration";
groupSubPlot(monitor,"ReconstructionLoss","ReconstructionLoss");
groupSubPlot(monitor,"PredictionLoss","PredictionLoss");
groupSubPlot(monitor,"IterationTPpropR","IterationTPpropR");
groupSubPlot(monitor,"IterationTPpropH","IterationTPpropH");
groupSubPlot(monitor,"IterationTPpropW","IterationTPpropW");
groupSubPlot(monitor,"IterationTPpropR2","IterationTPpropR2");
groupSubPlot(monitor,"IterationTPpropH2","IterationTPpropH2");
groupSubPlot(monitor,"IterationTPpropW2","IterationTPpropW2")
% Loop over epochs.
netE_history = cell(numEpochs,nBatch);
netD_history = cell(numEpochs,nBatch);
xTPpropR_history = zeros(numEpochs,nBatch);
xTPpropH_history = zeros(numEpochs,nBatch);
xTPpropW_history = zeros(numEpochs,nBatch);
xTPpropR2_history = zeros(numEpochs,nBatch);
xTPpropH2_history = zeros(numEpochs,nBatch);
xTPpropW2_history = zeros(numEpochs,nBatch);
ReconstructionLoss_history = zeros(numEpochs,nBatch);
PredictionLoss_history = zeros(numEpochs,nBatch);
% Train
epoch = 0; iter = 0; xIter = 0;
[netE,netD] = createAE();
while epoch < numEpochs && ~monitor.Stop
epoch = epoch + 1
for j = 1:length(select_iter) %1:nBatch
iter = select_iter(j)
% Evaluate loss and gradients.
[ lossP,lossR,...
xTPpropR,xTPpropH,xTPpropW,...
xTPpropR2,xTPpropH2,xTPpropW2,...
gradientsE,gradientsD] = ...
dlfeval(@modelLoss,...
netE,netD,...
dlarray(X_batch{iter}, 'BC'), ...
tau_batch{iter},...
tauH_batch{iter},...
tauW_batch{iter},...
btype_label,...
label_height,...
ind_batch{iter}, ...
gradientsE_prev, gradientsD_prev);
gradientsE_prev = gradientsE;
gradientsD_prev = gradientsD;
xTPpropR_history(epoch,j) = xTPpropR;
xTPpropH_history(epoch,j) = xTPpropH;
xTPpropW_history(epoch,j) = xTPpropW;
xTPpropR2_history(epoch,j) = xTPpropR2;
xTPpropH2_history(epoch,j) = xTPpropH2;
xTPpropW2_history(epoch,j) = xTPpropW2;
ReconstructionLoss_history(epoch,j) = lossR;
PredictionLoss_history(epoch,j) = lossP;
% Update learnable parameters.
[netE,trailingAvgE,trailingAvgSqE] = adamupdate(netE, ...
gradientsE,trailingAvgE,trailingAvgSqE,...
(epoch-1).*nBatch+j,learnRate,gradDecay,sqGradDecay);
netE_history{epoch,j} = netE;
[netD, trailingAvgD, trailingAvgSqD] = adamupdate(netD, ...
gradientsD,trailingAvgD,trailingAvgSqD,...
(epoch-1).*nBatch+j,learnRate,gradDecay,sqGradDecay);
netD_history{epoch,j} = netD;
recordMetrics(monitor, ...
(epoch-1).*nBatch+j, ...
ReconstructionLoss=lossR, ...
PredictionLoss=lossP, ...
IterationTPpropR=xTPpropR, ...
IterationTPpropH=xTPpropH, ...
IterationTPpropW=xTPpropW, ...
IterationTPpropR2=xTPpropR2, ...
IterationTPpropH2=xTPpropH2, ...
IterationTPpropW2=xTPpropW2);
end
end
% global
save("output/20241025_DeepGC4/global/outputTrainedModels.mat",...
"netE_history","netD_history",...
"xTPpropR_history","xTPpropH_history","xTPpropW_history",...
"xTPpropR2_history","xTPpropH2_history","xTPpropW2_history",...
"ReconstructionLoss_history","PredictionLoss_history")