-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
133 lines (102 loc) · 3.02 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
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
import os
import glob
import json
import torch
import random
import numpy as np
from data import *
from BERT import *
from utils.remi import *
from utils.utils import *
from utils.vocab import *
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.callbacks import LearningRateMonitor
### load config file
with open("./config.json", "r") as f:
config = json.load(f)
### fix random seed
random_seed = config["random_seed"]
# it may slow computing performance
# torch.backends.cudnn.deterministic = True
# torch.backends.cudnn.benchmark = False
random.seed(random_seed)
np.random.seed(random_seed)
torch.manual_seed(random_seed)
torch.cuda.manual_seed(random_seed)
torch.cuda.manual_seed_all(random_seed)
#### Pytorch-lightning 1.9.4 for interal precision
torch.set_float32_matmul_precision("high")
#### initialize model with GPU
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
### load file_list
folder_path = "./data/lmd_full_remi/"
folder_list = glob.glob(os.path.join(folder_path, "*"))
train_folder, val_folder, test_folder = dataset_split(folder_list)
train_files = folder_to_file(train_folder)
val_files = folder_to_file(val_folder)
test_files = folder_to_file(test_folder)
random.shuffle(train_files)
print(
f"train_files : {len(train_files)}, val_files : {len(val_files)}, test_files : {len(test_files)}"
)
### load dataloader
train_module = DataModule(
train_files,
batch_size=config["batch_size"],
num_workers=config["num_workers"],
masking=config["masking"],
replace=config["replace"],
phase="train",
)
val_module = DataModule(
val_files,
batch_size=config["batch_size"],
num_workers=config["num_workers"],
phase="val",
)
test_module = DataModule(
test_files,
batch_size=config["batch_size"],
num_workers=config["num_workers"],
phase="test",
)
train_set = train_module.return_dataloader()
val_set = val_module.return_dataloader()
test_set = test_module.return_dataloader()
### define model
model = BERT_Lightning(
dim=config["dim"],
depth=config["depth"],
heads=config["heads"],
dim_head=int(config["dim"] / config["heads"]),
mlp_dim=int(4 * config["dim"]),
max_len=config["max_len"],
rate=config["rate"],
loss_weights=config["loss_weights"],
lr=config["lr"],
warm_up=config["warm_up"],
temp=config["temp"],
mode=config["mode"],
).to(device)
### callback functions
model_name = [key + "_" + str(value) for key, value in config.items()]
model_name = "-".join(param for param in model_name)
model_name = "BERT-" + model_name + "-{epoch}-{val_loss:.4f}"
lr_monitor = LearningRateMonitor(logging_interval="step")
checkpoint = ModelCheckpoint(
filename=model_name,
dirpath="./model/",
monitor="val_loss",
mode="min",
)
### train model
trainer = pl.Trainer(
num_nodes=1,
precision=16,
max_epochs=config["epochs"],
accelerator="gpu",
devices=config["gpus"],
callbacks=[lr_monitor, checkpoint],
)
trainer.fit(model, train_set, val_set)