← Về thư mục
📄 / / profiles / r-and-d / raw_evidence / scientific-agent-skills / skills / torchdrug / references / knowledge_graphs.md

Knowledge Graph Reasoning

The official TorchDrug 0.2.1 reasoning tutorial covers two workflows:

Both use tasks.KnowledgeGraphCompletion.

Datasets

Documented knowledge graph datasets:

Use predefined splits:

from torchdrug import datasets

dataset = datasets.FB15k237("~/kg-datasets/")
train_set, valid_set, test_set = dataset.split()

RotatE embedding workflow

Model

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

task = tasks.KnowledgeGraphCompletion(
    model,
    num_negative=256,
    adversarial_temperature=1,
)

Train and evaluate

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 workflow

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")

Other documented models

Embedding models:

Graph-attention model:

Verify each constructor in the model API. Do not transfer argument names from PyKEEN, DGL-KE, or PyTorch Geometric.

Task behavior

KnowledgeGraphCompletion owns:

Important constructor options include:

TorchDrug 0.2.1 added full-batch evaluation support. Choose it according to graph size and available memory.

Evaluation

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:

Biomedical use

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:

  1. define the exact relation being predicted,
  2. preserve entity and relation type constraints,
  3. exclude known positives correctly,
  4. check for train/test leakage through inverse or duplicate relations,
  5. calibrate or rank model scores,
  6. validate candidates against independent evidence and domain experts.

TorchDrug's generic KnowledgeGraphCompletion API does not automatically apply biomedical type constraints or causal interpretation.

Common failures

Entity/relation mismatch

Build model sizes from dataset.num_entity and dataset.num_relation.

Evaluation out of memory

Lower batch size or disable full-batch evaluation. Reducing negative samples mainly affects training, not the size of all-entity ranking.

NeuralLP produces invalid shapes

Use num_relation=dataset.num_relation and let KnowledgeGraphCompletion.preprocess() construct the fact graph.

Inflated metrics

Check for inverse-relation leakage, duplicate triples, accidental use of test facts, and raw rather than filtered ranking.