forked from andreaskipf/learnedcardinalities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
205 lines (152 loc) · 8.46 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import argparse
import time
import os
import torch
from torch.autograd import Variable
from torch.utils.data import DataLoader
from mscn.util import *
from mscn.data import get_train_datasets, load_data, make_dataset
from mscn.model import SetConv
def unnormalize_torch(vals, min_val, max_val):
vals = (vals * (max_val - min_val)) + min_val
return torch.exp(vals)
def qerror_loss(preds, targets, min_val, max_val):
qerror = []
preds = unnormalize_torch(preds, min_val, max_val)
targets = unnormalize_torch(targets, min_val, max_val)
for i in range(len(targets)):
if (preds[i] > targets[i]).cpu().data.numpy()[0]:
qerror.append(preds[i] / targets[i])
else:
qerror.append(targets[i] / preds[i])
return torch.mean(torch.cat(qerror))
def predict(model, data_loader, cuda):
preds = []
t_total = 0.
model.eval()
for batch_idx, data_batch in enumerate(data_loader):
samples, predicates, joins, targets, sample_masks, predicate_masks, join_masks = data_batch
if cuda:
samples, predicates, joins, targets = samples.cuda(), predicates.cuda(), joins.cuda(), targets.cuda()
sample_masks, predicate_masks, join_masks = sample_masks.cuda(), predicate_masks.cuda(), join_masks.cuda()
samples, predicates, joins, targets = Variable(samples), Variable(predicates), Variable(joins), Variable(
targets)
sample_masks, predicate_masks, join_masks = Variable(sample_masks), Variable(predicate_masks), Variable(
join_masks)
t = time.time()
outputs = model(samples, predicates, joins, sample_masks, predicate_masks, join_masks)
t_total += time.time() - t
for i in range(outputs.data.shape[0]):
preds.append(outputs.data[i])
return preds, t_total
def print_qerror(preds_unnorm, labels_unnorm):
qerror = []
for i in range(len(preds_unnorm)):
if preds_unnorm[i] > float(labels_unnorm[i]):
qerror.append(preds_unnorm[i] / float(labels_unnorm[i]))
else:
qerror.append(float(labels_unnorm[i]) / float(preds_unnorm[i]))
print("Median: {}".format(np.median(qerror)))
print("90th percentile: {}".format(np.percentile(qerror, 90)))
print("95th percentile: {}".format(np.percentile(qerror, 95)))
print("99th percentile: {}".format(np.percentile(qerror, 99)))
print("Max: {}".format(np.max(qerror)))
print("Mean: {}".format(np.mean(qerror)))
def train_and_predict(workload_name, featurization, num_queries, num_buckets, num_samples, num_epochs, batch_size, hid_units, cuda):
# Load training and validation data
num_materialized_samples = num_samples
dicts, column_min_max_vals, min_val, max_val, labels_train, labels_test, max_num_joins, max_num_predicates, train_data, test_data = get_train_datasets(
num_queries, num_materialized_samples, featurization, num_buckets)
table2vec, column2vec, op2vec, join2vec = dicts
# Train model
sample_feats = len(table2vec) + num_materialized_samples
join_feats = len(join2vec)
if featurization == "range":
predicate_feats = len(column2vec) + 2*len(op2vec)+2
elif featurization == "conj" or featurization == "disj":
predicate_feats = num_buckets + 1 + len(column2vec)
else:
predicate_feats = len(column2vec) + len(op2vec) + 1
model = SetConv(sample_feats, predicate_feats, join_feats, hid_units)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
if cuda:
model.cuda()
train_data_loader = DataLoader(train_data, batch_size=batch_size)
test_data_loader = DataLoader(test_data, batch_size=batch_size)
model.train()
for epoch in range(num_epochs):
loss_total = 0.
for batch_idx, data_batch in enumerate(train_data_loader):
samples, predicates, joins, targets, sample_masks, predicate_masks, join_masks = data_batch
if cuda:
samples, predicates, joins, targets = samples.cuda(), predicates.cuda(), joins.cuda(), targets.cuda()
sample_masks, predicate_masks, join_masks = sample_masks.cuda(), predicate_masks.cuda(), join_masks.cuda()
samples, predicates, joins, targets = Variable(samples), Variable(predicates), Variable(joins), Variable(
targets)
sample_masks, predicate_masks, join_masks = Variable(sample_masks), Variable(predicate_masks), Variable(
join_masks)
optimizer.zero_grad()
outputs = model(samples, predicates, joins, sample_masks, predicate_masks, join_masks)
loss = qerror_loss(outputs, targets.float(), min_val, max_val)
loss_total += loss.item()
loss.backward()
optimizer.step()
print("Epoch {}, loss: {}".format(epoch, loss_total / len(train_data_loader)))
# Get final training and validation set predictions
preds_train, t_total = predict(model, train_data_loader, cuda)
print("Prediction time per training sample: {}".format(t_total / len(labels_train) * 1000))
preds_test, t_total = predict(model, test_data_loader, cuda)
print("Prediction time per validation sample: {}".format(t_total / len(labels_test) * 1000))
# Unnormalize
preds_train_unnorm = unnormalize_labels(preds_train, min_val, max_val)
labels_train_unnorm = unnormalize_labels(labels_train, min_val, max_val)
preds_test_unnorm = unnormalize_labels(preds_test, min_val, max_val)
labels_test_unnorm = unnormalize_labels(labels_test, min_val, max_val)
label = labels_test_unnorm
# Print metrics
print("\nQ-Error training set:")
print_qerror(preds_train_unnorm, labels_train_unnorm)
print("\nQ-Error validation set:")
print_qerror(preds_test_unnorm, labels_test_unnorm)
print("")
# Load test data
file_name = "workloads/" + workload_name
joins, predicates, tables, samples, label = load_data(file_name, num_materialized_samples, featurization)
# Get feature encoding and proper normalization
samples_test = encode_samples(tables, samples, table2vec)
predicates_test, joins_test = encode_data(predicates, joins, column_min_max_vals, column2vec, op2vec, join2vec, featurization, num_buckets)
labels_test, _, _ = normalize_labels(label, min_val, max_val)
print("Number of test samples: {}".format(len(labels_test)))
max_num_predicates = max([len(p) for p in predicates_test])
max_num_joins = max([len(j) for j in joins_test])
# Get test set predictions
test_data = make_dataset(samples_test, predicates_test, joins_test, labels_test, max_num_joins, max_num_predicates)
test_data_loader = DataLoader(test_data, batch_size=batch_size)
preds_test, t_total = predict(model, test_data_loader, cuda)
print("Prediction time per test sample: {}".format(t_total / len(labels_test) * 1000))
# Unnormalize
preds_test_unnorm = unnormalize_labels(preds_test, min_val, max_val)
# Print metrics
print("\nQ-Error " + workload_name + ":")
print_qerror(preds_test_unnorm, label)
# Write predictions
file_name = "results/predictions_" + workload_name + "_" + featurization + str(int(time.time())) + ".csv"
os.makedirs(os.path.dirname(file_name), exist_ok=True)
with open(file_name, "w") as f:
for i in range(len(preds_test_unnorm)):
f.write(str(preds_test_unnorm[i]) + "," + str(label[i]) + "\n")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("testset", help="synthetic, scale, or job-light")
parser.add_argument("--feat", help="featurization: mscn, range, conj, or disj (default: mscn)", type=str, default="mscn")
parser.add_argument("--queries", help="number of training queries (default: 10000)", type=int, default=10000)
parser.add_argument("--buckets", help="number of buckets (default: 32)", type=int, default=32)
parser.add_argument("--samples", help="number of materialized samples (default: 1000)", type=int, default=1000)
parser.add_argument("--epochs", help="number of epochs (default: 10)", type=int, default=10)
parser.add_argument("--batch", help="batch size (default: 1024)", type=int, default=1024)
parser.add_argument("--hid", help="number of hidden units (default: 256)", type=int, default=256)
parser.add_argument("--cuda", help="use CUDA", action="store_true", default=False)
args = parser.parse_args()
train_and_predict(args.testset, args.feat, args.queries, args.buckets, args.samples, args.epochs, args.batch, args.hid, args.cuda)
if __name__ == "__main__":
main()