-
Notifications
You must be signed in to change notification settings - Fork 3
/
base_module.py
255 lines (238 loc) · 8.54 KB
/
base_module.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
from abc import ABCMeta, abstractmethod
from typing import Dict, Any, Optional
import torch
import torch.nn as nn
from torch.nn import functional as F
from pytorch_lightning import LightningModule
from torchmetrics.classification import Accuracy
from torchmetrics.regression import MeanAbsoluteError, R2Score
from transformers import get_constant_schedule_with_warmup
from crystal_gnn.models.module_utils import Normalizer
class BaseModule(LightningModule, metaclass=ABCMeta):
def __init__(self, _config: Dict[str, Any]):
super().__init__()
self.save_hyperparameters(_config)
self.num_classes = _config["num_classes"]
self.readout_dim = self.num_classes if self.num_classes != 2 else 1
# log
if self.num_classes > 1:
self.accuracy = Accuracy(task="multiclass", num_classes=self.num_classes)
else:
self.mae = MeanAbsoluteError()
self.r2 = R2Score()
# optimizer
self.optimizer = _config["optimizer"]
self.lr = _config["lr"]
self.weight_decay = _config["weight_decay"]
self.scheduler = _config["scheduler"]
# normalizer (only when num_classes == 1)
if self.num_classes == 1:
print(f"set normalizer with mean: {_config['mean']}, std: {_config['std']}")
self.normalizer = Normalizer(mean=_config["mean"], std=_config["std"])
@abstractmethod
def forward(self, batch: Dict[str, Any]) -> torch.Tensor:
pass
def training_step(
self,
batch: Dict[str, Any],
batch_idx: int, # pylint: disable=unused-argument
) -> torch.Tensor:
logits = self.forward(batch)
target = batch["target"]
if self.num_classes == 1:
target = self.normalizer.encode(target)
loss = self._calculate_loss(logits, target)
if self.num_classes == 1:
logits = self.normalizer.decode(logits)
target = self.normalizer.decode(target)
self._log_metrics(
logits,
target,
"train",
loss=loss,
on_step=True,
on_epoch=True,
)
return loss
def validation_step(
self,
batch: Dict[str, Any],
batch_idx: int, # pylint: disable=unused-argument
) -> torch.Tensor:
logits = self.forward(batch)
target = batch["target"]
if self.num_classes == 1:
target = self.normalizer.encode(target)
loss = self._calculate_loss(logits, target)
if self.num_classes == 1:
logits = self.normalizer.decode(logits)
target = self.normalizer.decode(target)
self._log_metrics(
logits,
target,
"val",
loss=loss,
on_step=False,
on_epoch=True,
)
return loss
def test_step(
self,
batch: Dict[str, Any],
batch_idx, # pylint: disable=unused-argument
) -> torch.Tensor:
logits = self.forward(batch)
target = batch["target"]
if self.num_classes == 1:
target = self.normalizer.encode(target)
loss = self._calculate_loss(logits, target)
if self.num_classes == 1:
logits = self.normalizer.decode(logits)
target = self.normalizer.decode(target)
self._log_metrics(
logits,
target,
"test",
loss=loss,
on_step=False,
on_epoch=True,
)
return loss
def predict_step(
self,
batch: Dict[str, Any],
batch_idx, # pylint: disable=unused-argument
) -> torch.Tensor:
logits = self.forward(batch)
if self.num_classes == 1:
logits = self.normalizer.decode(logits)
elif self.num_classes == 2:
logits = torch.sigmoid(logits)
logits = logits > 0.5
else:
logits = logits.argmax(dim=1)
return logits.squeeze()
def configure_optimizers(self) -> Dict[str, Any]:
return self._set_configure_optimizers()
def _init_weights(self, module: torch.nn.Module) -> None:
if isinstance(module, nn.Linear):
module.weight.data.normal_(mean=0.0, std=0.02)
if module.bias is not None:
module.bias.data.zero_()
elif isinstance(module, nn.Embedding):
module.weight.data.normal_(mean=0.0, std=0.02)
if module.padding_idx is not None:
module.weight.data[module.padding_idx].zero_()
elif isinstance(module, nn.LayerNorm):
module.bias.data.zero_()
module.weight.data.fill_(1.0)
def _calculate_loss(
self, logits: torch.Tensor, target: torch.Tensor
) -> torch.Tensor:
if self.num_classes == 1:
loss = F.mse_loss(logits.squeeze(), target)
elif self.num_classes == 2:
loss = F.binary_cross_entropy_with_logits(logits.squeeze(), target)
# the bce includes a sigmoid fuction
else:
loss = F.cross_entropy(logits, target)
return loss
def _log_metrics(
self,
logits: torch.Tensor,
target: torch.Tensor,
split: str,
loss: Optional[torch.Tensor] = None,
on_step: bool = False,
on_epoch: bool = False,
) -> None:
self.log(
f"{split}/loss",
loss,
on_step=on_step,
on_epoch=on_epoch,
sync_dist=True,
batch_size=self.hparams.batch_size,
)
if self.num_classes == 1:
self.log(
f"{split}/mae",
self.mae(logits.squeeze(), target),
on_step=on_step,
on_epoch=on_epoch,
sync_dist=True,
batch_size=self.hparams.batch_size,
)
self.log(
f"{split}/r2",
self.r2(logits.squeeze(), target),
on_step=on_step,
on_epoch=on_epoch,
sync_dist=True,
batch_size=self.hparams.batch_size,
)
elif self.num_classes == 2:
logits = F.sigmoid(logits)
logits = logits > 0.5
self.log(
f"{split}/accuracy",
self.accuracy(logits.squeeze(), target),
on_step=on_step,
on_epoch=on_epoch,
sync_dist=True,
batch_size=self.hparams.batch_size,
)
else:
logits = logits.argmax(dim=1)
self.log(
f"{split}/accuracy",
self.accuracy(logits.squeeze(), target),
on_step=on_step,
on_epoch=on_epoch,
sync_dist=True,
batch_size=self.hparams.batch_size,
)
def _set_configure_optimizers(self):
lr = self.lr
weight_decay = self.weight_decay
if self.optimizer == "adam":
optimizer = torch.optim.Adam(
self.parameters(), lr=lr, weight_decay=weight_decay
)
elif self.optimizer == "sgd":
optimizer = torch.optim.SGD(
self.parameters(), lr=lr, weight_decay=weight_decay
)
elif self.optimizer == "adamw":
optimizer = torch.optim.AdamW(
self.parameters(), lr=lr, weight_decay=weight_decay
)
else:
raise ValueError(f"Invalid optimizer: {self.optimizer}")
# get max_steps
if self.trainer.max_steps == -1:
max_steps = self.trainer.estimated_stepping_batches
else:
max_steps = self.trainer.max_steps
# set scheduler
if self.scheduler == "constant":
scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lambda _: 1.0)
elif self.scheduler == "cosine":
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=10)
elif self.scheduler == "reduce_on_plateau":
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer, mode="min"
)
elif self.scheduler == "constant_with_warmup":
warmup_step = int(max_steps * 0.05)
scheduler = get_constant_schedule_with_warmup(
optimizer, num_warmup_steps=warmup_step
)
else:
raise ValueError(f"Invalid scheduler: {self.scheduler}")
lr_scheduler = {
"scheduler": scheduler,
"name": "learning rate",
"monitor": "val/loss",
}
return ([optimizer], [lr_scheduler])