-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
94 lines (77 loc) · 2.58 KB
/
run.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
import torch
import json
import os
current_directory = os.path.dirname(os.path.abspath(__file__))
os.chdir(current_directory)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
with open('training files/file1.txt', 'r', encoding='utf-8') as f:
test_data = f.read().lower()
print("file opened!")
f.close()
print(f"{(len(test_data)/1e6):.2f} million letters")
from tokenizer import KMerTokenizer
tokenizer = KMerTokenizer(k_mers=4)
tokenizer.load_model('tokenizer/vocabs/base_4k.json')
vocab_size = tokenizer.vocab_size
# Train and test splits
data = torch.tensor(tokenizer.encode(test_data), dtype=torch.long)
n = int(0.9*len(data)) # first 90% will be train, rest val
train_data = data[:n]
val_data = data[n:]
print(train_data[:10])
print(val_data[:10])
with open('enigma2/config.json', 'r', encoding='utf-8') as file:
params = json.load(file)
# required parameters
batch_size = params['batch_size']
block_size = params['block_size']
max_iters = params['max_iters']
eval_interval = params['eval_interval']
eval_iters = params['eval_iters']
learning_rate = params['learning_rate']
torch.manual_seed(19)
# data loading
def get_batch(split):
# generate a small batch of data of inputs x and targets y
data = train_data if split == 'train' else val_data
ix = torch.randint(len(data) - block_size, (batch_size,))
x = torch.stack([data[i:i+block_size] for i in ix])
y = torch.stack([data[i+1:i+block_size+1] for i in ix])
x, y = x.to(device), y.to(device)
return x, y
@torch.no_grad()
def estimate_loss():
out = {}
model.eval()
for split in ['train', 'val']:
losses = torch.zeros(eval_iters)
for k in range(eval_iters):
X, Y = get_batch(split)
logits, loss = model(X, Y)
losses[k] = loss.item()
out[split] = losses.mean()
model.train()
return out
from enigma2 import Transformer
model = Transformer(vocab_size=vocab_size)
m = model.to(device)
# no of parameters
n_param = sum(p.numel() for p in m.parameters())/1e6
print(f"{n_param:.2f} million")
optimizer = torch.optim.AdamW(model.parameters(), lr=learning_rate)
steps = []
train_losses = []
val_losses = []
for iter in range(max_iters):
if iter % eval_interval == 0 or iter == max_iters - 1:
losses = estimate_loss()
print(f"step {iter}: train loss {losses['train']:.4f}, val loss {losses['val']:.4f}")
steps.append(iter)
train_losses.append(losses['train'])
val_losses.append(losses['val'])
xb, yb = get_batch('train')
logits, loss = model(xb, yb)
optimizer.zero_grad(set_to_none=True)
loss.backward()
optimizer.step()
torch.save(model.state_dict(), f'enigma_{n_param:.0f}m.pth')