-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
377 lines (327 loc) · 12 KB
/
main.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# -*- coding: utf-8 -*-
"""Main script."""
import getpass
import logging
import math
import pathlib
from itertools import product
from textwrap import dedent
from typing import Iterable, Optional, cast
import click
import pandas as pd
import pykeen.version
import seaborn as sns
import torch
from docdata import get_docdata
from matplotlib import pyplot as plt
from more_click import force_option, verbose_option
from pykeen.datasets import dataset_resolver, get_dataset
from pykeen.datasets.base import Dataset
from pykeen.sampling import negative_sampler_resolver
from pykeen.sampling.filtering import PythonSetFilterer
from torch.utils.benchmark import Timer
from tqdm import tqdm
logger = logging.getLogger(__name__)
USER = getpass.getuser()
VERSION = pykeen.get_version()
GIT_HASH = pykeen.get_git_hash()
GIT_BRANCH = pykeen.get_git_branch()
HERE = pathlib.Path(__file__).resolve().parent
DEFAULT_DIRECTORY = HERE.joinpath("data", USER, GIT_HASH)
TIMES_KEY = "times"
FNR_KEY = "fnr"
TIMES_COLUMNS = ["batch_size", "batch_id", "time"]
FNR_COLUMNS = ["fnr"]
@click.command()
@click.option(
"-d",
"--dataset",
type=click.Choice(dataset_resolver.lookup_dict, case_sensitive=False),
)
@click.option("--num-random-batches", type=int, default=20)
@click.option("--batch-size", type=int, default=32)
@click.option(
"-n", "--num-samples", type=int, default=4096
) # TODO: Determine this based on dataset?
@click.option("-o", "--directory", type=pathlib.Path, default=DEFAULT_DIRECTORY)
@click.option("--skip-fnr", is_flag=True)
@force_option
@verbose_option
def benchmark(
dataset: Optional[str],
num_random_batches: int,
batch_size: int,
num_samples: int,
directory: pathlib.Path,
skip_fnr: bool,
force: bool,
) -> None:
"""Benchmark negative sampling."""
times_dfs, fnr_dfs = [], []
for dataset_instance in _iterate_datasets(dataset):
times_dfs.append(
_time_helper(
dataset_instance,
directory=directory.joinpath(TIMES_KEY),
num_random_batches=num_random_batches,
force=force,
)
)
if not skip_fnr:
fnr_dfs.append(
_fnr_helper(
dataset_instance,
directory=directory.joinpath(FNR_KEY),
num_samples=num_samples,
batch_size=batch_size,
force=force,
)
)
sns.set_style("whitegrid")
_plot_times(pd.concat(times_dfs), key=TIMES_KEY, directory=directory)
if not skip_fnr:
_plot_fnr(pd.concat(fnr_dfs), key=FNR_KEY, directory=directory)
with directory.joinpath("README.md").open("w") as file:
print(
dedent(
f"""\
# Results on PyKEEN v{VERSION} ({GIT_BRANCH})
- See commit: [{GIT_HASH}](https://github.com/pykeen/pykeen/commit/{GIT_HASH})
- Browse commit: [{GIT_HASH}](https://github.com/pykeen/pykeen/tree/{GIT_HASH})
Run again with:
```shell
$ git clone https://github.com/pykeen/pykeen.git
$ cd pykeen
$ git checkout 2207eaef
$ pip install -e .
$ cd ..
$ git clone https://github.com/pykeen/negative-sampler-benchmark.git
$ cd negative-sampler-benchmark
$ python main.py
```
## Speed Performance
![Times](times.svg)
## False Negative Rate
![False Negative Rate](fnr.png)
"""
),
file=file,
)
def _time_helper(
dataset: Dataset,
*,
directory: pathlib.Path,
num_random_batches: int,
force: bool = False,
) -> pd.DataFrame:
dataset_directory = directory.joinpath(dataset.get_normalized_name())
dataset_directory.mkdir(exist_ok=True, parents=True)
batch_sizes = [
2**i for i in range(1, min(16, int(math.log2(dataset.training.num_triples))))
]
logger.info(
f"Evaluating batch sizes {batch_sizes} for dataset {dataset.get_normalized_name()}"
)
dfs = []
sampler_it = tqdm(list(negative_sampler_resolver), desc="Sampler", leave=False)
for negative_sampler_cls in sampler_it:
sampler_it.set_postfix(
sampler=negative_sampler_cls.get_normalized_name(),
dataset=dataset.get_normalized_name(),
)
path = dataset_directory.joinpath(
negative_sampler_cls.get_normalized_name()
).with_suffix(".tsv.gz")
if path.exists() and not force:
df = pd.read_csv(path, sep="\t")
else:
negative_sampler = negative_sampler_resolver.make(
query=negative_sampler_cls,
triples_factory=dataset.training,
)
progress = tqdm(
product(batch_sizes, range(num_random_batches)),
unit_scale=True,
desc="Batch",
total=len(batch_sizes) * num_random_batches,
leave=False,
)
data = []
for batch_size, batch_id in progress:
progress.set_postfix(
size=batch_size,
id=batch_id,
sampler=negative_sampler_cls.get_normalized_name(),
dataset=dataset.get_normalized_name(),
)
positive_batch_idx = torch.randperm(dataset.training.num_triples)[
:batch_size
]
positive_batch = dataset.training.mapped_triples[positive_batch_idx]
timer = Timer(
stmt="sampler.corrupt_batch(positive_batch=positive_batch)",
globals=dict(
sampler=negative_sampler,
positive_batch=positive_batch,
),
)
measurement = timer.blocked_autorange()
data.extend((batch_size, batch_id, t) for t in measurement.raw_times)
df = pd.DataFrame(data=data, columns=TIMES_COLUMNS)
df.to_csv(path, sep="\t", index=False)
df["dataset"] = dataset.get_normalized_name()
df["sampler"] = negative_sampler_cls.get_normalized_name()
df = df[["dataset", "sampler", *TIMES_COLUMNS]]
dfs.append(df)
return pd.concat(dfs)
def _plot_times(df: pd.DataFrame, *, key: str, directory: pathlib.Path):
g = sns.relplot(
data=df,
x="batch_size",
y="time",
hue="sampler",
kind="line",
col="dataset",
height=3.5,
col_wrap=4 if df.dataset.nunique() > 4 else None,
ci="sd",
estimator="mean",
).set(
xscale="log",
xlabel="Batch Size",
ylabel="Seconds Per Batch",
)
g.tight_layout()
g.fig.suptitle(_prep_title("Time Results"), fontsize=22, y=0.98)
make_space_above(g.axes, topmargin=1.0)
directory.mkdir(exist_ok=True, parents=True)
figure_path_stem = directory.joinpath(key)
plt.savefig(figure_path_stem.with_suffix(".svg"))
plt.savefig(figure_path_stem.with_suffix(".pdf"))
plt.savefig(figure_path_stem.with_suffix(".png"), dpi=300)
def _fnr_helper(
dataset: Dataset,
*,
directory: pathlib.Path,
num_samples: int,
batch_size: int,
force: bool = False,
) -> pd.DataFrame:
dataset_directory = directory.joinpath(dataset.get_normalized_name())
dataset_directory.mkdir(exist_ok=True, parents=True)
# create index structure for existence check
filterer = PythonSetFilterer(
mapped_triples=cast(
torch.LongTensor,
torch.cat(
[
dataset.training.mapped_triples,
dataset.validation.mapped_triples,
# dataset_instance.testing, # TODO: should this be used?
],
dim=0,
),
),
)
dfs = []
sampler_it = tqdm(list(negative_sampler_resolver), desc="Sampler", leave=False)
for negative_sampler_cls in sampler_it:
sampler_it.set_postfix(
sampler=negative_sampler_cls.get_normalized_name(),
dataset=dataset.get_normalized_name(),
)
path = dataset_directory.joinpath(
negative_sampler_cls.get_normalized_name()
).with_suffix(".tsv.gz")
if path.exists() and not force:
df = pd.read_csv(path, sep="\t")
else:
sampler = negative_sampler_resolver.make(
query=negative_sampler_cls,
triples_factory=dataset.training,
num_negs_per_pos=num_samples,
)
positive_batches = tqdm(
dataset.training.mapped_triples.split(split_size=batch_size, dim=0),
unit="batch",
unit_scale=True,
desc="Batch",
)
data = []
for positive_batch in positive_batches:
positive_batches.set_postfix(
batch_size=batch_size,
sampler=negative_sampler_cls.get_normalized_name(),
dataset=dataset.get_normalized_name(),
)
negative_batch = sampler.corrupt_batch(positive_batch=positive_batch)
false_negative_rates = (
filterer.contains(batch=negative_batch.view(-1, 3))
.view(negative_batch.shape[:-1])
.float()
.mean(dim=-1)
)
data.extend(false_negative_rates.tolist())
df = pd.DataFrame(data, columns=FNR_COLUMNS)
df.to_csv(path, sep="\t", index=False)
df["dataset"] = dataset.get_normalized_name()
df["sampler"] = negative_sampler_cls.get_normalized_name()
df = df[["dataset", "sampler", *FNR_COLUMNS]]
dfs.append(df)
return pd.concat(dfs)
def _plot_fnr(df: pd.DataFrame, *, directory: pathlib.Path, key: str):
g = sns.catplot(
data=df,
x="sampler",
y="fnr",
col="dataset",
kind="box",
height=3.5,
col_wrap=4 if df.dataset.nunique() > 4 else None,
).set_axis_labels(
"",
"False Negative Rate",
)
g.tight_layout()
g.fig.suptitle(_prep_title("False Negative Rate Results"), fontsize=22, y=0.98)
make_space_above(g.axes, topmargin=1.0)
directory.mkdir(exist_ok=True, parents=True)
figure_path_stem = directory.joinpath(key)
# plt.savefig(figure_path_stem.with_suffix('.svg')) # no SVG because too many outliers
plt.savefig(figure_path_stem.with_suffix(".pdf"))
plt.savefig(figure_path_stem.with_suffix(".png"), dpi=300)
def _prep_title(s: str) -> str:
title = f"{s} from {USER}/{pykeen.version.get_version(with_git_hash=True)}"
if (branch := pykeen.version.get_git_branch()) is not None:
title += f" ({branch})"
return title
def _iterate_datasets(dataset: Optional[str]) -> Iterable[Dataset]:
if dataset:
_dataset_list = [dataset]
else:
_dataset_list = _get_datasets()
it = tqdm(_dataset_list, desc="Dataset")
for dataset in it:
it.set_postfix(dataset=dataset)
yield get_dataset(dataset=dataset)
def _triples(d: str) -> int:
return get_docdata(dataset_resolver.lookup_dict[d])["statistics"]["triples"]
def _get_datasets():
rv = sorted(dataset_resolver.lookup_dict, key=_triples)
return rv[: rv.index("fb15k237") + 1] # include fb15k-237
def make_space_above(axes, topmargin: float = 1.0) -> None:
"""Increase figure size to make topmargin (in inches) space for titles, without changing the axes sizes.
:param axes: The array of axes.
:param topmargin: The margin (in inches) to impose on the top of the figure.
.. seealso:: Credit to https://stackoverflow.com/a/55768955/5775947
"""
fig = axes.flatten()[0].figure
s = fig.subplotpars
width, height = fig.get_size_inches()
fig_height = height - (1.0 - s.top) * height + topmargin
fig.subplots_adjust(
bottom=s.bottom * height / fig_height, top=1 - topmargin / fig_height
)
fig.set_figheight(fig_height)
if __name__ == "__main__":
benchmark()