skills/torchdrug/references/knowledge_graphs.md
The official TorchDrug 0.2.1 reasoning tutorial covers two workflows:
Both use tasks.KnowledgeGraphCompletion.
Documented knowledge graph datasets:
FB15k: 14,951 entities, 1,345 relations, 592,213 tripletsFB15k237: 14,541 entities, 237 relations, 310,116 tripletsWN18: 40,943 entities, 18 relations, 151,442 tripletsWN18RR: 40,943 entities, 11 relations, 93,003 tripletsHetionet: 45,158 entities, 24 relations, 2,025,177 tripletsUse predefined splits:
from torchdrug import datasets
dataset = datasets.FB15k237("~/kg-datasets/")
train_set, valid_set, test_set = dataset.split()
import torch
from torchdrug import core, models, tasks
model = models.RotatE(
num_entity=dataset.num_entity,
num_relation=dataset.num_relation,
embedding_dim=2048,
max_score=9,
)
embedding_dim=2048 follows the tutorial and may be reduced for memory or speed.
task = tasks.KnowledgeGraphCompletion(
model,
num_negative=256,
adversarial_temperature=1,
)
num_negative controls negative samples per positive.adversarial_temperature enables score-weighted negative sampling.optimizer = torch.optim.Adam(task.parameters(), lr=2e-5)
solver = core.Engine(
task,
train_set,
valid_set,
test_set,
optimizer,
batch_size=1024,
)
solver.train(num_epoch=200)
solver.evaluate("valid")
Add gpus=[0] for a supported CUDA device. Reduce the epoch count for smoke
tests.
NeuralLP learns weighted chain-like rules up to a configured maximum length.
model = models.NeuralLP(
num_relation=dataset.num_relation,
hidden_dim=128,
num_step=3,
num_lstm_layer=2,
)
task = tasks.KnowledgeGraphCompletion(
model,
fact_ratio=0.75,
num_negative=256,
sample_weight=False,
)
fact_ratio=0.75 reserves 75% of training facts for the background graph used
for reasoning.
optimizer = torch.optim.Adam(task.parameters(), lr=1e-3)
solver = core.Engine(
task,
train_set,
valid_set,
test_set,
optimizer,
batch_size=64,
)
solver.train(num_epoch=10)
solver.evaluate("valid")
Embedding models:
models.TransEmodels.DistMultmodels.ComplExmodels.SimplEmodels.RotatEGraph-attention model:
models.KBGATVerify each constructor in the model API. Do not transfer argument names from PyKEEN, DGL-KE, or PyTorch Geometric.
KnowledgeGraphCompletion owns:
Important constructor options include:
criterionmetricnum_negativemarginadversarial_temperaturestrict_negativefact_ratiosample_weightfull_batch_evalTorchDrug 0.2.1 added full-batch evaluation support. Choose it according to graph size and available memory.
Use filtered ranking metrics:
Filtered evaluation removes other known true triples before ranking. Preserve training, validation, and test facts exactly as the task expects to avoid leakage or incorrect filtering.
Also report:
Hetionet supports biomedical link-prediction experiments, but a high model score does not establish a new treatment, causal mechanism, or validated association.
For drug-repurposing analysis:
TorchDrug's generic KnowledgeGraphCompletion API does not automatically apply
biomedical type constraints or causal interpretation.
Build model sizes from dataset.num_entity and dataset.num_relation.
Lower batch size or disable full-batch evaluation. Reducing negative samples mainly affects training, not the size of all-entity ranking.
Use num_relation=dataset.num_relation and let
KnowledgeGraphCompletion.preprocess() construct the fact graph.
Check for inverse-relation leakage, duplicate triples, accidental use of test facts, and raw rather than filtered ranking.