-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pipeline_KNet.py
203 lines (145 loc) · 7.62 KB
/
Pipeline_KNet.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import torch
import torch.nn as nn
import random
class Pipeline_KNET:
def __init__(self, folderName, modelName):
super().__init__()
self.folderName = folderName + '/'
self.modelName = modelName
self.modelFileName = self.folderName + "model_" + self.modelName + ".pt"
self.PipelineName = self.folderName + "pipeline_" + self.modelName + ".pt"
def save(self):
torch.save(self, self.PipelineName)
def setssModel(self, ssModel):
self.ssModel = ssModel
def setModel(self, model):
self.model = model
def setTrainingParams(self, n_Epochs, n_Batch, learningRate, weightDecay):
self.N_Epochs = n_Epochs # Number of Training Epochs
self.N_B = n_Batch # Number of Samples in Batch
self.learningRate = learningRate # Learning Rate
self.weightDecay = weightDecay # L2 Weight Regularization - Weight Decay
# MSE LOSS Function
self.loss_fn = nn.MSELoss(reduction='mean')
# Use the optim package to define an Optimizer that will update the weights of
# the model for us. Here we will use Adam; the optim package contains many other
# optimization algoriths. The first argument to the Adam constructor tells the
# optimizer which Tensors it should update.
self.optimizer = torch.optim.Adam(self.model.parameters(), lr=self.learningRate, weight_decay=self.weightDecay)
def NNTrain(self, n_Examples, train_input, train_target, n_CV, cv_input, cv_target):
self.N_E = n_Examples # Training examples
self.N_CV = n_CV # Validation examples
MSE_cv_linear_batch = torch.empty([self.N_CV])
self.MSE_cv_linear_epoch = torch.empty([self.N_Epochs])
self.MSE_cv_dB_epoch = torch.empty([self.N_Epochs])
MSE_train_linear_batch = torch.empty([self.N_B])
self.MSE_train_linear_epoch = torch.empty([self.N_Epochs])
self.MSE_train_dB_epoch = torch.empty([self.N_Epochs])
##############
### Epochs ###
##############
self.MSE_cv_dB_opt = 1000
self.MSE_cv_idx_opt = 0
for ti in range(0, self.N_Epochs):
#################################
### Validation Sequence Batch ###
#################################
# Cross Validation Mode
self.model.eval()
# all validation samples
for j in range(0, self.N_CV):
# extract the observations y
# TODO: Preprocessing to cv_input
y_cv = cv_input[j, :, :]
self.model.InitSequence(self.ssModel.m1x_0, self.ssModel.T)
x_out_cv = torch.zeros(self.ssModel.m, self.ssModel.T)
for t in range(0, self.ssModel.T):
observation_t = y_cv[:, t]
state_t = self.model(observation_t)
x_out_cv[:, t] = state_t
# Compute Training Loss
# TODO: Preprocessing to cv_target to be of same size as x_out (state)
# TODO: Maybe add x_out_cv*lamda where lambda = [0, 1, 0, 0, 0] =state
cv_target_j = cv_target[j, :, :]
cv_loss = self.loss_fn(x_out_cv, cv_target_j)#.item()
MSE_cv_linear_batch[j] = cv_loss
# Average
self.MSE_cv_linear_epoch[ti] = torch.mean(MSE_cv_linear_batch)
self.MSE_cv_dB_epoch[ti] = 10 * torch.log10(self.MSE_cv_linear_epoch[ti])
if (self.MSE_cv_dB_epoch[ti] < self.MSE_cv_dB_opt):
self.MSE_cv_dB_opt = self.MSE_cv_dB_epoch[ti]
self.MSE_cv_idx_opt = ti
torch.save(self.model, self.modelFileName)
###############################
### Training Sequence Batch ###
###############################
# Training Mode
self.model.train()
# Init Hidden State
self.model.init_hidden()
Batch_Optimizing_LOSS_sum = 0
for j in range(0, self.N_B):
n_e = random.randint(0, self.N_E - 1)
y_training = train_input[n_e, :, :]
self.model.InitSequence(self.ssModel.m1x_0, self.ssModel.T)
x_out_training = torch.empty(self.ssModel.m, self.ssModel.T)
for t in range(0, self.ssModel.T):
x_out_training[:, t] = self.model(y_training[:, t])
# Compute Training Loss
LOSS = self.loss_fn(x_out_training, train_target[n_e, :, :])
MSE_train_linear_batch[j] = LOSS.item()
Batch_Optimizing_LOSS_sum = Batch_Optimizing_LOSS_sum + LOSS
# Average
self.MSE_train_linear_epoch[ti] = torch.mean(MSE_train_linear_batch)
self.MSE_train_dB_epoch[ti] = 10 * torch.log10(self.MSE_train_linear_epoch[ti])
##################
### Optimizing ###
##################
# Before the backward pass, use the optimizer object to zero all of the
# gradients for the variables it will update (which are the learnable
# weights of the model). This is because by default, gradients are
# accumulated in buffers( i.e, not overwritten) whenever .backward()
# is called. Checkout docs of torch.autograd.backward for more details.
self.optimizer.zero_grad()
# Backward pass: compute gradient of the loss with respect to model
# parameters
Batch_Optimizing_LOSS_mean = Batch_Optimizing_LOSS_sum / self.N_B
Batch_Optimizing_LOSS_mean.backward()
# Calling the step function on an Optimizer makes an update to its
# parameters
self.optimizer.step()
########################
### Training Summary ###
########################
print(ti, "MSE Training :", self.MSE_train_dB_epoch[ti], "[dB]", "MSE Validation :",
self.MSE_cv_dB_epoch[ti],
"[dB]")
if (ti > 1):
d_train = self.MSE_train_dB_epoch[ti] - self.MSE_train_dB_epoch[ti - 1]
d_cv = self.MSE_cv_dB_epoch[ti] - self.MSE_cv_dB_epoch[ti - 1]
print("diff MSE Training :", d_train, "[dB]", "diff MSE Validation :", d_cv, "[dB]")
print("Optimal idx:", self.MSE_cv_idx_opt, "Optimal :", self.MSE_cv_dB_opt, "[dB]")
def NNTest(self, n_Test, test_input, test_target):
self.N_T = n_Test
self.MSE_test_linear_arr = torch.empty([self.N_T])
# MSE LOSS Function
loss_fn = nn.MSELoss(reduction='mean')
self.model = torch.load(self.modelFileName)
self.model.eval()
torch.no_grad()
x_out_array = torch.empty(self.N_T, self.ssModel.m, self.ssModel.T_test)
for j in range(0, self.N_T):
y_mdl_tst = test_input[j, :, :]
self.model.InitSequence(self.ssModel.m1x_0, self.ssModel.T_test)
x_out_test = torch.empty(self.ssModel.m, self.ssModel.T_test)
for t in range(0, self.ssModel.T_test):
x_out_test[:, t] = self.model(y_mdl_tst[:, t])
self.MSE_test_linear_arr[j] = loss_fn(x_out_test, test_target[j, :, :]).item()
x_out_array[j, :, :] = x_out_test
# Average
self.MSE_test_linear_avg = torch.mean(self.MSE_test_linear_arr)
self.MSE_test_dB_avg = 10 * torch.log10(self.MSE_test_linear_avg)
# Print MSE Cross Validation
str = self.modelName + "-" + "MSE Test:"
print(str, self.MSE_test_dB_avg, "[dB]")
return [self.MSE_test_linear_arr, self.MSE_test_linear_avg, self.MSE_test_dB_avg, x_out_array]