-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformer_lm.py
151 lines (116 loc) · 4.41 KB
/
transformer_lm.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import time
import wandb
from data_handler import build_data, get_batch, get_voc_size
from model import Transformer_LM
N = 2000
train_split = 0.9
eval_interval = 500
eval_iter = 50
device = "cuda" if torch.cuda.is_available() else "cpu"
def objective(config, wandb_log):
# train un model avec les HP config
lr = 10**config['log_learning_rate']
batch_size = config['batch_size']
n_layers = config['n_layers']
d_model = config['d_model']
n_heads = config['n_heads']
act = config['act']
optimizer_hp = config['optimizer']
build_data('villes.txt', train_split=train_split)
model = Transformer_LM(n_layers, d_model, n_heads, get_voc_size(), 50, act)
model.to(device)
if optimizer_hp == 'SGD':
optimizer = torch.optim.SGD(model.parameters(), lr=lr)
elif optimizer_hp == 'SGD_M':
optimizer = torch.optim.SGD(model.parameters(), lr=lr, momentum=0.9)
elif optimizer_hp == 'Adam':
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
else:
optimizer = torch.optim.AdamW(model.parameters(), lr=lr, weight_decay=0.01, betas=(0.9, 0.99))
start_time = time.time()
if wandb_log:
wandb.watch(model, log="all")
for update_num in range(N):
Xb, Yb = get_batch('train', batch_size)
logits, loss = model(Xb, Yb)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# eval : track loss (train & val), update_to_data
if wandb_log and (update_num % eval_interval == 0):
to_log = {}
with torch.no_grad():
model.eval()
for split in ['train', 'val']:
loss_mean = 0
for i in range(eval_iter):
Xb, Yb = get_batch(split, batch_size)
logits, loss = model(Xb, Yb)
loss_mean += loss.item()
loss_mean /= eval_iter
to_log["loss_" + split] = loss_mean
model.train()
scalars_dict = {}
for name, p in model.named_parameters():
scalars_dict[name] = (lr*p.grad.std() / p.data.std()).log10().item()
wandb.log(to_log | {"update_to_data": scalars_dict}, step=update_num)
end_time = time.time()
num_examples_processed = N * batch_size
print("training throughput = {} examples/s".format(str(num_examples_processed/(end_time-start_time))))
with torch.no_grad():
val_loss_mean = 0
for _ in range(eval_iter):
Xb, Yb = get_batch('val', batch_size)
logits, loss = model(Xb, Yb)
val_loss_mean += loss.item()
val_loss_mean /= eval_iter
if wandb_log:
wandb.log({"training_throughput": num_examples_processed/(end_time-start_time)})
wandb.log({"params_num": sum([p.numel() for p in model.parameters()])})
return val_loss_mean
def run():
config = {
"log_learning_rate": np.log(0.03),
"batch_size": 1024,
"n_layers": 2,
"d_model": 64,
"n_heads": 2,
"act": 'selu',
"optimizer": "AdamW",
"architecture": "Transformer"
}
wandb.init(project="blablateurbinaire", config=config)
_ = objective(config, wandb_log=True)
wandb.finish()
def run_one_sweep():
wandb.init(project='blablateurbinaire')
val_loss = objective(wandb.config, wandb_log=False)
wandb.log({'final_val_loss': val_loss})
def sweep():
sweep_configuration = {
'method': 'random',
'metric':
{
'goal': 'minimize',
'name': 'final_val_loss'
},
'parameters':
{
'log_learning_rate': {'min': np.log10(0.0001), 'max': np.log10(0.1)},
'batch_size': {'values': [256, 512, 1024]},
'n_layers' : {'values': [1, 2, 4]},
'd_model' : {'values': [32, 64, 128]},
'n_heads' : {'values': [1, 2, 4, 8]},
'act' : {'values': ['selu', 'gelu']},
'optimizer': {'values': ['AdamW']},
'architecture': {'values': ['Transformer']}
}
}
sweep_id = wandb.sweep(sweep=sweep_configuration, project='blablateurbinaire')
wandb.agent(sweep_id, function=run_one_sweep)
#run()
sweep()