forked from SamLynnEvans/Transformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
183 lines (152 loc) · 6.96 KB
/
train.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
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
import argparse
import time
import torch
from Models import get_model
from Process import *
import torch.nn.functional as F
from Optim import CosineWithRestarts
from Batch import create_masks
import dill as pickle
def train_model(model, opt):
print("training model...")
model.train()
start = time.time()
if opt.checkpoint > 0:
cptime = time.time()
for epoch in range(opt.epochs):
total_loss = 0
if opt.floyd is False:
print(" %dm: epoch %d [%s] %d%% loss = %s" %\
((time.time() - start)//60, epoch + 1, "".join(' '*20), 0, '...'), end='\r')
if opt.checkpoint > 0:
torch.save(model.state_dict(), 'weights/model_weights')
for i, batch in enumerate(opt.train):
src = batch.src.transpose(0,1)
trg = batch.trg.transpose(0,1)
trg_input = trg[:, :-1]
src_mask, trg_mask = create_masks(src, trg_input, opt)
preds = model(src, trg_input, src_mask, trg_mask)
ys = trg[:, 1:].contiguous().view(-1)
opt.optimizer.zero_grad()
loss = F.cross_entropy(preds.view(-1, preds.size(-1)), ys, ignore_index=opt.trg_pad)
loss.backward()
opt.optimizer.step()
if opt.SGDR == True:
opt.sched.step()
total_loss += loss.item()
if (i + 1) % opt.printevery == 0:
p = int(100 * (i + 1) / opt.train_len)
avg_loss = total_loss/opt.printevery
if opt.floyd is False:
print(" %dm: epoch %d [%s%s] %d%% loss = %.3f" %\
((time.time() - start)//60, epoch + 1, "".join('#'*(p//5)), "".join(' '*(20-(p//5))), p, avg_loss), end='\r')
else:
print(" %dm: epoch %d [%s%s] %d%% loss = %.3f" %\
((time.time() - start)//60, epoch + 1, "".join('#'*(p//5)), "".join(' '*(20-(p//5))), p, avg_loss))
total_loss = 0
if opt.checkpoint > 0 and ((time.time()-cptime)//60) // opt.checkpoint >= 1:
torch.save(model.state_dict(), 'weights/model_weights')
cptime = time.time()
print("%dm: epoch %d [%s%s] %d%% loss = %.3f\nepoch %d complete, loss = %.03f" %\
((time.time() - start)//60, epoch + 1, "".join('#'*(100//5)), "".join(' '*(20-(100//5))), 100, avg_loss, epoch + 1, avg_loss))
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-src_data', required=True)
parser.add_argument('-trg_data', required=True)
parser.add_argument('-src_lang', required=True)
parser.add_argument('-trg_lang', required=True)
parser.add_argument('-no_cuda', action='store_true')
parser.add_argument('-SGDR', action='store_true')
parser.add_argument('-epochs', type=int, default=2)
parser.add_argument('-d_model', type=int, default=512)
parser.add_argument('-n_layers', type=int, default=6)
parser.add_argument('-heads', type=int, default=8)
parser.add_argument('-dropout', type=int, default=0.1)
parser.add_argument('-batchsize', type=int, default=1500)
parser.add_argument('-printevery', type=int, default=100)
parser.add_argument('-lr', type=int, default=0.0001)
parser.add_argument('-load_weights')
parser.add_argument('-create_valset', action='store_true')
parser.add_argument('-max_strlen', type=int, default=80)
parser.add_argument('-floyd', action='store_true')
parser.add_argument('-checkpoint', type=int, default=0)
opt = parser.parse_args()
opt.device = 0 if opt.no_cuda is False else -1
if opt.device == 0:
assert torch.cuda.is_available()
read_data(opt)
SRC, TRG = create_fields(opt)
opt.train = create_dataset(opt, SRC, TRG)
model = get_model(opt, len(SRC.vocab), len(TRG.vocab))
opt.optimizer = torch.optim.Adam(model.parameters(), lr=opt.lr, betas=(0.9, 0.98), eps=1e-9)
if opt.SGDR == True:
opt.sched = CosineWithRestarts(opt.optimizer, T_max=opt.train_len)
if opt.checkpoint > 0:
print("model weights will be saved every %d minutes and at end of epoch to directory weights/"%(opt.checkpoint))
if opt.load_weights is not None and opt.floyd is not None:
os.mkdir('weights')
pickle.dump(SRC, open('weights/SRC.pkl', 'wb'))
pickle.dump(TRG, open('weights/TRG.pkl', 'wb'))
train_model(model, opt)
if opt.floyd is False:
promptNextAction(model, opt, SRC, TRG)
def yesno(response):
while True:
if response != 'y' and response != 'n':
response = input('command not recognised, enter y or n : ')
else:
return response
def promptNextAction(model, opt, SRC, TRG):
saved_once = 1 if opt.load_weights is not None or opt.checkpoint > 0 else 0
if opt.load_weights is not None:
dst = opt.load_weights
if opt.checkpoint > 0:
dst = 'weights'
while True:
save = yesno(input('training complete, save results? [y/n] : '))
if save == 'y':
while True:
if saved_once != 0:
res = yesno("save to same folder? [y/n] : ")
if res == 'y':
break
dst = input('enter folder name to create for weights (no spaces) : ')
if ' ' in dst or len(dst) < 1 or len(dst) > 30:
dst = input("name must not contain spaces and be between 1 and 30 characters length, enter again : ")
else:
try:
os.mkdir(dst)
except:
res= yesno(input(dst + " already exists, use anyway? [y/n] : "))
if res == 'n':
continue
break
print("saving weights to " + dst + "/...")
torch.save(model.state_dict(), f'{dst}/model_weights')
if saved_once == 0:
pickle.dump(SRC, open(f'{dst}/SRC.pkl', 'wb'))
pickle.dump(TRG, open(f'{dst}/TRG.pkl', 'wb'))
saved_once = 1
print("weights and field pickles saved to " + dst)
res = yesno(input("train for more epochs? [y/n] : "))
if res == 'y':
while True:
epochs = input("type number of epochs to train for : ")
try:
epochs = int(epochs)
except:
print("input not a number")
continue
if epochs < 1:
print("epochs must be at least 1")
continue
else:
break
opt.epochs = epochs
train_model(model, opt)
else:
print("exiting program...")
break
# for asking about further training use while true loop, and return
if __name__ == "__main__":
main()