TL;DR
A knowledge graph stores facts as triples, but almost every real graph is missing many facts. A knowledge graph embedding gives each entity and each relation a short vector of numbers, then scores a triple (subject, relation, object) with a simple formula built from those vectors. Training makes true triples score higher than false ones, so the model can rank the facts that are probably missing.
Builds on: Knowledge Graph, Vector, Dot Product · Leads to: DistMult, Link Prediction
Where it's used
Completing large catalogues. Public knowledge bases such as Wikidata hold millions of entities and relations, and most entities lack some of the facts that similar entities have. An embedding model proposes the most likely missing triples, and a human editor or a rule checks them before they are added.
Entity search and disambiguation. A search system that sees the name "Paris" needs to know whether the query means the city or a person. Entity vectors that place related entities close together help rank the candidates, because the graph neighbourhood of each candidate is compressed into its vector.
Recommendation with side information. A shopping graph links users, products, brands and categories. Treating "bought", "viewed" and "made by" as relations lets the same scoring idea rank products for a user, using the same machinery as link prediction.
How it works
Take a tiny catalogue with three entities and one relation. The triples we know are (Ben, lives in, Leeds) and (Ana, lives in, ?). We want to know whether Ana lives in Leeds or in Paris. Each entity and relation has a 2-dimensional vector. Training has produced these values:
- Ben = (1, 0)
- Ana = (1, 0.2)
- Leeds = (1, 1)
- Paris = (3, 3)
- lives in = (0, 1)
The score uses a translation idea: the subject's vector plus the relation's vector should land close to the object's vector. If the triple is true, the result should be near the object.
flowchart LR T["Triple: subject, relation, object"] --> V["Look up three vectors"] V --> A["Add subject and relation"] A --> D["Measure distance to object"] D --> S["Score = minus distance"] S --> R["Rank candidate objects"]
- Look up the vectors. For the query (Ana, lives in, ?), the subject is Ana = (1, 0.2) and the relation is lives in = (0, 1). Check: each vector has two numbers.
- Add the subject and relation. Ana plus lives in is . This is where the model expects the object to be.
- Measure the distance to each candidate. The distance to Leeds = (1, 1) is . The distance to Paris = (3, 3) is . Check: Leeds is closer.
- Turn distance into a score. Use score = minus distance, so a larger score means a more plausible triple. Leeds scores and Paris scores about . Check: the higher score belongs to Leeds.
- Rank the candidates. Sort the candidate objects by score. The top answer is Leeds, and the model predicts (Ana, lives in, Leeds). Check: the prediction comes from the graph's own structure, not from a text rule.
The model did not read any sentence about Ana. It learned that people who live in Leeds sit at the point that "lives in" moves them to, and Ana's vector sits where that rule places her.
The math
A knowledge graph is a set of triples , where and are entities and is a relation. The model gives each entity a vector and each relation a vector . The score of a triple is a function of these vectors, and the lesson uses the translation score, which is the negative distance:
Here is the Euclidean length (the straight-line distance between two points), so the score is 0 when the translated subject lands exactly on the object.
Step 2 in symbols: the translation
The vector is the point where the relation sends the subject. In the example, and , so . Each coordinate is added separately, which is the same rule as vector addition.
Step 3 in symbols: the ranking
For a query , the model computes for every candidate and sorts them. In the example the distances are and .
Step 4 in symbols: training with a margin
Training needs to push true triples above false ones. The false ones are not given; they come from corrupting a true triple by replacing its object (or subject) with a random entity. Write for the distance of a true triple and for the distance of its corrupted version. The margin loss is
where is the margin, a number chosen in advance. The loss is zero when the corrupted triple is at least farther away than the true one. This is the same shape as the Hinge Loss used for support vector machines. Training runs Gradient Descent on over all sampled pairs, adjusting the entity and relation vectors.
Worked examples
Use the vectors above. Ben = (1, 0), lives in = (0, 1), Leeds = (1, 1).
- Compute Ben plus lives in: .
- The distance to Leeds is . The triple (Ben, lives in, Leeds) is true, and its score is .
- Suppose Paris = (3, 3). The distance is , so the score is about .
- The true object scores higher, so the ranking puts Leeds first.
Answer: the ranking is Leeds, then Paris, and the score for the true triple is 0.
Use the margin . The positive triple is (Ben, lives in, Leeds), and the corrupted triple replaces Leeds with Ana, giving (Ben, lives in, Ana). Ben = (1, 0), lives in = (0, 1), Leeds = (1, 1), Ana = (1, 0.2).
- Positive distance: , and the distance to Leeds is 0. So .
- Negative distance: the distance from (1, 1) to Ana = (1, 0.2) is . So .
- Loss: . The corrupted triple is too close, so the loss is positive.
- Choose the direction. The loss falls when the corrupted distance grows, so the update moves Ana away from . Here points from Ana up to (1, 1), so the direction away from (1, 1) is .
- Take a step of size along that direction: Ana moves from (1, 0.2) to (1, 0.1). The new distance is 0.9, and the new loss is .
Answer: one step halved the loss from 0.2 to 0.1, by moving the false triple's object away from where the relation sends Ben.
Suppose a catalogue has 50,000 entities and 1,000 relations, and each vector has 200 numbers.
- The model stores numbers.
- A table that stores a yes or no answer for every possible triple would need entries, most of them unknown or false.
- To answer a query (h, r, ?), the model computes 50,000 distances, each a sum over 200 coordinates. That is about 10 million additions, which is quick on a modern computer.
Answer: the vectors take about 10 million numbers against a table of about 2.5 trillion entries, and a query is one pass over the candidates.
Try it: a catalogue has 20,000 entities, 300 relations and vectors of length 100. How many numbers does the model store? Answer: .
Common mistakes and tradeoffs
It is tempting to think a high score means the fact is true. In fact the score only says the triple looks like the facts that were given. Scores rank candidates; they are not proof, which is why the ranked list goes to a person or a rule before it is added to the graph.
It is tempting to think a triple that is not in the graph is false. In fact the graph is incomplete, so a missing triple is unknown. Negative sampling treats unseen triples as negatives for training, which is a convenient assumption that can push down facts that are simply not yet recorded.
It is tempting to think the translation score handles every relation. In fact it treats the relation as a shift, so it is awkward for relations that are symmetric, such as "is married to". For those, a score that multiplies entries, such as the one in DistMult, is often used instead.
Use knowledge graph embeddings when the graph is large and incomplete, and when you need a ranked list of likely facts. Do not use them when the graph is small and the rules are few and exact, since a logical rule gives a reason that a person can check. Alternatives include rule mining, path-based ranking, and graph neural networks for Link Prediction.
History
Graph-shaped knowledge bases are older than the word "embedding". Semantic networks in the 1960s and 1970s stored facts as labelled links, and later systems stored triples in databases. Google introduced its Knowledge Graph in 2012, which brought the term into wide use for large public graphs.
Learned representations for words arrived around the same time. The word2vec work of 2013 showed that a model trained to predict neighbouring words gives useful vectors, which led to the idea of word embeddings. In 2013, Bordes and colleagues applied a translation idea to knowledge bases in a model called TransE, which made the simple vector-addition score popular. In 2015 Yang and colleagues proposed DistMult, a multiplicative score that worked well on symmetric relations. Later models added complex-valued and rotation-based scores to handle more relation patterns.
Recap
- A knowledge graph stores facts as triples, and most real graphs are incomplete.
- An embedding model gives each entity and relation a vector and scores a triple from those vectors, for example by the distance from subject plus relation to object.
- Training uses true triples and corrupted ones, with a margin loss that pushes true triples above false ones.
- The result is a ranked list of likely missing facts, which needs checking before it is trusted.
Read next: DistMult for a multiplicative score, and Link Prediction for the general task that these models solve.
Check yourself
QuestionWhat does a knowledge graph embedding give each entity and each relation?Show answer
A vector of numbers. The score of a triple is then computed from these vectors, so the model can rank facts that are not yet in the graph.
QuestionWith subject vector (1, 0), relation vector (0, 1) and object vector (1, 1), what is the translation score of the triple?Show answer
The subject plus the relation is (1, 1), and the distance to the object (1, 1) is 0. The score is .
QuestionWhy does training use corrupted triples, and why does the margin loss use a margin?Show answer
A knowledge graph lists only true facts, so the model needs false examples to learn from. Corrupted triples provide them. The margin says how much farther a false triple must be than a true one before the loss is zero, which keeps the model from stopping as soon as the true triple is merely ahead.
QuestionA user says the model gives a high score to (Ana, lives in, Paris), so Ana must live in Paris. What is wrong with this?Show answer
A high score means the triple looks like the given facts, not that it is true. The graph is incomplete, so the ranking is a suggestion to check, not a verified fact.