Manuscript Offprint
arXiv:2608.30652PyKEEN-NSX: A Modular Framework for Static, Dynamic and Schema-Aware Negative Sampling in PyKEEN
Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.
Abstract
Embedding methods have become popular due to their scalability on link prediction and/or triple classification tasks on Knowledge Graphs (KGs). Embedding models are trained relying on both positive and negative samples of triples. However, since KGs generally contain only positive assertions, negative samples are artificially generated through negative sampling strategies, ranging from simple random corruption to more sophisticated approaches that exploit structural, semantic, or embedding information. The design and implementation of advanced negative samplers remains challenging, as most popular Knowledge Graph Embedding (KGE) libraries provide support only for basic strategies and lack a unified framework for developing more advanced and customized solutions. To address this gap, we introduce PyKEEN-NSX, an extension of PyKEEN, the popular KGE framework, that provides a modular engineered abstraction for negative sampling. The proposed architecture separates the generation of candidate negative pools, conditioned on an explicit context, from the selection strategy, enabling the development and integration of static, schema-aware and dynamic approaches within a consistent framework. Based on this abstraction, we implement six negative samplers, while remaining fully compatible with existing PyKEEN workflows and pipelines. As a proof of concept, we study negative availability across four datasets, showing that constrained pools frequently fall below the requested number of negatives, so that the encoded criterion is to a large extent replaced by the random fallback that supplements them.
Keywords
Knowledge Graphs, Graph Embedding, Graph Representation Learning, Negative Sampling, Corruption Techniques
i.diliso1@phd.uniba.it (I. Diliso); nicola.fanizzi@uniba.it (N. Fanizzi); claudia.damato@uniba.it (C. d'Amato) 0009-0007-2942-202X (I. Diliso); 0000-0001-5319-7933 (N. Fanizzi); 0000-0002-3385-987X (C. d'Amato)
© 2026 Copyright for this paper by its authors. Use permitted under Creative Commons License Attribution 4.0 International (CC BY 4.0).

filtering and tensor handling to express the criterion that distinguishes it. The interplay between representation model and sampling strategy is thus impractical to investigate, and comparing samplers on equal terms becomes difficult. This work starts from the observation that the diversity of negative samplers is largely confined to a single component: any strategy factors into the generation of a pool of candidate entities and the selection of negatives from it, differing only in the former. Targeting PyKEEN [14], one of the most popular libraries for building KGE models, we introduce PyKEEN-NSX, a modular extension that makes this factorization explicit: the components common to all samplers are provided once, and a new strategy is obtained by defining its pool alone, while remaining usable with every existing KGE model available in the framework. Six samplers, spanning static, schema-aware and dynamic corruption, are implemented on this basis. As a proof of concept, we quantify negative availability across three datasets, showing that constrained pools, frequently fall below the required number of negatives, so that the intended criterion is to a large extent replaced by a fallback mechanism, e.g. random corruption, without being fully aware of it. In the following, Sect. 2 presents the abstraction and instantiates it over the literature, Sect. 3 describes the resource, and Sect. 4 reports the analysis. An extended version of this work is available as a preprint [15].
2. A General Abstraction for Negative Samplers
Negative samplers are usually presented as a heterogeneous catalogue (random, structural, schema-aware, adversarial) and implemented as self-contained, mutually incompatible components, yet they admit a common decomposition. Corrupting a positive triple of a KG , with assertions and schema when available, replaces its head, tail, or both with entities drawn from the entity set [16]. Given a corruption target , any sampler factors into: a pool generator , returning the entities admissible as corruptions of on under a context , i.e. the assertions , the schema , or the state of an auxiliary model; and a selector , drawing the negatives used for training. The factorization instantiates over the literature. Among static strategies, whose context is fixed and whose pool is therefore precomputed once, Random [5] and Bernoulli [10] take and require no context, the latter drawing the corruption target with a relation-dependent probability; Corrupt [17] ( ) restricts the pool to the entities observed in position for relation ; Typed [9] ( ) to those satisfying the domain/range of or sharing a class with the corrupted argument; and Relational [6] ( ) to those linked to the fixed argument by some relation . Dynamic strategies take and must rebuild the pool per batch: Nearest Neighbor [6] takes the entities closest to the corrupted argument under the auxiliary embedding, and Adversarial [6] takes the closest to the prediction of for target . All of the above leave as a uniform draw. The remaining variation lies in : NSCaching [12] draws uniformly from a per-triple cache of high-scoring negatives refreshed by importance sampling, self-adversarial sampling [18] keeps but weights the draw by the softmax of the model score, and KBGAN [11] samples from a generator, trained by policy gradient, over a random candidate subset of . Two observations follow. First, variation concentrates in : every strategy that does not consult a score at draw time leaves as a uniform draw, so a new sampler is fully specified by its pool. Second, the static/dynamic distinction is not a taxonomic divide but a property of : pools conditioned on or , both fixed, are precomputed once, whereas those conditioned on must be rebuilt per batch as training proceeds. Crucially, once is explicit, its size becomes measurable, turning a usually implicit assumption into a verifiable one: a strategy is meaningful only where , a condition constrained pools frequently violate (see Sect. 4).
3. PyKEEN-NSX
PyKEEN-NSX realizes the abstraction of Sect. 2 as an extension of PyKEEN. The resource is built around an abstract class that inherits from PyKEEN's NegativeSampler and provides, once, the utilities every sampler would otherwise reimplement: replication of the positive batch, choice of the corruption target,

