-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
58 lines (48 loc) · 1.98 KB
/
tasks.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
from collections.abc import Callable, Iterable
import torch as th
from torch import nn
import torch.nn.functional as F
from torch import optim
import pytorch_lightning as pl
from torchmetrics import Metric
from torchmetrics.classification import MulticlassAccuracy
class ClassificationTask(pl.LightningModule):
def __init__(
self,
model: nn.Module,
lr: float,
optimizer: Callable[[Iterable[th.Tensor], float], optim.Optimizer],
num_classes: int,
input_dim: tuple[int, ...]
) -> None:
super().__init__()
self.model = model
self.lr = lr
self.opt = optimizer
self._init_metrics(num_classes)
self.example_input_array = th.zeros(1, *input_dim)
def _init_metrics(self, num_classes: int) -> None:
self.train_acc = MulticlassAccuracy(num_classes=num_classes)
self.val_acc = self.train_acc.clone()
self.test_acc = self.train_acc.clone()
def forward(self, data: th.Tensor) -> th.Tensor:
return self.model(data)
def training_step(self, batch: tuple[th.Tensor, th.Tensor], batch_idx: int) -> th.Tensor:
output = self._helper(batch, "train")
_, target = batch
loss = F.cross_entropy(output, target)
self.log("loss", loss, prog_bar=True)
return loss
def validation_step(self, batch: tuple[th.Tensor, th.Tensor], batch_idx: int) -> None:
self._helper(batch, "val")
def test_step(self, batch: tuple[th.Tensor, th.Tensor], batch_idx: int) -> None:
self._helper(batch, "test")
def _helper(self, batch: tuple[th.Tensor, th.Tensor], stage: str) -> th.Tensor:
data, target = batch
output = self(data)
acc: Metric = getattr(self, f"{stage}_acc")
acc(output, target)
self.log(f"{stage}/{acc.__class__.__name__}", acc, prog_bar=True)
return output
def configure_optimizers(self) -> optim.Optimizer:
return self.opt(self.parameters(), lr=self.lr)