-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
220 lines (168 loc) · 8.9 KB
/
train.py
File metadata and controls
220 lines (168 loc) · 8.9 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
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from tqdm import tqdm
from model import build_transformer
from dataset import BilingualDataset, causal_mask
from config import get_weights_file_path, get_config
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
from pathlib import Path
from torch.utils.tensorboard import SummaryWriter
import warnings
def greedy_decode(model, src, src_mask, tokenizer_src, tokenizer_target, max_len, device):
sos_idx = tokenizer_target.token_to_id('[SOS]')
eos_idx = tokenizer_target.token_to_id('[EOS]')
# Precompute the encoder output and reuse it for every token we get from the decoder
encoder_output = model.encode(src, src_mask)
decoder_input = torch.empty(1, 1).fill_(sos_idx).type_as(src).to(device)
print(encoder_output.shape)
print(decoder_input.shape)
exit()
while True:
if decoder_input.size(1) == max_len:
break
# Build a mask for the target (decoder_input)
decoder_mask = causal_mask(decoder_input.size(1)).type_as(src_mask).to(device)
# print(decoder_mask.shape)
# exit()
output = model.decode(encoder_output, src_mask, decoder_input, decoder_mask)
prob = model.project(output[:, -1])
_, next_word = torch.max(prob, dim=1)
decoder_input = torch.cat([decoder_input, torch.empty(1, 1).type_as(src).fill_(next_word.item()).to(device)], dim=1)
if next_word == eos_idx:
break
return decoder_input.squeeze(0) # remove batch
def run_validation(model, validation_ds, tokenizer_src, tokenizer_target, max_len, device, print_msg, global_state, writer, num_examples=2):
model.eval()
count = 0
# source_texts = []
# expected = []
# predicted = []
console_width = 80
with torch.no_grad():
for batch in validation_ds:
count += 1
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)
print(encoder_input.shape)
print(encoder_mask.shape)
# exit()
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_target, max_len, device)
source_text = batch['src_text'][0]
target_text = batch['target_text'][0]
model_out_text = tokenizer_target.decode(model_out.detach().cpu().numpy())
print(model_out_text.shape)
# source_texts.append(source_text)
# expected.append(target_text)
# predicted.append(model_out_text)
# function since we are using tqdm, using print from tqdm here.
print_msg('-'*console_width)
print_msg(f'SOURCE: {source_text}')
print_msg(f'TARGET: {target_text}')
print_msg(f'PREDICTED: {model_out_text}')
if count == num_examples:
break
def get_all_sentences(ds, lang):
for item in ds:
yield item['translation'][lang]
def get_or_build_tokenizer(config, ds, lang):
# config['tokenizer_file'] = '../tokenizers/tokenizer_{0}.json'
tokenizer_path = Path(config['tokenizer_file'].format(lang))
if not Path.exists(tokenizer_path):
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):
ds_raw = load_dataset("opus_books", f'{config["lang_src"]}-{config["lang_target"]}', split='train')
# ds_raw = load_dataset(f"{config['datasource']}", f"{config['lang_src']}-{config['lang_tgt']}", split='train')
# print(len(ds_raw))
tokenizer_src = get_or_build_tokenizer(config, ds_raw, config['lang_src'])
tokenizer_target = get_or_build_tokenizer(config, ds_raw, config['lang_target'])
train_ds_size = int(0.9 * len(ds_raw))
val_ds_size = len(ds_raw) - train_ds_size
train_ds_raw, val_ds_raw = torch.utils.data.random_split(ds_raw, [train_ds_size, val_ds_size])
train_ds = BilingualDataset(train_ds_raw, tokenizer_src, tokenizer_target, config['lang_src'], config['lang_target'], config['seq_len'])
val_ds = BilingualDataset(val_ds_raw, tokenizer_src, tokenizer_target, config['lang_src'], config['lang_target'], config['seq_len'])
# print(len(train_ds))
# print(len(val_ds))
max_len_src = 0
max_len_target = 0
for item in ds_raw:
src_ids = tokenizer_src.encode(item['translation'][config['lang_src']]).ids
target_ids = tokenizer_target.encode(item['translation'][config['lang_target']]).ids
max_len_src = max(max_len_src, len(src_ids))
max_len_target = max(max_len_target, len(target_ids))
print(f"Max length of source sentence: {max_len_src}")
print(f"Max length of target sentence: {max_len_target}")
train_dataloader = DataLoader(train_ds, batch_size=config['batch_size'], shuffle=True)
val_dataloader = DataLoader(val_ds, batch_size=1, shuffle=True)
return train_dataloader, val_dataloader, tokenizer_src, tokenizer_target
def get_model(config, vocab_src_len, vocab_target_len):
model = build_transformer(vocab_src_len, vocab_target_len, config['seq_len'], config['seq_len'], config['d_model'])
return model
def train_model(config):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"Using device {device}")
Path(config['model_folder']).mkdir(parents=True, exist_ok=True)
train_dataloader, val_dataloader, tokenizer_src, tokenizer_target = get_ds(config)
model = get_model(config, tokenizer_src.get_vocab_size(), tokenizer_target.get_vocab_size()).to(device)
writer = SummaryWriter(config['experiment_name'])
optimizer = torch.optim.Adam(model.parameters(), config['lr'], eps=1e-9)
initial_epoch = 0
global_step = 0
if config['preload']:
model_filename = get_weights_file_path(config, config['preload'])
print(f"Preloading model {model_filename}")
state = torch.load(model_filename)
initial_epoch = state['epoch']
optimizer.load_state_dict(state['optimizer_state_dict'])
global_step = state['global_step']
loss_fn = nn.CrossEntropyLoss(ignore_index=tokenizer_src.token_to_id('[PAD]'), label_smoothing=0.1).to(device)
for epoch in range(initial_epoch, config['num_epochs']):
batch_iterator = tqdm(train_dataloader, desc=f'Processing epoch {epoch:02d}')
for batch in batch_iterator:
model.train()
encoder_input = batch['encoder_input'].to(device) # (Batch, seq_len)
decoder_input = batch['decoder_input'].to(device) # (Batch, seq_len)
encoder_mask = batch['encoder_mask'].to(device) # (batch, 1, 1, seq_len)
decoder_mask = batch['decoder_mask'].to(device) # (batch, 1, seq_len, seq_len)
# pass through transformer
encoder_output = model.encode(encoder_input, encoder_mask) # (batch, seq_len, d_model)
decoder_output = model.decode(encoder_output, encoder_mask, decoder_input, decoder_mask) # (batch, seq_len, d_model)
projection_output = model.project(decoder_output) # (batch, seq_len, target_vocab_size)
label = batch['label'].to(device) # (batch, seq_len) the translated sentence ground truth
# (batch, seq_len, target_vocab_size) -> (batch * seq_len, target_vocab_size)
loss = loss_fn(projection_output.view(-1, tokenizer_target.get_vocab_size()), label.view(-1))
batch_iterator.set_postfix({"loss": loss.item()})
# log on tensorboard
writer.add_scalar('train_loss', loss.item(), global_step)
writer.flush()
loss.backward()
optimizer.step()
optimizer.zero_grad()
global_step += 1
run_validation(model, val_dataloader, tokenizer_src, tokenizer_target, config['seq_len'], device, lambda msg: batch_iterator.write(msg), global_step, writer)
# save model
model_filename = get_weights_file_path(config, f'{epoch:02d}')
torch.save({
'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'global_step': global_step,
}, model_filename)
if __name__ == '__main__':
warnings.filterwarnings('ignore')
config = get_config()
train_model(config)