Figure 1: Architecture of PyKEEN-NSX. The abstract base class provides the shared corruption utilities and a default uniform selector ; a new strategy is obtained by defining its pool generator P through the highlighted hooks. Custom classes introduced by the extension are shown under/over each component.
tensor assembly, caching, and the fallback policy, applied when a pool is smaller than requested, in agreement with the literature. The two components of the abstraction, the pool generator P and the selector , are exposed as hooks (Fig. 1). A uniform selector is provided by default, so a new strategy is obtained by defining P alone; the same holds for the remaining components, whose default behavior may be redefined, down to the batch-level corruption routine itself. On this basis, PyKEEN-NSX ships six samplers. The static Corrupt, Relational and schema-aware Typed (in a domain/range and and entity-class variant) precompute their pools, the latter from an OWL ontology ingested by the extension's preprocessing utilities; the dynamic NearestNeighbour and Adversarial condition their pools on the state of an auxiliary model and rebuild them per batch. The latter accept any pre-trained model exposing PyKEEN's ERMode1 interface, together with a user-supplied prediction function, keeping the auxiliary model decoupled from the sampling logic. Since the base class conforms to PyKEEN's sampler interface, all six are usable throughout its training, evaluation and hyper-parameter optimization pipelines without modification. Making P an explicit object has two practical consequences beyond ease of extension. First, the fallback that supplements an undersized pool, usually hard-coded, becomes an exposed parameter (integrate): when a pool yields fewer than k candidates, the sampler can draw with replacement, top it up with randomly generated entities, or return the undersized pool as is. This makes the point at which a strategy degrades into random corruption both controllable and measurable directly from the interface without any training. Second, standardizing the pool under one base class makes samplers directly comparable; to our knowledge, no other KGE library offers these strategies pre-implemented under a common interface. Documentation , worked examples for training and standalone analysis, and consistent formatting accompany the code to support reuse.
4. Preliminary Negative Sampling Analysis
The number of negatives k drawn per positive is a standard parameter of KGE training. Under random corruption it is unproblematic: the pool is the whole entity set, so any k is satisfied. Once P is constrained by structural or semantic criteria this guarantee is lost, and the pool varies from triple to triple. Where it is too small, the common remedy is to supplement it with random entities [6], which secures k at the cost of overriding the criterion the strategy encodes. The extent of this substitution is rarely reported, yet where most triples admit no pool of the requested size, the training signal is determined largely by random corruption. Since PyKEEN-NSX exposes P, this can be measured through the interface itself, without training. Fig. 2 reports, for each sampler and dataset, the fraction of triples whose pool falls below k, i.e. the share of the training set on which the strategy must fall back on random corruption. The analysis covers four datasets: YAGO4-20 [19], DBpedia50K [20], ARCO20 [21]
https://ara-t3.github.io/pykeen-nsx/
https://github.com/ara-t3/pykeen-nsx

