-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtrain_wb.py
More file actions
381 lines (307 loc) · 11.7 KB
/
Copy pathtrain_wb.py
File metadata and controls
381 lines (307 loc) · 11.7 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
from model import build_transformer
from dataset import BilingualDataset, causal_mask
from config import get_config, get_weights_file_path
import torchtext.datasets as datasets
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader, random_split
from torch.optim.lr_scheduler import LambdaLR
import warnings
from tqdm import tqdm
import os
from pathlib import Path
# Huggingface datasets and tokenizers
from datasets import load_dataset
from tokenizers import Tokenizer
from tokenizers.models import WordLevel
from tokenizers.trainers import WordLevelTrainer
from tokenizers.pre_tokenizers import Whitespace
import wandb
import torchmetrics
def greedy_decode(
model, source, source_mask, tokenizer_src, tokenizer_tgt, max_len, device
):
"""
Function to decode the model's output using a greedy approach.
Args:
- model: The Transformer model.
- source: The input sequence from the source language.
- source_mask: The attention mask for the source sequence.
- tokenizer_src: Tokenizer for the source language.
- tokenizer_tgt: Tokenizer for the target language.
- max_len: The maximum length for the target sequence.
- device: The device to run the model on.
Returns:
- The decoded output sequence.
"""
sos_idx = tokenizer_tgt.token_to_id("[SOS]")
eos_idx = tokenizer_tgt.token_to_id("[EOS]")
# Compute the encoder output once and reuse it for each decoding step
encoder_output = model.encode(source, source_mask)
# Start the decoder input with the SOS token
decoder_input = (
torch.empty(1, 1).fill_(sos_idx).type_as(source).to(device)
)
while True:
if decoder_input.size(1) == max_len:
break
# Create mask for the target sequence
decoder_mask = causal_mask(decoder_input.size(1)).type_as(source_mask).to(
device
)
# Obtain output from the decoder
out = model.decode(encoder_output, source_mask, decoder_input, decoder_mask)
# Determine the next token
prob = model.project(out[:, -1])
_, next_word = torch.max(prob, dim=1)
decoder_input = torch.cat(
[decoder_input, torch.empty(1, 1).type_as(source).fill_(next_word.item()).to(device)], dim=1
)
if next_word == eos_idx:
break
return decoder_input.squeeze(0)
def run_validation(
model, validation_ds, tokenizer_src, tokenizer_tgt, max_len, device, print_msg, global_step, num_examples=2
):
"""
Perform validation and log metrics using wandb.
Args:
- model: The Transformer model.
- validation_ds: Validation dataset.
- tokenizer_src: Tokenizer for the source language.
- tokenizer_tgt: Tokenizer for the target language.
- max_len: The maximum length for the target sequence.
- device: The device to run the model on.
- print_msg: Function to print messages.
- global_step: Global step count.
- num_examples: Number of examples to validate.
"""
model.eval()
count = 0
source_texts = []
expected = []
predicted = []
try:
# Attempt to obtain the console window width
with os.popen("stty size", "r") as console:
_, console_width = console.read().split()
console_width = int(console_width)
except:
# Default console width if unable to obtain
console_width = 80
with torch.no_grad():
for batch in validation_ds:
count += 1
encoder_input = batch["encoder_input"].to(device) # (b, seq_len)
encoder_mask = batch["encoder_mask"].to(device) # (b, 1, 1, seq_len)
# Ensure batch size is 1 for validation
assert encoder_input.size(0) == 1, "Batch size must be 1 for validation"
model_out = greedy_decode(
model, encoder_input, encoder_mask, tokenizer_src, tokenizer_tgt, max_len, device
)
source_text = batch["src_text"][0]
target_text = batch["tgt_text"][0]
model_out_text = tokenizer_tgt.decode(model_out.detach().cpu().numpy())
source_texts.append(source_text)
expected.append(target_text)
predicted.append(model_out_text)
# Display source, target, and predicted text
print_msg("-" * console_width)
print_msg(f"{f'SOURCE: ':>12}{source_text}")
print_msg(f"{f'TARGET: ':>12}{target_text}")
print_msg(f"{f'PREDICTED: ':>12}{model_out_text}")
if count == num_examples:
print_msg("-" * console_width)
break
# Evaluate metrics for model performance
# Calculate the character error rate
metric = torchmetrics.CharErrorRate()
cer = metric(predicted, expected)
wandb.log({"validation/cer": cer, "global_step": global_step})
# Calculate the word error rate
metric = torchmetrics.WordErrorRate()
wer = metric(predicted, expected)
wandb.log({"validation/wer": wer, "global_step": global_step})
# Calculate the BLEU metric
metric = torchmetrics.BLEUScore()
bleu = metric(predicted, expected)
wandb.log({"validation/BLEU": bleu, "global_step": global_step})
def get_all_sentences(ds, lang):
"""
Generator to yield sentences from a dataset.
Args:
- ds: The dataset.
- lang: Language to extract.
Yields:
- Sentence in the specified language.
"""
for item in ds:
yield item["translation"][lang]
def get_or_build_tokenizer(config, ds, lang):
"""
Create or load a tokenizer for a given language.
Args:
- config: Configuration dictionary.
- ds: The dataset to build the tokenizer from.
- lang: The language for which the tokenizer is built.
Returns:
- tokenizer: The loaded or created tokenizer.
"""
tokenizer_path = Path(config["tokenizer_file"].format(lang))
if not Path.exists(tokenizer_path):
# Build a new tokenizer if one does not exist
tokenizer = Tokenizer(WordLevel(unk_token="[UNK]"))
tokenizer.pre_tokenizer = Whitespace()
trainer = WordLevelTrainer(
special_tokens=["[UNK]", "[PAD]", "[SOS]", "[EOS]"], min_frequency=2
)
tokenizer.train_from_iterator(get_all_sentences(ds, lang), trainer=trainer)
tokenizer.save(str(tokenizer_path))
else:
tokenizer = Tokenizer.from_file(str(tokenizer_path))
return tokenizer
def get_ds(config):
"""
Prepare datasets for training and validation.
Args:
- config: Configuration dictionary.
Returns:
- train_dataloader: DataLoader for the training dataset.
- val_dataloader: DataLoader for the validation dataset.
- tokenizer_src: Tokenizer for the source language.
- tokenizer_tgt: Tokenizer for the target language.
"""
# Load dataset and split into train and validation sets
ds_raw = load_dataset(
"opus_books", f"{config['lang_src']}-{config['lang_tgt']}", split="train"
)
# Build tokenizers for source and target languages
tokenizer_src = get_or_build_tokenizer(config, ds_raw, config["lang_src"])
tokenizer_tgt = get_or_build_tokenizer(config, ds_raw, config["lang_tgt"])
# Split dataset: 90% for training, 10% for validation
train_ds_size = int(0.9 * len(ds_raw))
val_ds_size = len(ds_raw) - train_ds_size
train_ds_raw, val_ds_raw = random_split(ds_raw, [train_ds_size, val_ds_size])
train_ds = BilingualDataset(
train_ds_raw,
tokenizer_src,
tokenizer_tgt,
config["lang_src"],
config["lang_tgt"],
config["seq_len"],
)
val_ds = BilingualDataset(
val_ds_raw,
tokenizer_src,
tokenizer_tgt,
config["lang_src"],
config["lang_tgt"],
config["seq_len"],
)
# Create DataLoaders for training and validation datasets
train_dataloader = DataLoader(
train_ds,
batch_size=config["batch_size"],
shuffle=True,
drop_last=True,
)
val_dataloader = DataLoader(
val_ds,
batch_size=1,
)
return train_dataloader, val_dataloader, tokenizer_src, tokenizer_tgt
def train(config, print_msg):
"""
Train the Transformer model.
Args:
- config: Configuration dictionary.
- print_msg: Function to print messages.
"""
train_dataloader, val_dataloader, tokenizer_src, tokenizer_tgt = get_ds(config)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Initialize the Transformer model
model = build_transformer(
src_vocab_size=tokenizer_src.get_vocab_size(),
tgt_vocab_size=tokenizer_tgt.get_vocab_size(),
src_seq_len=config["seq_len"],
tgt_seq_len=config["seq_len"],
d_model=config["d_model"],
N=config.get("num_layers", 6),
h=config.get("num_heads", 8),
dropout=config.get("dropout_p", 0.1),
).to(device)
# Load model weights if the weights file exists
model_weights_path = Path(get_weights_file_path(config))
if model_weights_path.is_file():
model.load_state_dict(torch.load(model_weights_path))
print_msg("Model weights loaded.")
else:
print_msg("No pre-trained weights found.")
# Define loss criterion
loss_fn = nn.CrossEntropyLoss(ignore_index=tokenizer_src.token_to_id("[PAD]"))
# Define optimizer and learning rate scheduler
optimizer = torch.optim.Adam(
model.parameters(), lr=config["lr"], eps=1e-9
)
lr_scheduler = LambdaLR(
optimizer=optimizer,
lr_lambda=lambda step: (
config["d_model"] ** (-0.5) * min(
(step + 1) ** (-0.5), (step + 1) * config["warmup_steps"] ** (-1.5)
)
),
)
# Initialize wandb for logging
wandb.init(project="translation-transformers", config=config)
wandb.watch(model, log="all")
# Training loop
model.train()
global_step = 0
for epoch in range(config["num_epochs"]):
print_msg(f"Epoch {epoch + 1} of {config['num_epochs']}")
# Track average loss for the epoch
losses = []
# Iterate through batches
for batch in tqdm(train_dataloader):
encoder_input = batch["encoder_input"].to(device)
encoder_mask = batch["encoder_mask"].to(device)
decoder_input = batch["decoder_input"].to(device)
decoder_mask = batch["decoder_mask"].to(device)
label = batch["label"].to(device)
# Get predictions from the model
prediction = model(
encoder_input, encoder_mask, decoder_input, decoder_mask
)
prediction = prediction.permute(0, 2, 1)
# Compute loss
loss = loss_fn(prediction, label)
losses.append(loss.item())
# Backpropagation
optimizer.zero_grad()
loss.backward()
optimizer.step()
lr_scheduler.step()
# Log loss and global step to wandb
wandb.log({"train/loss": loss.item(), "global_step": global_step})
global_step += 1
print_msg(f"Epoch {epoch + 1} loss: {sum(losses) / len(losses)}")
# Run validation
run_validation(
model,
val_dataloader,
tokenizer_src,
tokenizer_tgt,
config["seq_len"],
device,
print_msg,
global_step,
)
# Save model weights after each epoch
torch.save(model.state_dict(), get_weights_file_path(config))
if __name__ == "__main__":
config = get_config()
Path(config["model_dir"]).mkdir(parents=True, exist_ok=True)
warnings.warn = lambda *args, **kwargs: None # Suppress warnings
def print_msg(msg):
print(msg)
train(config, print_msg)