forked from threelittlemonkeys/lstm-crf-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
65 lines (62 loc) · 2.52 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
from model import *
from utils import *
from evaluate import *
def load_data():
data = dataloader()
batch = []
cti = load_tkn_to_idx(sys.argv[2]) # char_to_idx
wti = load_tkn_to_idx(sys.argv[3]) # word_to_idx
itt = load_idx_to_tkn(sys.argv[4]) # idx_to_tkn
print("loading %s..." % sys.argv[5])
with open(sys.argv[5], "r") as fo:
text = fo.read().strip().split("\n" * (HRE + 1))
for block in text:
for line in block.split("\n"):
x, y = line.split("\t")
x = [x.split(":") for x in x.split(" ")]
y = [int(y)] if HRE else [int(x) for x in y.split(" ")]
xc, xw = zip(*[(list(map(int, xc.split("+"))), int(xw)) for xc, xw in x])
data.append_item(xc = [xc], xw = [xw], y0 = y)
data.append_row()
data.strip()
for _batch in data.split():
xc, xw = data.tensor(_batch.xc, _batch.xw, _batch.lens)
_, y0 = data.tensor(None, _batch.y0, sos = True)
batch.append((xc, xw, y0))
print("data size: %d" % len(data.y0))
print("batch size: %d" % BATCH_SIZE)
return batch, cti, wti, itt
def train():
num_epochs = int(sys.argv[-1])
batch, cti, wti, itt = load_data()
model = rnn_crf(len(cti), len(wti), len(itt))
optim = torch.optim.Adam(model.parameters(), lr = LEARNING_RATE)
print(model)
epoch = load_checkpoint(sys.argv[1], model) if isfile(sys.argv[1]) else 0
filename = re.sub("\.epoch[0-9]+$", "", sys.argv[1])
print("training model...")
for ei in range(epoch + 1, epoch + num_epochs + 1):
loss_sum = 0
timer = time()
for xc, xw, y0 in batch:
loss = model(xc, xw, y0) # forward pass and compute loss
loss.backward() # compute gradients
optim.step() # update parameters
loss_sum += loss.item()
timer = time() - timer
loss_sum /= len(batch)
if ei % SAVE_EVERY and ei != epoch + num_epochs:
save_checkpoint("", None, ei, loss_sum, timer)
else:
save_checkpoint(filename, model, ei, loss_sum, timer)
if EVAL_EVERY and (ei % EVAL_EVERY == 0 or ei == epoch + num_epochs):
args = [model, cti, wti, itt]
evaluate(predict(sys.argv[6], *args), True)
model.train()
print()
if __name__ == "__main__":
if len(sys.argv) not in [7, 8]:
sys.exit("Usage: %s model char_to_idx word_to_idx tag_to_idx training_data (validation_data) num_epoch" % sys.argv[0])
if len(sys.argv) == 7:
EVAL_EVERY = False
train()