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
| import os import math import torch from torch import nn, Tensor import torch.nn.functional as F from torch.nn import TransformerEncoder, TransformerEncoderLayer from torchtext.data.utils import get_tokenizer from torchtext.vocab import build_vocab_from_iterator from torch.utils.data import dataset import time from tempfile import TemporaryDirectory from typing import Tuple
class TransformerModel(nn.Module): def __init__(self, ntoken: int, d_model: int, nhead: int, d_hid: int, nlayers: int, dropout: float = 0.5): super().__init__() self.model_type = 'Transformer' self.pos_encoder = PositionalEncoding(d_model, dropout) encoder_layers = TransformerEncoderLayer(d_model, nhead, d_hid, dropout) self.transformer_encoder = TransformerEncoder(encoder_layers, nlayers) self.encoder = nn.Embedding(ntoken, d_model) self.d_model = d_model self.decoder = nn.Linear(d_model, ntoken)
self.init_weights()
def init_weights(self) -> None: initrange = 0.1 self.encoder.weight.data.uniform_(-initrange, initrange) self.decoder.bias.data.zero_() self.decoder.weight.data.uniform_(-initrange, initrange)
def forward(self, src: Tensor, src_mask: Tensor) -> Tensor: src = self.encoder(src) * math.sqrt(self.d_model) src = self.pos_encoder(src) output = self.transformer_encoder(src, src_mask) output = self.decoder(output) return output
class PositionalEncoding(nn.Module): def __init__(self, d_model: int, dropout: float = 0.1, max_len: int = 5000): super().__init__() self.dropout = nn.Dropout(p=dropout)
position = torch.arange(max_len).unsqueeze(1) div_term = torch.exp(torch.arange(0, d_model, 2) * (-math.log(10000.0) / d_model)) pe = torch.zeros(max_len, 1, d_model) pe[:, 0, 0::2] = torch.sin(position * div_term) pe[:, 0, 1::2] = torch.cos(position * div_term) self.register_buffer('pe', pe)
def forward(self, x: Tensor) -> Tensor: x = x + self.pe[:x.size(0)] return self.dropout(x)
def generate_square_subsequent_mask(sz: int) -> Tensor: """Generates an upper-triangular matrix of -inf, with zeros on diag.""" mask = torch.triu(torch.ones(sz, sz) * float('-inf'), diagonal=1) return mask
def load_and_preprocess_data(local_data_path: str): if not os.path.exists(local_data_path): raise FileNotFoundError(f"Dataset not found at {local_data_path}")
def yield_tokens(file_path): with open(file_path, encoding="utf-8") as f: for line in f: yield tokenizer(line.strip())
tokenizer = get_tokenizer('basic_english') vocab = build_vocab_from_iterator(yield_tokens(os.path.join(local_data_path, 'wiki.train.tokens')), specials=['<unk>']) vocab.set_default_index(vocab['<unk>'])
def data_process(file_path): with open(file_path, encoding="utf-8") as f: return torch.cat([torch.tensor(vocab(tokenizer(line.strip())), dtype=torch.long) for line in f])
train_data = data_process(os.path.join(local_data_path, 'wiki.train.tokens')) val_data = data_process(os.path.join(local_data_path, 'wiki.valid.tokens')) test_data = data_process(os.path.join(local_data_path, 'wiki.test.tokens'))
return train_data, val_data, test_data, vocab
def batchify(data: Tensor, bsz: int) -> Tensor: seq_len = data.size(0) // bsz data = data[:seq_len * bsz] data = data.view(bsz, seq_len).t().contiguous() return data.to(device)
local_data_path = "wikitext-2" train_data, val_data, test_data, vocab = load_and_preprocess_data(local_data_path)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') batch_size = 20 eval_batch_size = 10 train_data = batchify(train_data, batch_size) val_data = batchify(val_data, eval_batch_size) test_data = batchify(test_data, eval_batch_size)
ntokens = len(vocab) emsize = 200 d_hid = 200 nlayers = 2 nhead = 2 dropout = 0.2 model = TransformerModel(ntokens, emsize, nhead, d_hid, nlayers, dropout).to(device)
criterion = nn.CrossEntropyLoss() lr = 5.0 optimizer = torch.optim.SGD(model.parameters(), lr=lr) scheduler = torch.optim.lr_scheduler.StepLR(optimizer, 1.0, gamma=0.95)
bptt = 35
def train(model: nn.Module): model.train() total_loss = 0. log_interval = 200 start_time = time.time() src_mask = generate_square_subsequent_mask(bptt).to(device)
num_batches = len(train_data) // bptt for batch, i in enumerate(range(0, train_data.size(0) - 1, bptt)): data, targets = get_batch(train_data, i) seq_len = data.size(0) if seq_len != bptt: src_mask = src_mask[:seq_len, :seq_len] output = model(data, src_mask) loss = criterion(output.view(-1, ntokens), targets)
optimizer.zero_grad() loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), 0.5) optimizer.step()
total_loss += loss.item() if batch % log_interval == 0 and batch > 0: lr = scheduler.get_last_lr()[0] ms_per_batch = (time.time() - start_time) * 1000 / log_interval cur_loss = total_loss / log_interval ppl = math.exp(cur_loss) print(f'| epoch {epoch:3d} | {batch:5d}/{num_batches:5d} batches | ' f'lr {lr:02.2f} | ms/batch {ms_per_batch:5.2f} | ' f'loss {cur_loss:5.2f} | ppl {ppl:8.2f}') total_loss = 0 start_time = time.time()
def evaluate(model: nn.Module, eval_data: Tensor) -> float: model.eval() total_loss = 0. src_mask = generate_square_subsequent_mask(bptt).to(device) with torch.no_grad(): for i in range(0, eval_data.size(0) - 1, bptt): data, targets = get_batch(eval_data, i) seq_len = data.size(0) if seq_len != bptt: src_mask = src_mask[:seq_len, :seq_len] output = model(data, src_mask) output_flat = output.view(-1, ntokens) total_loss += seq_len * criterion(output_flat, targets).item() return total_loss / (len(eval_data) - 1)
def get_batch(source: Tensor, i: int) -> Tuple[Tensor, Tensor]: seq_len = min(bptt, len(source) - 1 - i) data = source[i:i+seq_len] target = source[i+1:i+1+seq_len].reshape(-1) return data, target
best_val_loss = float('inf') epochs = 3
with TemporaryDirectory() as tempdir: best_model_params_path = os.path.join(tempdir, "best_model_params.pt")
for epoch in range(1, epochs + 1): epoch_start_time = time.time() train(model) val_loss = evaluate(model, val_data) val_ppl = math.exp(val_loss) elapsed = time.time() - epoch_start_time print('-' * 89) print(f'| end of epoch {epoch:3d} | time: {elapsed:5.2f}s | ' f'valid loss {val_loss:5.2f} | valid ppl {val_ppl:8.2f}') print('-' * 89)
if val_loss < best_val_loss: best_val_loss = val_loss torch.save(model.state_dict(), best_model_params_path)
scheduler.step()
model.load_state_dict(torch.load(best_model_params_path))
test_loss = evaluate(model, test_data) test_ppl = math.exp(test_loss) print('=' * 89) print(f'| End of training | test loss {test_loss:5.2f} | test ppl {test_ppl:8.2f}') print('=' * 89)
|