-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain.py
More file actions
63 lines (51 loc) · 1.96 KB
/
Copy pathtrain.py
File metadata and controls
63 lines (51 loc) · 1.96 KB
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
from __future__ import annotations
from types import SimpleNamespace
from typing import Any
import mlx.optimizers as optim
from mlx import nn
from examples.cifar_datamodule import Cifar10DataModule
from examples.resnet import resnet20
from graphene import Trainer, TrainModule
from graphene.metrics import Accuracy
class CifarTrainModule(TrainModule):
def __init__(self, args: SimpleNamespace) -> None:
super().__init__(args)
self.model = resnet20()
def configure_optimizers(self):
warmup = optim.linear_schedule(0, 1e-1, steps=4 * 190)
cosine = optim.cosine_decay(1e-1, 26 * 190)
lr_schedule = optim.join_schedules([warmup, cosine], [4 * 190])
optimizer = optim.Adam(learning_rate=lr_schedule)
return optimizer
def forward(self, x) -> Any:
return self.model(x)
def setup(self):
self.accs = []
self.validation_accuracy = Accuracy()
self.train_accuracy = Accuracy()
def training_step(self, batch: dict, batch_idx):
x, y = batch["image"], batch["label"]
y_hat = self.forward(x)
loss = nn.losses.cross_entropy(y_hat, y, reduction="mean")
self.train_accuracy(y_hat, y)
self.log("accuracy", self.train_accuracy)
return loss
def validation_step(self, batch: dict, batch_idx):
x, y = batch["image"], batch["label"]
y_hat = self.forward(x)
loss = nn.losses.cross_entropy(y_hat, y, reduction="mean")
self.validation_accuracy(y_hat, y)
self.log("accuracy", self.validation_accuracy)
return loss
if __name__ == "__main__":
datamodule = Cifar10DataModule(args=SimpleNamespace(batch_size=256))
trainmodule = CifarTrainModule(args=None)
trainer = Trainer(
train_module=trainmodule,
data_module=datamodule,
max_epochs=30,
run_validation_every_n_epochs=1,
# limit_validation_batches=10,
# limit_train_batches=10,/
)
trainer.fit()