-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlightning_utils.py
55 lines (46 loc) · 1.75 KB
/
lightning_utils.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import pytorch_lightning as pl
from pytorch_lightning.callbacks import ModelCheckpoint
# The training_step and validation_step are not implemented here yet
# I don't know how to do inheritance in Poython3
# For now, let's forget what is the best practice
class ParentFinetuner(pl.LightningModule):
def __init__(self, model, inp_scale=False, bounds=None, max_epochs=1000):
super(ParentFinetuner, self).__init__()
self.model = model
self.max_epochs = max_epochs
self.inp_scale = inp_scale
if bounds is not None:
self.lb, self.ub = bounds
def neural_net_scale(self, inp):
return 2*(inp-self.lb)/(self.ub-self.lb)-1
class Finetuner(pl.LightningModule):
def __init__(self, model, optimizer, inp_scale=False, bounds=None, max_epochs=1000):
super(Finetuner, self).__init__()
self.model = model
self.optimizer = optimizer
self.max_epochs = max_epochs
self.inp_scale = inp_scale
def forward(self, x):
H = x
if self.inp_scale: H = self.neural_net_scale(x)
return self.model(H)
def neural_net_scale(self, inp):
return 2*(inp-self.lb)/(self.ub-self.lb)-1
def configure_optimizers(self):
return self.optimizer
def training_step(self, train_batch, batch_idx):
x, y = train_batch
x = x.view(x.size(0), -1)
y_hat = self.forward(x)
loss = F.mse_loss(y_hat, y)
self.log('train_loss', loss)
return loss
def validation_step(self, val_batch, batch_idx):
x, y = val_batch
x = x.view(x.size(0), -1)
y_hat = self.forward(x)
loss = F.mse_loss(y_hat, y)
self.log('val_loss', loss)