| name | link-prediction-api |
| description | Use a pre-trained KGE model for inference via the KGE class. Use when loading a trained model, predicting missing links (head/relation/tail), answering multi-hop EPFO queries (1p 2p 3p 2i 3i ip pi 2u up), predicting literal values, accessing raw embeddings, or deploying with the Gradio UI. |
Use a Pre-Trained KGE Model for Inference
When to Use
- Loading a trained model to predict missing triples
- Running 1p / 2p / 3p / intersection / union / negation queries
- Extracting entity or relation embeddings
- Predicting literal values (numerical attributes) with
train_literals
- Deploying the model as a web UI via
index_serve.py
Reference Implementation
dicee/knowledge_graph_embeddings.py — KGE class
dicee/abstracts.py — mixins BaseInteractiveKGE, InteractiveQueryDecomposition, BaseInteractiveTrainKGE (source of is_seen, sample_entity, train_literals, etc.)
dicee/scripts/index_serve.py — Gradio deployment
1. Load a Pre-Trained Model
from dicee import KGE
model = KGE(path="Experiments/2024-01-01_12-00/")
model = KGE(path="MyRun/")
The path directory must contain:
model.pt — model weights
configuration.json — training config
entity_to_idx.csv — entity index mapping
relation_to_idx.csv — relation index mapping
2. Predict Missing Links — predict_topk
predict_topk infers the missing element in a triple. Supply exactly two of h, r, t.
results = model.predict_topk(h=["Alice"], r=["knows"], topk=5)
results = model.predict_topk(r=["knows"], t=["Bob"], topk=5)
results = model.predict_topk(h=["Alice"], t=["Bob"], topk=5)
results = model.predict_topk(h=["Alice", "Dave"], r=["knows", "friendOf"], topk=3)
results = model.predict_topk(h=["Alice"], r=["knows"],
within=["Bob", "Carol", "Eve"], topk=3)
Notes:
- Scores are sigmoid-normalised (0–1)
batch_size controls memory during inference (default: 1024)
- Input strings must match the KG's entity/relation vocabulary exactly
3. Check Vocabulary
model.is_seen(entity="Alice")
model.is_seen(relation="knows")
model.sample_entity(n=10)
len(model.entity_to_idx)
len(model.relation_to_idx)
4. Access Raw Embeddings
vecs = model.get_transductive_entity_embeddings(
indices=["Alice", "Bob"],
as_list=True
)
vecs = model.get_transductive_entity_embeddings(
indices=["Alice", "Bob"],
as_numpy=True
)
vecs = model.get_transductive_entity_embeddings(
indices=["Alice", "Bob"],
as_pytorch=True
)
5. Multi-Hop Query Answering — answer_multi_hop_query
Answers EPFO (Existential Positive First-Order) queries including conjunctions, disjunctions, and negations.
Supported Query Types
query_type | Pattern | Meaning |
|---|
"1p" | (e, (r,)) | Single hop: ?x : r(e, x) |
"2p" | (e, (r1, r2)) | Two hops: ?x : ∃y. r1(e,y) ∧ r2(y,x) |
"3p" | (e, (r1, r2, r3)) | Three hops |
"2i" | ((e1,(r1,)), (e2,(r2,))) | Intersection: ?x : r1(e1,x) ∧ r2(e2,x) |
"3i" | three conjuncts | Three-way intersection |
"ip" | ((2i_query), (r,)) | Intersection then hop |
"pi" | ((2p_query), (e2,(r2,))) | Hop then intersection |
"2in" | with negation "n" | Intersection with negation |
"2u" | with union "u" | Disjunction |
"up" | union then hop | Union followed by projection |
Examples
results = model.answer_multi_hop_query(
query_type="1p",
query=("Alice", ("knows",)),
k=10, tnorm="min"
)
results = model.answer_multi_hop_query(
query_type="2p",
query=("Alice", ("knows", "knows")),
k=10, tnorm="prod"
)
results = model.answer_multi_hop_query(
query_type="2i",
query=(("Alice", ("knows",)), ("Bob", ("knows",))),
k=10, tnorm="min"
)
results = model.answer_multi_hop_query(
query_type="1p",
queries=[
("Alice", ("knows",)),
("Bob", ("friendOf",)),
],
k=5, tnorm="prod"
)
Parameters
| Param | Type | Description |
|---|
query_type | str | One of: 1p 2p 3p 2i 3i ip pi 2in 3in inp pin pni 2u up |
query | tuple | Nested tuple encoding the query (see pattern table above) |
queries | list | List of query tuples for batch evaluation |
tnorm | str | T-norm for conjunction: "prod" (product) or "min" (Gödel) |
neg_norm | str | Negation norm: "standard", "sugeno", or "yager" |
k | int | Top-k answer entities to return |
use_logits | bool | Use raw logits (default: True) vs sigmoid probabilities |
6. Literal Prediction
Train a linear regression head on top of entity embeddings to predict numerical attribute values.
model.train_literals(train_file_path="KGs/DBpedia/literals_train.tsv")
predictions = model.predict_literals(
entity=["Berlin", "Paris"],
attribute=["population", "area"]
)
7. Evaluate on a Custom Dataset
from dicee import KGE
model = KGE(path="MyRun/")
triples = [
("Alice", "knows", "Bob"),
("Carol", "livesIn", "Berlin"),
]
metrics = model.eval_lp_performance(dataset=triples, filtered=True)
print(metrics)
8. Move Model to Device
model.to("cpu")
model.to("cuda")
model.to("cuda:1")
9. Deploy as a Web UI
Use the built-in Gradio server:
python -m dicee.scripts.index_serve --path "MyRun/"
This launches a Gradio interface at http://localhost:7860 with:
- Subject / Predicate / Object fields
- 1vsAll scoring (fill two fields, leave one blank)
- Top-k entity scores displayed
10. Common Errors
| Error | Cause | Fix |
|---|
KeyError: 'Alice' | Entity not in vocabulary | Check model.is_seen(entity="Alice") |
AssertionError: k <= num_entities | topk exceeds vocab size | Reduce topk |
RuntimeError: model.pt not found | Wrong path | Verify path contains model.pt |
FileNotFoundError: entity_to_idx | Missing index files | Retrain or restore from backup |
| Query returns empty results | Wrong query structure | Check query tuple nesting against pattern table |