-
Notifications
You must be signed in to change notification settings - Fork 2
/
logic.py
147 lines (129 loc) · 4.65 KB
/
logic.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
import argparse
import torch
import torch.nn as nn
from text.datasetpl import Dataset
from layers import PLRNN, PLTFN
import math
import time
import opts
import numpy as np
# build opt parser
parser = argparse.ArgumentParser(description='propositional logic')
parser.add_argument('-data_dir', required=True,
default='propositionallogic', type=str,
help='train file, one sentence per line.')
parser.add_argument('-max_bin', type=int, default=7,
help='max number of logical operations.')
# opts.py
opts.add_md_help_argument(parser)
opts.model_opts(parser)
opts.train_opts(parser)
opts.preprocess_opts(parser)
opt = parser.parse_args()
opt.cuda = len(opt.gpuid) > 0
# for grid search
opt.inner_size = 2 * opt.word_vec_size
opt.head_size = opt.word_vec_size // opt.num_heads
print(opt)
print('-' * 42)
torch.manual_seed(opt.seed)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if torch.cuda.is_available():
torch.cuda.manual_seed(opt.seed)
def prepare_batch(mb):
y, x1, x2 = mb
return y.to(device), x1.to(device), x2.to(device)
def eval(model, dataset, valid=True):
print('-' * 42)
print('| Evaluating')
g_tot, g_crt = 0, 0
bin_acc = []
for i in range(1, 13):
if valid:
test_data_i = dataset.get_valid(i, 64)
else:
test_data_i = dataset.get_test(i, 64)
tot, crt = 0., 0.
for mb in test_data_i:
y, x1, x2 = prepare_batch(mb)
pred = model(x1, x2)
_, yhat = pred.max(1)
crt += y.eq(yhat).long().sum().item()
tot += y.numel()
acc = crt * 100. / tot
g_tot += tot
g_crt += crt
bin_acc += [acc]
print("| n_ops: %2d | accuracy: %.2f | %4d" % (i, acc, tot))
g_acc = g_crt * 100 / g_tot
print('| Average accuracy: %.2f' % g_acc)
print('-' * 42)
report = {'bin_acc': bin_acc, 'avg_acc': g_acc}
return report
def noam_scheduler(optimizer, step):
lr = (np.power(opt.word_vec_size, -0.5)
* min(np.power(step, -0.5),
step * np.power(opt.warmup, -1.5)))
optimizer.param_groups[0]['lr'] = lr
return lr
def train():
dataset = Dataset(opt.data_dir)
vocab_size = len(dataset.token2idx)
n_classes = len(dataset.class2idx)
if opt.arch == 'rnn':
model = PLRNN(opt.word_vec_size, vocab_size, n_classes,
opt.layers, opt.dropout)
else:
model = PLTFN(opt.word_vec_size, vocab_size,
n_classes, opt.num_heads,
opt.head_size, opt.layers, opt.inner_size,
opt.dropout)
print(model)
print('initialize model')
for p in model.parameters():
p.data.uniform_(-opt.param_init, opt.param_init)
crit = nn.NLLLoss(size_average=False)
model = model.to(device)
crit = crit.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=opt.lr)
best_valid_acc = 0
for eidx in range(opt.epochs):
tot_loss = 0
n_samples = 0
train_data = dataset.get_train(opt.max_bin, opt.batch_size)
num_batches = len(train_data)
ud_start = time.time()
model.train()
for i, mb in enumerate(train_data):
y, x1, x2 = prepare_batch(mb)
optimizer.zero_grad()
pred = model(x1, x2)
loss = crit(pred, y)
loss.backward()
if opt.max_grad_norm > 0:
torch.nn.utils.clip_grad_norm_(model.parameters(),
opt.max_grad_norm)
optimizer.step()
tot_loss += loss.item()
n_samples += y.numel()
if i % opt.report_every == 0 and i > 0:
ud = time.time() - ud_start
args = [eidx, i / num_batches, math.exp(tot_loss/n_samples),
opt.report_every/ud, opt.lr]
print("| epoch {:2d} | {:.2f}% | ppl {:.3f} "
"| speed {:.1f} b/s | lr {:.5f}".format(*args))
ud_start = time.time()
model.eval()
report = eval(model, dataset, True)
if report['avg_acc'] > best_valid_acc:
# save checkpoint here!
best_valid_acc = report['avg_acc']
# print('| Run model on test data!')
test_report = eval(model, dataset, False)
checkpoint = {'params': model.state_dict(),
'opt': opt,
'report': test_report}
torch.save(checkpoint, opt.save_model)
print('| Save checkpoint: %s | Valid ppl: %.3f' %
(opt.save_model, best_valid_acc))
train()