Figure 2: Negative availability: for each k, the percentage of training triples whose pool cannot supply k negatives, and on which the sampler therefore falls back on random corruption (k = 0 counts empty pools).

Figure 3: Link prediction evaluation (Mean Reciprocal Rank) on ARCO20 (RotatE, k = 40), for each sampler in its pure form and with random fallback (integrate).
and WHOW5 [21]. Schema-aware strategies require a T that standard KGE benchmarks do not distribute; we therefore rely on the ontology-equipped datasets provided by [21]. Dynamic samplers are excluded, since their pool is fixed by construction at the k nearest entities, hence a parameter of the strategy rather than a property of the data. Fig. 3 compares the samplers on link prediction with RotatE on ARCO20, the dataset richest in schema axioms, under default PyKEEN hyperparameters and k = 40, a common high-negative setting, each in its pure form and with random integration. The two figures agree: the gain from integration follows the shortfall of Fig. 2, and the integrated score approaches that of Random (0.686) as the shortfall grows. Relational, which cannot supply forty negatives for 99% of ARCO20 triples, recovers 0.674 and is thus indistinguishable from random corruption, whereas Corrupt, short on 16%, reaches only 0.370. Dynamic samplers are unaffected, their pools being fixed at k by construction. We emphasize how the shortfall curve acts as a compatibility check between strategy and dataset, computable before any training: it gives the largest k at which a strategy still is itself, and so whether it is applicable at all on a given KG.
5. Conclusions and Future Work
We presented PyKEEN-NSX, a modular extension of PyKEEN that factors negative sampling into a pool generator, conditioned on an explicit context, and a selector. This abstraction unifies static, schema-aware and dynamic strategies under a single interface, lets a new sampler be defined by its pool alone, and turns pool size into a measurable property rather than an implicit assumption. Using it, our preliminary analysis showed that constrained pools, structural ones above all, rarely reach the requested k, and that under random integration link prediction recovers the score of random corruption itself: the encoded criterion is silently replaced by its fallback. The extension makes this substitution controllable and observable without training. Future work will extend the catalogue on the selector side, where score-based strategies such as NSCaching and self-adversarial sampling remain unimplemented, and broaden the evaluation across datasets and models. Performance optimization, through caching and faster pool computation, and a parallel implementation are also foreseen.
Declaration on Generative AI
During the preparation of this work, DeepL Write, Grammarly, and ChatGPT were used for grammar checking, rephrasing, and rewording. After using these tools and services, the authors reviewed and edited the content as needed, taking full responsibility for the publication’s content.
References
[1] K. Gashteovski, R. Gemulla, B. Kotnis, S. Hertling, C. Meilicke, On aligning openie extractions with knowledge bases: A case study, Association for Computational Linguistics (ACL), 2020.
[2] D. Li, H. Qu, J. Wang, A survey on knowledge graph-based recommender systems, in: 2023 China Automation Congress (CAC), IEEE, 2023, pp. 2925–2930.
[3] T. Madushanka, R. Ichise, Negative sampling in knowledge graph representation learning: A review, arXiv preprint arXiv:2402.19195 (2024).
[4] M. Nickel, K. Murphy, V. Tresp, E. Gabrilovich, A review of relational machine learning for knowledge graphs, Proceedings of the IEEE 104 (2016) 11–33. doi:10.1109/JPROC.2015.2483592.
[5] A. Bordes, N. Usunier, A. Garcia-Duran, J. Weston, O. Yakhnenko, Translating embeddings for modeling multi-relational data, Advances in neural information processing systems 26 (2013).
[6] B. Kotnis, V. Nastase, Analysis of the impact of negative sampling on link prediction in knowledge graphs, arXiv preprint arXiv:1708.06816 (2017).
[7] S. Dash, A. Gliozzo, Distributional negative sampling for knowledge base completion, arXiv preprint arXiv:1908.06178 (2019).
[8] C. Wang, Z. Zhu, P. Meng, Y. Qiu, Leveraging network structure for efficient dynamic negative sampling in network embedding, Information Sciences 606 (2022) 853–863.
[9] D. Krompaß, S. Baier, V. Tresp, Type-constrained representation learning in knowledge graphs, in: The Semantic Web-ISWC 2015: 14th International Semantic Web Conference, Bethlehem, PA, USA, October 11-15, 2015, Proceedings, Part I 14, Springer, 2015, pp. 640–655.
[10] Z. Wang, J. Zhang, J. Feng, Z. Chen, Knowledge graph embedding by translating on hyperplanes, in: Proceedings of the AAAI conference on artificial intelligence, volume 28, 2014.
[11] L. Cai, W. Y. Wang, KBGAN: Adversarial learning for knowledge graph embeddings, in: M. Walker, H. Ji, A. Stent (Eds.), Proceedings of the 2018 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, Volume 1 (Long Papers), Association for Computational Linguistics, New Orleans, Louisiana, 2018, pp. 1470–1480. URL: https://aclanthology.org/N18-1133. doi:10.18653/v1/N18-1133.
[12] Y. Zhang, Q. Yao, Y. Shao, L. Chen, Nscaching: simple and efficient negative sampling for knowledge graph embedding, in: 2019 IEEE 35th International Conference on Data Engineering (ICDE), IEEE, 2019, pp. 614–625.
[13] N. Jain, T.-K. Tran, M. H. Gad-Elrab, D. Stepanova, Improving knowledge graph embeddings with ontological reasoning, in: International semantic web conference, Springer, 2021, pp. 410–426.
[14] M. Ali, M. Berrendorf, C. T. Hoyt, L. Vermue, S. Sharifzadeh, V. Tresp, J. Lehmann, PyKEEN 1.0: A Python Library for Training and Evaluating Knowledge Graph Embeddings, Journal of Machine Learning Research 22 (2021) 1–6. URL: http://jmlr.org/papers/v22/20-825.html.
[15] C. d'Amato, I. Diliso, N. Fanizzi, Z. Saeed, Enhancing pykeen with multiple negative sampling solutions for knowledge graph embedding models, arXiv preprint arXiv:2508.05587 (2025).
[16] X. Chen, W. Zhang, Z. Yao, M. Chen, S. Tang, Negative sampling with adaptive denoising mixup for knowledge graph embedding, in: International Semantic Web Conference, Springer, 2023, pp. 253–270.
[17] R. Socher, D. Chen, C. D. Manning, A. Ng, Reasoning with neural tensor networks for knowledge base completion, Advances in neural information processing systems 26 (2013).
[18] Z. Sun, Z.-H. Deng, J.-Y. Nie, J. Tang, Rotate: Knowledge graph embedding by relational rotation in complex space, arXiv preprint arXiv:1902.10197 (2019).
[19] T. Pellissier Tanon, G. Weikum, F. Suchanek, Yago 4: A reason-able knowledge base, in: The Semantic Web: 17th International Conference, ESWC 2020, Heraklion, Crete, Greece, May 31–June 4, 2020, Proceedings 17, Springer, 2020, pp. 583–596.
[20] J. Lehmann, R. Isele, M. Jakob, A. Jentzsch, D. Kontokostas, P. N. Mendes, S. Hellmann, M. Morsey, P. Van Kleef, S. Auer, et al., Dbpedia—a large-scale, multilingual knowledge base extracted from wikipedia, Semantic web 6 (2015) 167–195.
[21] I. Diliso, R. Barile, C. d'Amato, N. Fanizzi, JediKG: Return of the Schema - Building Complete Datasets for Machine Learning and Reasoning on Knowledge Graphs, in: International Semantic Web Conference, Springer, 2026. "To Appear".