-
Notifications
You must be signed in to change notification settings - Fork 0
/
pl_vime.py
302 lines (231 loc) · 10.5 KB
/
pl_vime.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
from typing import Dict, Any, Type
import torch
from torch import nn
import pytorch_lightning as pl
import copy
from model import VIME
from misc.scorer import BaseScorer
class PLVIME(pl.LightningModule):
"""The pytorch lightning module of VIME
"""
def __init__(self,
model_hparams: Dict[str, Any],
optim: torch.optim,
optim_hparams: Dict[str, Any],
scheduler: torch.optim.lr_scheduler,
scheduler_hparams: Dict[str, Any],
num_categoricals: int,
num_continuous: int,
u_label,
loss_fn: nn.Module,
scorer: Type[BaseScorer],
random_seed: int = 0,
) -> None:
"""Initialize the pytorch lightining module of VIME
Args:
model_hparams (Dict[str, Any]): The hyperparameters of VIME
optim (torch.optim): The optimizer for training
optim_hparams (Dict[str, Any]): The hyperparameters of the optimizer
scheduler (torch.optim.lr_scheduler): The scheduler for training
scheduler_hparams (Dict[str, Any]): The hyperparameters of the scheduler
num_categoricals (int): The number of categorical features
num_continuous (int): The number of continuous features
u_label (Any): The specifier for unlabeled data.
loss_fn (nn.Module): The loss function of pytorch
scorer (BaseScorer): The scorer to measure the performance
random_seed (int, optional): The random seed. Defaults to 0.
"""
super().__init__()
pl.seed_everything(random_seed)
hparams = copy.deepcopy(model_hparams)
self.alpha1 = hparams["alpha1"]
self.alpha2 = hparams["alpha2"]
del hparams["alpha1"]
del hparams["alpha2"]
self.beta = hparams["beta"]
del hparams["beta"]
self.K = hparams["K"]
self.consistency_len = self.K + 1
del hparams["K"]
self.model = VIME(**hparams)
self.optim = getattr(torch.optim, optim)
self.optim_hparams = optim_hparams
self.scheduler = getattr(torch.optim.lr_scheduler, scheduler)
self.scheduler_hparams = scheduler_hparams
self.num_categoricals = num_categoricals
self.num_continuous = num_continuous
self.u_label = u_label
self.pretraining_mask_loss = nn.BCELoss()
self.pretraining_feature_loss1 = nn.CrossEntropyLoss()
self.pretraining_feature_loss2 = nn.MSELoss()
self.consistency_loss = nn.MSELoss()
self.loss_fn = loss_fn()
self.scorer = scorer
self.do_pretraining()
self.pretraining_step_outputs = []
self.finetunning_step_outputs = []
self.save_hyperparameters()
def configure_optimizers(self):
"""Configure the optimizer
"""
self.optimizer = self.optim(self.parameters(), **self.optim_hparams)
if len(self.scheduler_hparams) == 0:
return [self.optimizer]
self.scheduler = self.scheduler(self.optimizer, **self.scheduler_hparams)
return [self.optimizer], [{'scheduler': self.scheduler, 'interval': 'step'} ]
def do_pretraining(self) -> None:
"""Set the module to pretraining
"""
self.model.do_pretraining()
self.training_step = self.pretraining_step
self.on_validation_start = self.on_pretraining_validation_start
self.validation_step = self.pretraining_step
self.on_validation_epoch_end = self.pretraining_validation_epoch_end
def do_finetunning(self) -> None:
"""Set the module to finetunning
"""
self.model.do_finetunning()
self.training_step = self.finetuning_step
self.on_validation_start = self.on_finetunning_validation_start
self.validation_step = self.finetuning_step
self.on_validation_epoch_end = self.finetuning_validation_epoch_end
def forward(self,
batch:Dict[str, Any]
) -> torch.FloatTensor:
"""Do forward pass for given input
Args:
batch (Dict[str, Any]): The input batch
Returns:
torch.FloatTensor: The output of forward pass
"""
return self.model(batch)
def get_pretraining_loss(self, batch:Dict[str, Any]):
"""Calculate the pretraining loss
Args:
batch (Dict[str, Any]): The input batch
Returns:
torch.FloatTensor: The final loss of pretraining step
"""
mask_output, feature_output = self.model.pretraining_step(batch["input"])
mask_loss = self.pretraining_mask_loss(mask_output, batch["label"][0])
feature_loss1, feature_loss2 = 0, 0
if self.num_categoricals > 0:
feature_loss1 = self.pretraining_feature_loss1(feature_output[:, :self.num_categoricals], batch["label"][1][:, :self.num_categoricals])
if self.num_continuous > 0:
feature_loss2 = self.pretraining_feature_loss2(feature_output[:, self.num_categoricals:], batch["label"][1][:, self.num_categoricals:])
final_loss = mask_loss + self.alpha1 * feature_loss1 + self.alpha2 * feature_loss2
return final_loss
def pretraining_step(self,
batch,
batch_idx: int
) -> Dict[str, Any]:
"""Pretraining step of VIME
Args:
batch (Dict[str, Any]): The input batch
batch_idx (int): For compatibility, do not use
Returns:
Dict[str, Any]: The loss of the pretraining step
"""
loss = self.get_pretraining_loss(batch)
self.pretraining_step_outputs.append({
"loss" : loss
})
return {
"loss" : loss
}
def on_pretraining_validation_start(self):
"""Log the training loss of the pretraining
"""
if len(self.pretraining_step_outputs) > 0:
train_loss = torch.Tensor([out["loss"] for out in self.pretraining_step_outputs]).cpu().mean()
self.log("train_loss", train_loss, prog_bar = True)
self.pretraining_step_outputs = []
return super().on_validation_start()
def pretraining_validation_epoch_end(self) -> None:
"""Log the validation loss of the pretraining
"""
val_loss = torch.Tensor([out["loss"] for out in self.pretraining_step_outputs]).cpu().mean()
self.log("val_loss", val_loss, prog_bar = True)
self.pretraining_step_outputs = []
return super().on_validation_epoch_end()
def get_finetunning_loss(self, batch:Dict[str, Any]):
"""Calculate the finetunning loss
Args:
batch (Dict[str, Any]): The input batch
Returns:
torch.FloatTensor: The final loss of finetunning step
torch.Tensor: The label of the labeled data
torch.Tensor: The predicted label of the labeled data
"""
x = batch["input"]
y = batch["label"]
unsupervised_loss = 0
unlabeled = x[y == self.u_label]
if len(unlabeled) > 0:
u_y_hat = self.model.finetunning_step(unlabeled)
target = u_y_hat[::self.consistency_len]
target = target.repeat(1, self.K).reshape((-1, u_y_hat.shape[-1]))
preds = torch.stack([u_y_hat[i, :] for i in range(len(u_y_hat)) if i % self.consistency_len != 0], dim = 0)
unsupervised_loss += self.consistency_loss(preds, target)
labeled_x = x[y != self.u_label].squeeze()
labeled_y = y[y != self.u_label].squeeze()
y_hat = self.model.finetunning_step(labeled_x).squeeze()
supervised_loss = self.loss_fn(y_hat, labeled_y)
loss = supervised_loss + self.beta * unsupervised_loss
return loss, labeled_y, y_hat
def finetuning_step(self,
batch,
batch_idx: int
) -> Dict[str, Any]:
"""Finetunning step of VIME
Args:
batch (Dict[str, Any]): The input batch
batch_idx (int): For compatibility, do not use
Returns:
Dict[str, Any]: The loss of the finetunning step
"""
loss, y, y_hat = self.get_finetunning_loss(batch)
self.finetunning_step_outputs.append(
{
"loss" : loss,
"y" : y,
"y_hat" : y_hat
}
)
return {
"loss" : loss
}
def on_finetunning_validation_start(self):
"""Log the training loss and the performance of the finetunning
"""
if len(self.finetunning_step_outputs) > 0:
train_loss = torch.Tensor([out["loss"] for out in self.finetunning_step_outputs]).cpu().mean()
y = torch.cat([out["y"] for out in self.finetunning_step_outputs]).cpu().detach().numpy()
y_hat = torch.cat([out["y_hat"] for out in self.finetunning_step_outputs]).cpu().detach().numpy()
train_score = self.scorer(y, y_hat)
self.log("train_loss", train_loss, prog_bar = True)
self.log("train_" + self.scorer.__name__, train_score, prog_bar = True)
self.finetunning_step_outputs = []
return super().on_validation_start()
def finetuning_validation_epoch_end(self) -> None:
"""Log the validation loss and the performance of the finetunning
"""
val_loss = torch.Tensor([out["loss"] for out in self.finetunning_step_outputs]).cpu().mean()
y = torch.cat([out["y"] for out in self.finetunning_step_outputs]).cpu().numpy()
y_hat = torch.cat([out["y_hat"] for out in self.finetunning_step_outputs]).cpu().numpy()
val_score = self.scorer(y, y_hat)
self.log("val_" + self.scorer.__name__, val_score, prog_bar = True)
self.log("val_loss", val_loss, prog_bar = True)
self.finetunning_step_outputs = []
return super().on_validation_epoch_end()
def predict_step(self, batch, batch_idx: int
) -> torch.FloatTensor:
"""The perdict step of VIME
Args:
batch (Dict[str, Any]): The input batch
batch_idx (int): For compatibility, do not use
Returns:
torch.FloatTensor: The predicted output (logit)
"""
y_hat = self.model.finetunning_step(batch["input"])
return y_hat