-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlp_lm.py
151 lines (115 loc) · 4.55 KB
/
mlp_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_bengio import build_data, get_batch, get_voc_size
from model import BengioLM
N = 20000
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
# : config.keys = ['context_len', 'log_learning_rate', 'batch_size', 'embed_dim', 'hidden_dim', 'optimizer']
context_len = config['context_len']
lr = 10**config['log_learning_rate']
batch_size = config['batch_size']
embed_dim = config['embed_dim']
hidden_dim = config['hidden_dim']
optimizer_hp = config['optimizer']
build_data('villes.txt', context_len=config['context_len'], train_split=train_split)
model = BengioLM(get_voc_size(), context_len, embed_dim, hidden_dim)
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(batch_size, 'train', device)
logits = model(Xb)
loss = F.cross_entropy(logits, 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(batch_size, split, device)
logits = model(Xb)
loss_mean += F.cross_entropy(logits, Yb).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(batch_size, 'val', device)
logits = model(Xb)
val_loss_mean += F.cross_entropy(logits, Yb).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,
"embed_dim": 16,
"hidden_dim": 100,
"context_len": 3,
"optimizer": "Adam",
"architecture": "Bengio"
}
wandb.init(project="communes_lm", config=config)
_ = objective(config, wandb_log=True)
wandb.finish()
def run_one_sweep():
wandb.init(project='communes_lm')
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.3)},
'batch_size': {'values': [1024]},
'embed_dim': {'values': [8, 16, 32, 64]},
'hidden_dim': {'values': [50, 100, 300, 500]},
'context_len': {'values': [3, 5, 8]},
'optimizer': {'values': ['SGD', 'SGD_M', 'Adam', 'AdamW']},
'architecture': {'values': ['Bengio']}
}
}
sweep_id = wandb.sweep(sweep=sweep_configuration, project='communes_lm')
wandb.agent(sweep_id, function=run_one_sweep)
#run()
sweep()