-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain.py
64 lines (55 loc) · 1.5 KB
/
train.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
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import Callback, EarlyStopping, ModelCheckpoint
from pytorch_lightning.loggers.wandb import WandbLogger
from dataloader import CustomDataLoader
from lit_model import LitModel
def main():
datamodule = CustomDataLoader(
batch_size=8,
num_workers=0,
pin_memory=False
)
datamodule.setup()
lit_model = LitModel(
lr=1e-3,
weight_decay=1e-4,
milestones=[10],
gamma=0.5,
d_model=128,
dim_feedforward=256,
nhead=4,
dropout=0.3,
num_decoder_layers=3,
max_output_len=200
)
callbacks= []
callbacks.append(
ModelCheckpoint(
save_top_k=1,
save_weights_only=True,
mode="min",
monitor="val/loss",
filename="{epoch}-{val/loss:.2f}-{val/cer:.2f}"
)
)
callbacks.append(
EarlyStopping(
patience=3,
mode="min",
monitor="val/loss",
min_delta=1e-3
)
)
logger = WandbLogger(project="人工智能大作业 - 手写公式识别 - test")
trainer = Trainer(
accelerator='gpu',
max_epochs=30,
num_sanity_val_steps=0,
callbacks=callbacks,
logger=logger
)
trainer.tune(lit_model, datamodule=datamodule)
trainer.fit(lit_model, datamodule=datamodule)
trainer.test(lit_model, datamodule=datamodule)
if __name__ == "__main__":
main()