CS224W

Lecture 10 · Thu 10/22 · slides PDF · lecture notes

Knowledge Graph Embeddings

Complete a knowledge graph by embedding entities and relations so that a relation acts as a geometric operation — translation, projection, scaling or rotation — and pick the geometry that matches the logic of your relations.

27
sectionstick "got it" as you go
22
figuresredrawn from the slides
interactive figuresplay with the ideas
5
self-check questionsplus flashcards
18′
reading time4,007 words, walkthrough aside

Before you start: review of the earlier sessions

The thread so far. Session 9 added relation types to GNNs; session 10 shows that for huge, feature-poor knowledge graphs a shallow embedding with the right geometry is still the workhorse. Session 15 will ask how to make this inductive.

What this session takes from earlier ones

  • Lecture 9: relation types: a KG is a heterogeneous graph with many relations and entities
  • Lecture 2: shallow embeddings: KG embeddings are the same encoder–decoder template with a relation-aware decoder
  • Lecture 9: link prediction per relation and tail-corruption negatives

Those sessions in one minute each

Lecture 9 · Heterogeneous Graphs

Treat each relation type (source type, edge type, target type) as its own channel of messages, aggregate within and then across channels — and share or factorise weights so the parameter count doesn't grow with every new relation.

\[ \mathbf{W}_r=\sum_{b=1}^{B}a_{rb}\,\mathbf{V}_b \qquad\text{(basis decomposition: } |R|\cdot B \text{ scalars} + B \text{ shared matrices)} \]

Lecture 2 · Node Embeddings

Define similarity by co-occurrence on short random walks, then learn one vector per node so that dot products reproduce it — fast with negative sampling, but just a lookup table.

\[ \mathcal{L}=\sum_{u\in V}\sum_{v\in N_R(u)}\Big[-\log\sigma(\mathbf{z}_u^\top\mathbf{z}_v)-\sum_{i=1}^{k}\log\sigma(-\mathbf{z}_u^\top\mathbf{z}_{n_i})\Big] \]

\(N_R(u)\): multiset of nodes visited by walks from \(u\) under strategy \(R\) (uniform for DeepWalk, \(p,q\)-biased for node2vec); \(n_i\): negatives sampled ∝ degree; \(\sigma\): sigmoid.

Warm-up: answer these before you read on

What problem do basis and block-diagonal decompositions solve in R-GCN? from Lecture 9

With many relations, one full weight matrix per relation overfits and is expensive; sharing a few basis matrices (or restricting to blocks) cuts parameters.

What do p and q control in node2vec? from Lecture 2

The return parameter p penalises stepping back to the previous node; the in-out parameter q biases towards staying close (BFS, small q favours DFS). They trade off local versus structural similarity.

How are negatives chosen for heterogeneous link prediction? from Lecture 9

Corrupt the tail (or head) of a true triple with a node of the correct type for that relation, so the negative is plausible.

Give the three limits of shallow embeddings. from Lecture 2

No parameter sharing (one vector per node), no use of node features, and no embedding for nodes unseen at training time (transductive).

More questions from every session you have reached, with spaced repetition: the cumulative review.

In short

A knowledge graph (KG) stores facts as triples \((h,r,t)\): head entity, relation, tail entity — (Obama, nationality, USA). KGs (Freebase, Wikidata, Google's, Amazon's) are massive and badly incomplete: in Freebase 93.8% of persons have no place of birth. KG completion asks: given \((h,r,?)\), predict the missing tail.

The approach here is shallow (no GNN): give each entity and each relation an embedding, and a score function \(f_r(h,t)\) that is high for true triples. The magic is in the geometry: in TransE a relation is a translation (\(\mathbf h+\mathbf r\approx\mathbf t\)); TransR projects into a relation-specific space first; DistMult uses a diagonal bilinear form; ComplEx moves to complex numbers; RotatE makes a relation a rotation in the complex plane.

Each geometry can or cannot express certain relation patterns — symmetry, antisymmetry, inversion, composition, 1-to-N. Reading the expressiveness table is the main skill of this lecture: it tells you which model your KG needs.

The picture: four geometries of a relation

TransE, TransR, DistMult, RotatE TransE: h + r ≈ t h (Obama) r (nationality) t (USA) 1 TransR: M_r h + r ≈ M_r t entity space ℝᵈ relation space ℝᵏ h t M_r 2 DistMult: ⟨h, r, t⟩ = Σᵢ hᵢ rᵢ tᵢ h ⊙ r t₁ (high) t₂ (low) symmetric in h and t! 3 RotatE: h ∘ r ≈ t, |rᵢ| = 1 ReIm hᵢ tᵢ θᵣ 4

How to read it (hover a step)

  1. TransE: a relation is a translation vector. Score \(f_r(h,t)=-\|\mathbf h+\mathbf r-\mathbf t\|\).
  2. TransR: each relation has its own space; project entities with \(\mathbf M_r\), then translate. Different entities can collapse to the same point in relation space.
  3. DistMult: score = cosine-like agreement between \(\mathbf h\odot\mathbf r\) and \(\mathbf t\). Because multiplication commutes, \(f_r(h,t)=f_r(t,h)\).
  4. RotatE: in each complex coordinate, the relation rotates the head by an angle \(\theta_r\); composition = adding angles.

1 · Knowledge graphs and the completion task

A KG captures entities (nodes, labelled with types) and relationships (typed edges) — a heterogeneous graph. Examples: bibliographic networks (paper, author, conference, year; pubWhere, hasAuthor, cite), biomedical KGs (drug, disease, protein, pathway; treats, causes, has_func). In industry: Google Knowledge Graph, Amazon Product Graph, Microsoft Satori, LinkedIn, IBM Watson — they serve search answers ("movies directed by James Cameron").

Public KGs: Freebase (~80M entities, ~38K relation types, ~3B triples), Wikidata, DBpedia, YAGO, NELL. Benchmarks: FB15k / FB15k-237 (Freebase subsets), WN18RR (WordNet).

Freebase: massive and incomplete. slide 11 ↗

KG completion: for a given (head, relation), predict missing tails — e.g. (J.K. Rowling, genre, ?) → Science Fiction. Slightly different from link prediction: the query fixes the head and relation and ranks all candidate tails.

KG completion: predict the missing tail. slide 13 ↗

Representation: triples \((h,r,t)\). Entities and relations get shallow embeddings in \(\mathbb{R}^k\) (no GNN here!). Goal: the embedding of \((h,r)\) should be close to the embedding of \(t\). Two design questions: how to embed \((h,r)\), and how to define the score \(f_r(h,t)\).

2 · Relation patterns: the logic a model must express

PatternLogical formExample
Symmetric\(r(h,t)\Rightarrow r(t,h)\)Roommate, Family
Antisymmetric\(r(h,t)\Rightarrow\neg r(t,h)\)Hypernym (poodle → dog)
Inverse\(r_2(h,t)\Rightarrow r_1(t,h)\)Advisor / Advisee
Composition\(r_1(x,y)\wedge r_2(y,z)\Rightarrow r_3(x,z)\)My mother's husband is my father
1-to-N\(r(h,t_1),\dots,r(h,t_n)\) all trueStudentsOf

A model "can model" a pattern if there exist embeddings that make the score consistent with it for all entities.

Four relation patterns. slide 23 ↗

3 · TransE (Bordes et al., 2013)

Intuition: translation, \(\mathbf h+\mathbf r\approx\mathbf t\) if the fact holds. Recall word2vec's "king − man + woman ≈ queen": the "female" direction is a translation.

Training (Algorithm 1): initialise uniformly and normalise; sample a minibatch of true triples; for each, corrupt the head or tail into a triple \((h',r,t')\) not in the KG; minimise the margin (contrastive) loss

Learning TransE: corrupted triples and a margin loss. slide 21 ↗
\[ \sum\big[\gamma+d(\mathbf h+\mathbf r,\mathbf t)-d(\mathbf h'+\mathbf r,\mathbf t')\big]_+ \]

which pushes true triples closer than corrupted ones by at least the margin \(\gamma\).

  • ✓ Antisymmetric: \(\mathbf h+\mathbf r=\mathbf t\) but \(\mathbf t+\mathbf r\ne\mathbf h\).
  • ✓ Inverse: set \(\mathbf r_1=-\mathbf r_2\).
  • ✓ Composition: \(\mathbf r_3=\mathbf r_1+\mathbf r_2\).
  • ✗ Symmetric: \(\mathbf h+\mathbf r=\mathbf t\) and \(\mathbf t+\mathbf r=\mathbf h\) force \(\mathbf r=0,\ \mathbf h=\mathbf t\).
  • ✗ 1-to-N: \(\mathbf t_1=\mathbf h+\mathbf r=\mathbf t_2\), contradiction for distinct tails.

4 · TransR (Lin et al., 2015)

TransE translates every relation in the same space. TransR gives each relation its own space: entities live in \(\mathbb{R}^d\), relation \(\mathbf r\in\mathbb{R}^k\) with projection \(\mathbf M_r\in\mathbb{R}^{k\times d}\). Project \(\mathbf h_\perp=\mathbf M_r\mathbf h,\ \mathbf t_\perp=\mathbf M_r\mathbf t\); score \(f_r(h,t)=-\|\mathbf h_\perp+\mathbf r-\mathbf t_\perp\|\).

  • ✓ Symmetric: \(\mathbf r=0\) and \(\mathbf M_r\mathbf h=\mathbf M_r\mathbf t\) — distinct in entity space, same point in relation space.
  • Symmetric and 1-to-N relations in TransR. slide 33 ↗
  • ✓ Antisymmetric, ✓ inverse (\(\mathbf r_2=-\mathbf r_1,\ \mathbf M_{r_1}=\mathbf M_{r_2}\)).
  • ✓ 1-to-N: learn \(\mathbf M_r\) with \(\mathbf M_r\mathbf t_1=\mathbf M_r\mathbf t_2\) (\(\mathbf t_1-\mathbf t_2\in\ker\mathbf M_r\)).
  • ✓ Composition: a kernel argument (below).
Prove TransR can model composition. Hint: kernels.

Pick \(\mathbf g_i\) with \(\mathbf M_{r_i}\mathbf g_i=\mathbf r_i\). Then \(r_1(x,y)\) ⇔ \(\mathbf M_{r_1}(\mathbf y-\mathbf x)=\mathbf r_1\) ⇔ \(\mathbf y\in\mathbf x+\mathbf g_1+\ker\mathbf M_{r_1}\). Similarly \(\mathbf z\in\mathbf y+\mathbf g_2+\ker\mathbf M_{r_2}\). So \(\mathbf z\in\mathbf x+\mathbf g_1+\mathbf g_2+\ker\mathbf M_{r_1}+\ker\mathbf M_{r_2}\). Construct \(\mathbf M_{r_3}\) (same shape) with \(\ker\mathbf M_{r_3}=\ker\mathbf M_{r_1}+\ker\mathbf M_{r_2}\) — possible because the kernel only needs to be at least as large — and set \(\mathbf r_3=\mathbf M_{r_3}(\mathbf g_1+\mathbf g_2)\). Then \(\mathbf M_{r_3}\mathbf x+\mathbf r_3=\mathbf M_{r_3}\mathbf z\). ∎ (Intuition: compositions of affine maps are affine.)

5 · DistMult (Yang et al., 2015)

Switch from distances to bilinear scores: \(f_r(h,t)=\mathbf h^\top\mathbf A_r\mathbf t\). A full \(\mathbf A_r\) is too expressive (overfits), so restrict it to be diagonal:

\[ f_r(h,t)=\langle\mathbf h,\mathbf r,\mathbf t\rangle=\sum_i h_i\,r_i\,t_i \]

Read it as the cosine-like agreement between \(\mathbf h\odot\mathbf r\) (Hadamard product) and \(\mathbf t\): tails on the same side of the hyperplane orthogonal to \(\mathbf h\odot\mathbf r\) score positive.

  • ✓ 1-to-N: \(\langle\mathbf h,\mathbf r,\mathbf t_1\rangle=\langle\mathbf h,\mathbf r,\mathbf t_2\rangle\) is easy.
  • ✓ Symmetric: automatically, by commutativity.
  • ✗ Antisymmetric: \(f_r(h,t)=f_r(t,h)\) always — DistMult cannot tell head from tail; every relation is modelled as symmetric.
  • ✗ Inverse: would need \(\mathbf r_2=\mathbf r_1\) — "Advisor" and "Advisee" would share an embedding.
  • ✗ Composition.

6 · ComplEx and RotatE: going complex

ComplEx (Trouillon et al., 2016)

Empirical comparison (slide 62). slide 62 ↗

DistMult in \(\mathbb{C}^k\): \(f_r(h,t)=\mathrm{Re}\big(\sum_i h_i\,r_i\,\bar t_i\big)\). The complex conjugate on the tail breaks the head–tail symmetry. Expanding,

\[ f_r=\langle\mathrm{Re}\,h,\mathrm{Re}\,r,\mathrm{Re}\,t\rangle+\langle\mathrm{Re}\,h,\mathrm{Im}\,r,\mathrm{Im}\,t\rangle+\langle\mathrm{Im}\,h,\mathrm{Re}\,r,\mathrm{Im}\,t\rangle-\langle\mathrm{Im}\,h,\mathrm{Im}\,r,\mathrm{Re}\,t\rangle \]
  • ✓ Antisymmetric (conjugate makes \(f_r(h,t)\ne f_r(t,h)\) possible); ✓ symmetric (set \(\mathrm{Im}\,\mathbf r=0\)); ✓ inverse (\(\mathbf r_1=\bar{\mathbf r}_2\)); ✓ 1-to-N; ✗ composition.

RotatE (Sun et al., 2019)

TransE in complex space: \(\mathbf t\approx\mathbf h\circ\mathbf r\) with \(|r_i|=1\) (each \(r_i=e^{i\theta_{r,i}}\) is a rotation); score \(-\|\mathbf h\circ\mathbf r-\mathbf t\|\).

  • ✓ symmetric (\(\theta=0\) or \(\pi\)), ✓ antisymmetric, ✓ inverse (\(\theta_{r_1}=-\theta_{r_2}\)), ✓ composition (\(\theta_{r_3}=\theta_{r_1}+\theta_{r_2}\)); 1-to-N only in a weak sense (many tails equidistant).

7 · Cheat-sheet: which model for which KG?

ModelScoreEmbeddingSym.Antisym.Inv.Compos.1-to-N
TransE\(-\|\mathbf h+\mathbf r-\mathbf t\|\)\(\mathbb{R}^k\)✗✓✓✓✗
TransR\(-\|\mathbf M_r\mathbf h+\mathbf r-\mathbf M_r\mathbf t\|\)\(\mathbb{R}^d,\mathbb{R}^k,\mathbf M_r\)✓✓✓✓✓
DistMult\(\langle\mathbf h,\mathbf r,\mathbf t\rangle\)\(\mathbb{R}^k\)✓✗✗✗✓
ComplEx\(\mathrm{Re}\langle\mathbf h,\mathbf r,\bar{\mathbf t}\rangle\)\(\mathbb{C}^k\)✓✓✓✗✓
RotatE\(-\|\mathbf h\circ\mathbf r-\mathbf t\|\)\(\mathbb{C}^k,|r_i|=1\)✓✓✓✓(weak)

Practical advice (slide 61): KGs differ drastically in their relation patterns; no single embedding wins. Try TransE for a quick run if your KG has few symmetric relations; then more expressive ComplEx or RotatE. On FB15k-237, RotatE reaches MRR 0.338 / Hits@10 0.533; on WN18RR 0.476 / 0.571 — the best of the five in the slides' table.

The mathematics, step by step

A knowledge-graph embedding gives every entity a vector and turns every relation into an operation on vectors. One scoring function \(f_r(h,t)\) says how plausible the triple (head, relation, tail) is. The cards go through the loss and the four classic scoring functions, and for each one work out which relation patterns it can and cannot express, because that algebra is the content of the lecture.

Symbols used in this session

symbolmeaningwhat kind of object
\((h,r,t)\)a triple: head entity, relation, tail entitya fact in the knowledge graph
\(\mathbf h,\ \mathbf t\)embeddings of the head and tail entitiesvectors in \(\mathbb R^k\) (or \(\mathbb C^k\) for ComplEx and RotatE)
\(\mathbf r\)embedding of the relationvector in the same space
\(f_r(h,t)\)score of the triple: higher means more plausiblereal number
\(d(\cdot,\cdot)\)distance, \(d(\mathbf a,\mathbf b)=\lVert\mathbf a-\mathbf b\rVert\)non-negative number
\((h',r,t')\)a corrupted triple: head or tail replaced at random (a negative)a triple assumed false
\(\gamma\)margin: how much better a true triple must score than a corrupted onepositive number
\([x]_+\)\(\max(0,x)\)function
\(\bar t,\ \mathrm{Re}\)complex conjugate; real part of a complex numberoperations on complex numbers

1The margin loss

\[ \mathcal L=\sum_{(h,r,t)}\ \sum_{(h',r,t')}\big[\gamma+d(\mathbf h+\mathbf r,\mathbf t)-d(\mathbf h'+\mathbf r,\mathbf t')\big]_+ \]

In words. For every true triple and every corrupted version of it, pay a penalty unless the true triple's distance is smaller than the corrupted one's by at least the margin \(\gamma\).

Why it has this form. The graph only lists true facts, so negatives are manufactured by replacing the head or the tail with a random entity. The loss does not ask the true distance to be zero, only to beat the false one by \(\gamma\); once that holds, the bracket is negative, \([\cdot]_+\) makes it zero and the pair stops producing gradient. Training effort goes to the pairs that are still confused. Entity vectors are kept at length 1, otherwise the model could satisfy the margin by growing all vectors.

With numbers. \(\gamma=1\). True triple at distance \(0.5\). A corrupted triple at distance \(2.0\): \([1+0.5-2.0]_+=0\), already fine. A corrupted triple at distance \(1.2\): \([1+0.5-1.2]_+=0.3\), still penalised, although the true triple is already closer.

Watch out. A random corruption can accidentally be a true fact that is missing from the graph; evaluation therefore uses 'filtered' ranks that skip known true triples.

2TransE: a relation is a translation

\[ \mathbf h+\mathbf r\approx\mathbf t,\qquad f_r(h,t)=-\lVert\mathbf h+\mathbf r-\mathbf t\rVert \]

In words. Add the relation vector to the head: you should land on the tail. The score is minus the distance by which you miss.

Why it has this form. Read off the patterns from the equation. Antisymmetry: if \(\mathbf h+\mathbf r=\mathbf t\) then \(\mathbf t+\mathbf r\ne\mathbf h\) unless \(\mathbf r=\mathbf 0\), so yes. Inverse: \(\mathbf r_2=-\mathbf r_1\). Composition: \(\mathbf r_3=\mathbf r_1+\mathbf r_2\). Symmetry would need \(\mathbf h+\mathbf r=\mathbf t\) and \(\mathbf t+\mathbf r=\mathbf h\), which forces \(\mathbf r=\mathbf 0\) and \(\mathbf h=\mathbf t\): not expressible. One-to-many: \(\mathbf h+\mathbf r\) is a single point, so two different tails \(\mathbf t_1,\mathbf t_2\) of the same head and relation are forced to the same place: not expressible.

With numbers. \(\mathbf h=(1,0)\), \(\mathbf r=(1,2)\). The tail \(\mathbf t=(2,2)\) gives distance \(0\), score \(0\) (best possible). The tail \(\mathbf t'=(2,3)\) gives distance \(1\), score \(-1\). Composition: with \(\mathbf r_1=(1,2)\) and \(\mathbf r_2=(3,-1)\), applying one after the other is the single translation \((4,1)\).

Watch out. Because vector addition commutes, TransE cannot model relations where the order of composition matters; every pair of relations composes the same way in both orders.

3TransR: translate in a relation-specific space

\[ \mathbf h_\perp=\mathbf M_r\mathbf h,\quad \mathbf t_\perp=\mathbf M_r\mathbf t,\qquad f_r(h,t)=-\lVert\mathbf h_\perp+\mathbf r-\mathbf t_\perp\rVert \]

In words. First project both entities with a matrix that belongs to the relation, then apply TransE in the projected space.

Why it has this form. The projection lets different entities coincide for one relation while staying different for others. Symmetry: choose \(\mathbf r=\mathbf 0\) and let \(\mathbf M_r\) send \(\mathbf h\) and \(\mathbf t\) to the same point; yes. One-to-many: \(\mathbf M_r\) can map several tails \(\mathbf t_1,\mathbf t_2\) to the same \(\mathbf t_\perp\); yes. Antisymmetry and inverse work as in TransE. The cost is a \(k\times d\) matrix per relation on top of the vector.

With numbers. \(\mathbf M_r=\begin{pmatrix}1&0\end{pmatrix}\) keeps only the first coordinate. Tails \((2,5)\) and \((2,-7)\) both project to \(2\). With \(\mathbf h=(1,0)\) projecting to \(1\) and \(r=1\): \(1+1-2=0\) for both tails. One head, one relation, two correct tails.

Watch out. Each relation now lives in its own space, so composing two relations is no longer a simple sum of vectors.

4DistMult: a relation rescales coordinates

\[ f_r(h,t)=\langle\mathbf h,\mathbf r,\mathbf t\rangle=\sum_{i=1}^{k}h_i\,r_i\,t_i \]

In words. Multiply the three vectors coordinate by coordinate and add up. Equivalently: stretch the head by the relation, then take the dot product with the tail.

Why it has this form. \(\sum_ih_ir_it_i=(\mathbf h\odot\mathbf r)^\top\mathbf t\): the score is the cosine-like agreement between the rescaled head and the tail, so the true tails lie on one side of a hyperplane; many tails can score high, hence one-to-many yes. Multiplication of numbers commutes, \(h_ir_it_i=t_ir_ih_i\), so \(f_r(h,t)=f_r(t,h)\) always: symmetry yes, but antisymmetry and inverse no, since every relation is forced to be symmetric. Composition is not expressible either.

With numbers. \(\mathbf h=(1,2)\), \(\mathbf r=(3,-1)\), \(\mathbf t=(2,1)\): \(1\cdot3\cdot2+2\cdot(-1)\cdot1=6-2=4\). Swap head and tail: \(2\cdot3\cdot1+1\cdot(-1)\cdot2=4\). The model scores 'A is the parent of B' and 'B is the parent of A' identically.

Watch out. A good score on a benchmark does not contradict this: many benchmark relations are symmetric or are tested in one direction only.

5ComplEx and RotatE: complex numbers break the symmetry

\[ \text{ComplEx: } f_r(h,t)=\mathrm{Re}\Big(\sum_{i=1}^{k}h_i\,r_i\,\bar t_i\Big)\qquad\quad \text{RotatE: } \mathbf t\approx\mathbf h\circ\mathbf r,\ \ r_i=e^{i\theta_i},\quad f_r(h,t)=-\lVert\mathbf h\circ\mathbf r-\mathbf t\rVert \]

In words. ComplEx is DistMult with complex vectors and the tail conjugated. RotatE turns each complex coordinate of the head by an angle that belongs to the relation and asks to land on the tail.

Why it has this form. Conjugation treats head and tail differently, so swapping them changes the score when \(r_i\) has an imaginary part: antisymmetry and inverse (\(\mathbf r_2=\bar{\mathbf r}_1\)) become expressible; a purely real \(\mathbf r\) gives back the symmetric DistMult. In RotatE \(|r_i|=1\), so multiplying by \(r_i\) is a rotation by \(\theta_i\). Symmetric: \(\theta_i\in\{0,\pi\}\) (rotating twice returns to the start). Inverse: \(-\theta_i\). Composition: angles add, \(\theta_3=\theta_1+\theta_2\). Antisymmetric: any other angle. All four patterns at once.

With numbers. One complex coordinate, \(r=i\) (a quarter turn). ComplEx with \(h=1\), \(t=i\): \(h\,r\,\bar t=1\cdot i\cdot(-i)=1\), score \(+1\). Swapped, \(h=i\), \(t=1\): \(i\cdot i\cdot1=-1\), score \(-1\). RotatE: \(h=1\) rotated by \(r=i\) is \(i\); the inverse relation \(r=-i\) rotates \(i\) back to \(1\); the relation \(r=-1\) (half turn) is symmetric, since \(1\to-1\to1\).

Watch out. RotatE, like TransE, maps a head to exactly one point, so one-to-many relations remain a problem for it; ComplEx handles those but cannot compose relations.

Check yourself

Questions from the lecture, in order. Answer each one in your head (or on paper) before you open it. If one stumps you, follow the link to the walkthrough block it comes from.

Check yourself: why can't TransE use the "collapse" trick for symmetry? show answer

Its only map is the identity, which is injective, so distinct entities can never coincide.

from TransR: translate in a relation-specific space

Check yourself: on a KG where 80% of the edges are "is_a" (antisymmetric), would you try DistMult? show answer

No: it assigns (dog, is_a, poodle) the same score as (poodle, is_a, dog).

from What DistMult can and cannot express

Check yourself: compute one coordinate by hand with \(h=1+i\), \(r=i\), \(t=1\). show answer

\(h\,r\,\bar t=(1+i)i=-1+i\), real part \(-1\). Swap: \(t\,r\,\bar h=i(1-i)=1+i\), real part \(+1\). Different scores for the two directions.

from ComplEx: DistMult in complex space

Lecture walkthrough reference · slide by slide · opens on demand

The whole lecture in order, grouped by topic. Every drawing on the slides is redrawn here as a clean figure (click the step buttons where a figure has them), the heading of each block links to the matching pages of the official PDF, and the Review section after the walkthrough ties this session to the earlier ones. Read this and you do not need the deck.

  1. Recap: heterogeneous graphs and R-GCN
  2. What a knowledge graph is
  3. KGs are massive and incomplete
  4. The KG completion task and shallow embeddings
  5. Many KG embedding models; the plan
  6. TransE: a relation is a translation
  7. Relation patterns: what a model should be able to express
  8. What TransE can and cannot express
  9. TransR: translate in a relation-specific space
  10. TransR can model composition: the kernel argument
  11. DistMult: bilinear scoring
  12. What DistMult can and cannot express
  13. ComplEx: DistMult in complex space
  14. What ComplEx can and cannot express
  15. The full table, RotatE, and how to choose a model
  16. Summary, and a look ahead: reasoning with embeddings (from the notes)

Recap: heterogeneous graphs and R-GCN slides 3-4 ↗

Slide 3 recalls the previous lecture. A heterogeneous graph is a graph with multiple relation types. The example is the six-node directed graph A–F with edges coloured by relation \(r_1,r_2,r_3\) (B→A and D→A with \(r_1\), C→A with \(r_2\), and so on); A is the target node.

Slide 4 recalls R-GCN: learn from such a graph by using different neural network weights for different relation types. The figure shows A's computation graph: each edge passes through a box (a weight matrix) coloured by its relation, and the results are aggregated into A.

This lecture keeps the multi-relational setting but changes the tool. Instead of a GNN that computes node embeddings from neighbourhoods, it learns shallow embeddings (one free vector per entity and per relation) and asks a geometric question: what operation should a relation be, so that the logic of real relations can be expressed?

What a knowledge graph is slides 5-9 ↗

Slides 5–9: a knowledge graph stores entities with types and typed relations, here the bibliographic schema of slide 6. slides 5-9 ↗

Slide 5 defines a knowledge graph (KG): knowledge in graph form. It captures entities, types and relationships:

  • Nodes are entities.
  • Nodes are labelled with their types.
  • Edges between two nodes capture relationships between entities.

So a KG is an example of a heterogeneous graph. The small figure shows typed nodes connected by labelled arrows.

Two examples:

  • Slide 6, bibliographic network. Node types: paper, title, author, conference, year. Relation types: pubWhere, pubYear, hasTitle, hasAuthor, cite. The schema figure puts Paper in the centre: Paper –pubWhere→ Conference, Paper –hasTitle→ Title, Paper –hasAuthor→ Author, Paper –pubYear→ Year, and a self-loop Paper –cite→ Paper.
  • Slide 7, biomedical KG. Node types: drug, disease, adverse event, protein, pathways. Relation types: has_func, causes, assoc, treats, is_a. The figure is a graph of drugs, diseases and proteins with these labelled edges.

Slide 8, KGs in practice: Google Knowledge Graph, Amazon Product Graph, Meta Graph API, IBM Watson, Microsoft Satori, Project Hanover/Literome, LinkedIn Knowledge Graph, Yandex Object Answer.

Slide 9, application: serving information. The screenshot is a Bing search for "latest films by the director of Titanic". The search engine answers with a row of movie posters ("Movies featuring James Cameron"), which needs a chain of facts: Titanic → directed by → James Cameron → directed → films. That is a query over a KG, not keyword matching.

KGs are massive and incomplete slides 10-11 ↗

Slides 10–11: public KGs are massive and incomplete; the slide's Freebase figures. slides 10-11 ↗

Slide 10 lists publicly available KGs: FreeBase, Wikidata, DBpedia, YAGO, NELL, etc. Their common characteristics:

  • Massive: millions of nodes and edges.
  • Incomplete: many true edges are missing.

Given a massive KG, enumerating all possible facts is intractable. So the question is: can we predict plausible but missing links?

Slide 11 gives numbers for Freebase: about 80 million entities, about 38K relation types, about 3 billion facts (triples). And it is very incomplete: 93.8% of persons in Freebase have no place of birth, and 78.5% have no nationality.

Research datasets: FB15k / FB15k-237, a complete subset of Freebase used by researchers to learn KG models. The table on the slide:

  • FB15k: 14,951 entities, 1,345 relations, 592,213 total edges.
  • FB15k-237: 14,505 entities, 237 relations, 310,079 total edges.

Why FB15k-237 exists: FB15k contains many inverse pairs (if (h, r, t) is in test, (t, r⁻¹, h) is often in train), so a model could "cheat" by memorising inverses. FB15k-237 removes these near-duplicate and inverse relations, which is why it has only 237 relations.

The KG completion task and shallow embeddings slides 13-15 ↗

Slides 13–15: KG completion fixes a head and a relation and asks for the tail, scored with shallow embeddings. slides 13-15 ↗

Slide 13 defines the task. Given an enormous KG, can we complete it? For a given (head, relation), we predict missing tails. This is slightly different from the link prediction task: there we score a pair of nodes; here the query fixes the head and the relation and ranks all candidate tails.

The example graph has authors (J.R.R. Tolkien, Stephen King, J.K. Rowling, Alan Poe, C.S. Lewis, Lloyd Alexander), genres (Science Fiction, Fantasy, Tragicomedy) and two relations, genre and Influence. The dashed red edge is the missing relation: predict the tail "Science Fiction" for ("J.K. Rowling", "genre").

Slide 14 recalls the simplest encoder from Lecture 2, the "shallow" encoding: the encoder is just an embedding lookup. The embedding matrix \(\mathbf{Z}\) has one column per node (the embedding vector of a specific node); its number of rows is the embedding dimension.

Slide 15 sets up the KG representation:

  • Edges in a KG are triples \((h,r,t)\): head \(h\) has relation \(r\) with tail \(t\).
  • Key idea: model entities and relations in an embedding space \(\mathbb{R}^k\), and associate entities and relations with shallow embeddings. Note: we do not learn a GNN here.
  • Given a true triple \((h,r,t)\), the goal is that the embedding of \((h,r)\) should be close to the embedding of \(t\).
  • Two design questions: how to embed \((h,r)\)? How to define the score \(f_r(h,t)\)? The score \(f_r\) should be high if \((h,r,t)\) exists, and low otherwise.

Each model below is one answer to these two questions. Training is always contrastive: push true triples to high scores and corrupted triples to low scores.

Many KG embedding models; the plan slides 16-17 ↗

Slide 16: a decade of KG embedding models; above the axis the distance-based family, below the bilinear family. slide 16 ↗

Slide 16 is a timeline of KG embedding models from 2011 to 2020: above the axis, SE (2011), UM (2012), TransE (2013), TransH (2014), KG2E and TransR (2015), Poincaré (2017), TorusE (2018), RotatE (2019), BoxE (2020); below it, RESCAL (2011), NTN (2013), SME (2014), DistMult (2015), ComplEx (2016), ConvE and R-GCN (2018), TuckER (2019). Colour roughly separates distance/translation models (blue), bilinear/factorisation models (green) and neural models (orange).

Slide 17: we will study several of these shallow, transductive models. They differ in two ways:

  • they are based on different geometric intuitions;
  • they capture different types of relations: they have different expressivity.

The slide previews the summary table that the lecture fills in step by step (columns: model, score, embedding space, then whether it can model symmetric, antisymmetric, inverse, composition and 1-to-N relations). The full table is on slide 60.

"Transductive" means the model can only score entities it saw during training: a new entity has no embedding. That is the price of shallow embeddings (compare with inductive GNNs, and with KG foundation models in Lecture 15).

TransE: a relation is a translation slides 19-21 ↗

Slides 19–20: TransE models a relation as a translation, the same idea as the king − man + woman = queen parallelogram. slides 19-20 ↗
Slide 21, Algorithm 1 of the TransE paper: corrupted triples and a margin loss. slide 21 ↗

Slide 19 [Bordes et al., NeurIPS 2013]. Intuition: translation. For a triple \((h,r,t)\), let \(\mathbf{h},\mathbf{r},\mathbf{t}\in\mathbb{R}^k\) be embedding vectors (bold = embedding). TransE wants

\[ \mathbf{h}+\mathbf{r}\approx\mathbf{t}\ \text{ if the link exists},\qquad \mathbf{h}+\mathbf{r}\neq\mathbf{t}\ \text{ otherwise}. \]

Scoring function:

\[ f_r(h,t)=-\|\mathbf{h}+\mathbf{r}-\mathbf{t}\|. \]

The figure: Obama + Nationality lands on U.S.A.

Slide 20 shows where the idea comes from: word embeddings. The parallelogram king → queen and man → woman: the "female" arrow is the same vector in both places, and the "royal" arrow (man → king, woman → queen) is also shared. A relation is a constant offset between entity vectors.

Slide 21, how to learn TransE (Algorithm 1 of the paper). Input: training set \(S=\{(h,r,t)\}\), entity set \(E\), relation set \(R\), margin \(\gamma\), embedding dimension \(k\).

  1. Initialise each relation \(\mathbf{r}\leftarrow\mathrm{uniform}(-\frac{6}{\sqrt{k}},\frac{6}{\sqrt{k}})\) and normalise \(\mathbf{r}\leftarrow\mathbf{r}/\|\mathbf{r}\|\); initialise each entity \(\mathbf{e}\) uniformly in the same range.
  2. Loop: normalise each entity \(\mathbf{e}\leftarrow\mathbf{e}/\|\mathbf{e}\|\); sample a minibatch \(S_{batch}\) of size \(b\).
  3. For each \((h,r,t)\in S_{batch}\), sample a corrupted triplet \((h',r,t')\) that does not appear in the KG (replace the head or the tail), and add the pair \(((h,r,t),(h',r,t'))\) to \(T_{batch}\).
  4. Update embeddings by gradient steps on the contrastive (margin) loss \[ \sum_{((h,r,t),(h',r,t'))\in T_{batch}}\big[\gamma+d(\mathbf{h}+\mathbf{r},\mathbf{t})-d(\mathbf{h}'+\mathbf{r},\mathbf{t}')\big]_+ , \] where \(d\) is the distance (negative of the score) and \([x]_+=\max(0,x)\).

The loss favours lower distance (higher score) for valid triplets and higher distance (lower score) for corrupted ones. It is zero once each true triple is closer than its corrupted partner by at least the margin \(\gamma\).

Why normalise entities each step? Without it, the loss could be reduced trivially by making all entity norms huge, which spreads corrupted triples far apart without learning anything.

Relation patterns: what a model should be able to express slides 22-23 ↗

Slides 22–23: the four relation patterns a KG embedding should be able to express. slides 22-23 ↗

Slide 22: relations in a heterogeneous KG have different properties. Examples:

  • Symmetry: if (h, "Roommate", t) exists, then (t, "Roommate", h) should also exist.
  • Inverse relation: if (h, "Advisor", t) exists, then (t, "Advisee", h) should also exist.

Can we categorise these patterns? And are KG embedding methods such as TransE expressive enough to model them?

Slide 23 gives the four categories (all statements hold for all entities):

  • Symmetric: \(r(h,t)\Rightarrow r(t,h)\). Examples: Family, Roommate.
    Antisymmetric: \(r(h,t)\Rightarrow\neg r(t,h)\). Example: Hypernym, a word with a broader meaning (poodle vs. dog: "dog" is a hypernym of "poodle", not the other way).
  • Inverse: \(r_2(h,t)\Rightarrow r_1(t,h)\). Example: (Advisor, Advisee).
  • Composition (transitive): \(r_1(x,y)\wedge r_2(y,z)\Rightarrow r_3(x,z)\). Example: my mother's husband is my father.
  • 1-to-N: \(r(h,t_1),r(h,t_2),\dots,r(h,t_n)\) are all true. Example: \(r\) is "StudentsOf".

"A model can model a pattern" means: there exist embeddings that make the score function respect the pattern. This is an expressiveness question, like Lecture 6's question about GNNs: if the geometry cannot represent a pattern, no amount of training will find it.

The five patterns are independent properties: symmetry does not imply 1-to-N, for example. So every model must be checked against each pattern separately.

What TransE can and cannot express slides 24-29 ↗

Slides 24–29: what a pure translation can and cannot express. Each ✓ is a construction, each ✗ a contradiction. slides 24-29 ↗

Each slide takes one pattern and either constructs embeddings (✓) or derives a contradiction (✗).

  • Slide 24, antisymmetric ✓: if \(\mathbf{h}+\mathbf{r}=\mathbf{t}\) with \(\mathbf{r}\neq0\), then \(\mathbf{t}+\mathbf{r}=\mathbf{h}+2\mathbf{r}\neq\mathbf{h}\). Going along \(\mathbf{r}\) twice does not bring you back.
  • Slide 25, inverse ✓: if \(\mathbf{h}+\mathbf{r}_2=\mathbf{t}\), set \(\mathbf{r}_1=-\mathbf{r}_2\); then \(\mathbf{t}+\mathbf{r}_1=\mathbf{h}\). Advisee is "Advisor walked backwards".
  • Slide 26, composition ✓: set \(\mathbf{r}_3=\mathbf{r}_1+\mathbf{r}_2\). If \(\mathbf{x}+\mathbf{r}_1=\mathbf{y}\) and \(\mathbf{y}+\mathbf{r}_2=\mathbf{z}\), then \(\mathbf{x}+\mathbf{r}_3=\mathbf{z}\). The figure shows the triangle x → y → z with \(\mathbf{r}_3\) as the third side.
  • Slide 27, symmetric ✗: TransE can model it only if \(\mathbf{r}=0\) and \(\mathbf{h}=\mathbf{t}\). Proof: for all \(h,t\) with \(r(h,t)\), \(r(t,h)\) is also true, so \(\mathbf{h}+\mathbf{r}-\mathbf{t}=0\) and \(\mathbf{t}+\mathbf{r}-\mathbf{h}=0\). Adding gives \(2\mathbf{r}=0\), so \(\mathbf{r}=0\) and \(\mathbf{h}=\mathbf{t}\). But \(h\) and \(t\) are different entities and should be mapped to different locations.
  • Slide 28, 1-to-N ✗: if \((h,r,t_1)\) and \((h,r,t_2)\) both exist (e.g. \(r\) = "StudentsOf"), then \(\mathbf{t}_1=\mathbf{h}+\mathbf{r}=\mathbf{t}_2\), although \(t_1\neq t_2\) are different entities: contradictory.

Slide 29 fills in the first row of the table: TransE, score \(-\|\mathbf{h}+\mathbf{r}-\mathbf{t}\|\), \(\mathbf{h},\mathbf{t},\mathbf{r}\in\mathbb{R}^k\): symmetric ✗, antisymmetric ✓, inverse ✓, composition ✓, 1-to-N ✗.

The pattern behind the results: a translation is an invertible, composable, non-self-inverse map. That gives inverse and composition for free, and makes symmetry and one-to-many impossible.

In practice the equalities are only approximate, so TransE can partly cope with 1-to-N by putting all tails near \(\mathbf{h}+\mathbf{r}\) (slide 60 calls this a "weaker notion of 1-to-N"). But then those tails become nearly identical in every other relation too.

TransR: translate in a relation-specific space slides 31-36 ↗

TransR: entities live in one space, every relation projects them into its own space and translates there. slides 31-32 ↗
Slides 33–36: with a projection per relation every pattern becomes expressible. slides 33-36 ↗

Slide 31 [Lin et al., AAAI 2015]. TransE models the translation of every relation in the same embedding space. Can we design a new space for each relation and translate in a relation-specific space? TransR: model entities as vectors in the entity space \(\mathbb{R}^d\), and each relation as a vector in a relation space, \(\mathbf{r}\in\mathbb{R}^k\), with a projection matrix \(\mathbf{M}_r\in\mathbb{R}^{k\times d}\).

Slide 32: use \(\mathbf{M}_r\) to project from the entity space \(\mathbb{R}^d\) into the relation space \(\mathbb{R}^k\):

\[ \mathbf{h}_\perp=\mathbf{M}_r\mathbf{h},\qquad \mathbf{t}_\perp=\mathbf{M}_r\mathbf{t},\qquad f_r(h,t)=-\|\mathbf{h}_\perp+\mathbf{r}-\mathbf{t}_\perp\|. \]

The figure has two panels: the entity space with points h and t, an arrow \(\mathbf{M}_r\), and the relation space where \(\mathbf{h}_\perp+\mathbf{r}\) lands on \(\mathbf{t}_\perp\).

The key new power: \(\mathbf{M}_r\) can be non-injective. Different entities can collapse to the same point in relation space while staying different in entity space.

  • Slide 33, symmetric ✓: take \(\mathbf{r}=0\) and learn \(\mathbf{M}_r\) with \(\mathbf{h}_\perp=\mathbf{M}_r\mathbf{h}=\mathbf{M}_r\mathbf{t}=\mathbf{t}_\perp\). We map \(h\) and \(t\) to the same location in the space of relation \(r\); they are still different in the entity space. Different symmetric relations may have different \(\mathbf{M}_r\).
  • Slide 34, antisymmetric ✓: with \(\mathbf{r}\neq0\), \(\mathbf{M}_r\mathbf{h}+\mathbf{r}=\mathbf{M}_r\mathbf{t}\); then \(\mathbf{M}_r\mathbf{t}+\mathbf{r}\neq\mathbf{M}_r\mathbf{h}\) (same argument as TransE, now inside the relation space).
  • Slide 35, 1-to-N ✓: if \((h,r,t_1)\) and \((h,r,t_2)\) exist, learn \(\mathbf{M}_r\) so that \(\mathbf{t}_\perp=\mathbf{M}_r\mathbf{t}_1=\mathbf{M}_r\mathbf{t}_2\). Note that \(\mathbf{t}_1\) does not need to equal \(\mathbf{t}_2\): we only need \(\mathbf{t}_1-\mathbf{t}_2\in\ker\mathbf{M}_r\).
  • Slide 36, inverse ✓: set \(\mathbf{r}_2=-\mathbf{r}_1\) and \(\mathbf{M}_{r_1}=\mathbf{M}_{r_2}\). Then \(\mathbf{M}_{r_1}\mathbf{t}+\mathbf{r}_1=\mathbf{M}_{r_1}\mathbf{h}\) and \(\mathbf{M}_{r_2}\mathbf{h}+\mathbf{r}_2=\mathbf{M}_{r_2}\mathbf{t}\).
Check yourself: why can't TransE use the "collapse" trick for symmetry? show answer

Its only map is the identity, which is injective, so distinct entities can never coincide.

TransR can model composition: the kernel argument slides 37-41 ↗

Slides 37–41: composition in TransR via kernels; linear maps chain, so a relation that absorbs both kernels represents the composed relation. slides 37-41 ↗

Slide 37: TransR can also model composition, \(r_1(x,y)\wedge r_2(y,z)\Rightarrow r_3(x,z)\) (my mother's husband is my father). High-level intuition: TransR models a triple with linear functions, and linear functions are chainable: if \(f(x)=ax+b\) and \(g(x)=cx+d\), then \(f(g(x))=a(cx+d)+b\) is again linear.

Slide 38, background: the kernel of a matrix \(\mathbf{M}\) is \(\ker\mathbf{M}=\{\mathbf{h}:\mathbf{M}\mathbf{h}=\mathbf{0}\}\). The figure shows a blob \(\ker(\mathbf{M})\) that \(\mathbf{M}\) sends to 0.

Slide 39, the proof, step 1. Assume vectors \(\mathbf{g}_1,\mathbf{g}_2\) with \(\mathbf{M}_{r_1}\mathbf{g}_1=\mathbf{r}_1\) and \(\mathbf{M}_{r_2}\mathbf{g}_2=\mathbf{r}_2\).

  • If \(r_1(x,y)\) exists: \(\mathbf{M}_{r_1}\mathbf{x}+\mathbf{r}_1=\mathbf{M}_{r_1}\mathbf{y}\Rightarrow\mathbf{M}_{r_1}(\mathbf{y}-\mathbf{x})=\mathbf{r}_1\Rightarrow\mathbf{y}-\mathbf{x}\in\mathbf{g}_1+\ker\mathbf{M}_{r_1}\Rightarrow\mathbf{y}\in\mathbf{x}+\mathbf{g}_1+\ker\mathbf{M}_{r_1}\).
  • Same for \(r_2(y,z)\): \(\mathbf{z}\in\mathbf{y}+\mathbf{g}_2+\ker\mathbf{M}_{r_2}\).
  • Then \(\mathbf{z}\in\mathbf{x}+\mathbf{g}_1+\mathbf{g}_2+\ker\mathbf{M}_{r_1}+\ker\mathbf{M}_{r_2}\).

Slide 40, step 2. Construct \(\mathbf{M}_{r_3}\) such that \(\ker\mathbf{M}_{r_3}=\ker\mathbf{M}_{r_1}+\ker\mathbf{M}_{r_2}\). It exists because \(\dim\ker\mathbf{M}_{r_3}\ge\dim\ker\mathbf{M}_{r_1}\) and \(\mathbf{M}_{r_3}\) has the same shape as \(\mathbf{M}_{r_1}\). Set \(\mathbf{r}_3=\mathbf{M}_{r_3}(\mathbf{g}_1+\mathbf{g}_2)\). Then \(\mathbf{z}-\mathbf{x}-(\mathbf{g}_1+\mathbf{g}_2)\in\ker\mathbf{M}_{r_3}\), so

\[ \mathbf{M}_{r_3}\mathbf{x}+\mathbf{r}_3=\mathbf{M}_{r_3}\mathbf{z}. \]

Why the existence claim holds: a \(k\times d\) matrix with a prescribed kernel \(K\) exists iff its rank \(d-\dim K\) is at most \(k\). Since \(\dim(K_1+K_2)\ge\dim K_1\), the required rank \(d-\dim(K_1+K_2)\) is at most \(\operatorname{rank}\mathbf{M}_{r_1}\le k\). Bigger kernel = smaller rank = always achievable.

Slide 41 updates the table with the TransR row: score \(-\|\mathbf{M}_r\mathbf{h}+\mathbf{r}-\mathbf{M}_r\mathbf{t}\|\), \(\mathbf{h},\mathbf{t}\in\mathbb{R}^d\), \(\mathbf{r}\in\mathbb{R}^k\), \(\mathbf{M}_r\in\mathbb{R}^{k\times d}\): ✓ for all five patterns.

The price: \(\mathbf{M}_{r_3}\) must "forget" every direction that \(\mathbf{M}_{r_1}\) or \(\mathbf{M}_{r_2}\) forgets, so long compositions lose information quickly. TransR also has \(k\times d\) parameters per relation, which is costly with thousands of relations (the same issue as R-GCN in Lecture 9).

DistMult: bilinear scoring slides 43-45 ↗

Slides 43–45: DistMult replaces distance by a bilinear score with a diagonal relation matrix; t scores high when it points along h ∘ r. slides 43-45 ↗

Slide 43 [Yang et al., ICLR 2015]. So far the score \(f_r(h,t)\) was the negative of an L1/L2 distance (TransE, TransR). New idea: bilinear modelling,

\[ f_r(h,t)=\mathbf{h}^\top\mathbf{A}\,\mathbf{t},\qquad \mathbf{h},\mathbf{t}\in\mathbb{R}^k,\ \mathbf{A}\in\mathbb{R}^{k\times k}. \]

Problem: this is too general and prone to overfitting: the matrix \(\mathbf{A}\) is too expressive (\(k^2\) parameters per relation; this full version is RESCAL from the timeline). Fix: limit \(\mathbf{A}\) to be diagonal. This is called DistMult.

Slide 44: entities and relations are vectors in \(\mathbb{R}^k\), and

\[ f_r(h,t)=\langle\mathbf{h},\mathbf{r},\mathbf{t}\rangle=\sum_i\mathbf{h}_i\cdot\mathbf{r}_i\cdot\mathbf{t}_i. \]

The figure: the three vectors \(\mathbf{h}\), \(\mathbf{r}\), \(\mathbf{t}\) are multiplied coordinate by coordinate (Product), and the products are summed (Sum) into \(f_r(h,t)\).

Slide 45, intuition: the score is a cosine similarity between \(\mathbf{h}\cdot\mathbf{r}\) and \(\mathbf{t}\), where \(\mathbf{h}\cdot\mathbf{r}\) is the Hadamard (element-wise) product, \([\mathbf{h}\cdot\mathbf{r}]_i=\mathbf{h}_i\cdot\mathbf{r}_i\):

\[ \langle\mathbf{h},\mathbf{r},\mathbf{t}\rangle=\cos(\mathbf{h}\cdot\mathbf{r},\mathbf{t})\,\|\mathbf{h}\cdot\mathbf{r}\|\,\|\mathbf{t}\|. \]

Example (figure): \(\mathbf{t}_2\) points to the same side as \(\mathbf{h}\cdot\mathbf{r}\), so \(f_r(h,t_2)>0\); \(\mathbf{t}_1\) points to the other side, so \(f_r(h,t_1)<0\). The dashed line is the hyperplane orthogonal to \(\mathbf{h}\cdot\mathbf{r}\).

Intuition: \(\mathbf{r}\) is a per-dimension "importance and sign" mask. It reweights the features of \(h\), and then asks whether \(t\) agrees with the reweighted vector.

What DistMult can and cannot express slides 46-51 ↗

Slides 46–51: DistMult handles 1-to-N and symmetry for free, and loses everything that needs direction. slides 46-51 ↗
  • Slide 46, 1-to-N ✓: if \((h,r,t_1)\) and \((h,r,t_2)\) exist, we can have \(\langle\mathbf{h},\mathbf{r},\mathbf{t}_1\rangle=\langle\mathbf{h},\mathbf{r},\mathbf{t}_2\rangle\) with \(\mathbf{t}_1\neq\mathbf{t}_2\): both tails just need the same projection on \(\mathbf{h}\cdot\mathbf{r}\) (figure: \(\mathbf{t}_1\), \(\mathbf{t}_2\) at equal angle and height relative to \(\mathbf{h}\cdot\mathbf{r}\)).
  • Slide 47, symmetric ✓, naturally: \(f_r(h,t)=\sum_i\mathbf{h}_i\mathbf{r}_i\mathbf{t}_i=\langle\mathbf{t},\mathbf{r},\mathbf{h}\rangle=f_r(t,h)\), due to the commutative property of multiplication.
  • Slide 48, antisymmetric ✗: by the same identity, \(r(h,t)\) and \(r(t,h)\) always have the same score. DistMult cannot tell the head from the tail, so every relation is modelled as symmetric, even antisymmetric ones.
  • Slide 49, inverse ✗: suppose DistMult models inverse relations: \(f_{r_2}(h,t)=\langle\mathbf{h},\mathbf{r}_2,\mathbf{t}\rangle=\langle\mathbf{t},\mathbf{r}_1,\mathbf{h}\rangle=f_{r_1}(t,h)\). For example \(\mathbf{r}_2=\mathbf{r}_1\) solves this (other solutions with \(\mathbf{r}_2\neq\mathbf{r}_1\) also exist). But semantically this does not make sense: the embedding of "Advisor" should not be the same as that of "Advisee". The point: with a symmetric score, "inverse" carries no information, because each relation already equals its own inverse.
  • Slide 50, composition ✗: intuition: the product is commutative (\(a\cdot b=b\cdot a\)), so DistMult does not distinguish head and tail entities, and it cannot model composition. Unlike a translation or a projection, the DistMult score is not a map from \(h\) to \(t\) that can be chained.

Slide 51 adds the DistMult row: score \(\langle\mathbf{h},\mathbf{r},\mathbf{t}\rangle\), \(\mathbf{h},\mathbf{t},\mathbf{r}\in\mathbb{R}^k\): symmetric ✓, antisymmetric ✗, inverse ✗, composition ✗, 1-to-N ✓.

So DistMult and TransE are almost complements: DistMult does symmetric and 1-to-N; TransE does antisymmetric, inverse and composition. ComplEx keeps DistMult's form but breaks its symmetry.

Check yourself: on a KG where 80% of the edges are "is_a" (antisymmetric), would you try DistMult? show answer

No: it assigns (dog, is_a, poodle) the same score as (poodle, is_a, dog).

ComplEx: DistMult in complex space slides 53-55 ↗

Slides 53–55: DistMult in complex space; the conjugate of the tail is what makes direction visible. slides 53-55 ↗

Slide 53 [Trouillon et al., ICML 2016]. Based on DistMult, ComplEx embeds entities and relations in a complex vector space \(\mathbb{C}^k\). A complex vector is \(\mathbf{u}=\mathbf{a}+\mathbf{b}i\) with \(\mathbf{a},\mathbf{b}\in\mathbb{R}^k\); its conjugate is \(\bar{\mathbf{u}}=\mathbf{a}-\mathbf{b}i\) (in the figure, \(\bar{\mathbf{u}}\) is the mirror image of \(\mathbf{u}\) across the real axis). Complex multiplication: \((a+ib)(c+id)=(ac-bd)+i(ad+bc)\). The example on the slide: \((3+4i)(2+3i)=-6+17i\), drawn as a scaling by 2 plus a "scale and rotate" by \(3i\), added together. Multiplying by a complex number scales and rotates.

Slide 54, score function:

\[ f_r(h,t)=\mathrm{Re}\Big(\sum_i\mathbf{h}_i\cdot\mathbf{r}_i\cdot\bar{\mathbf{t}}_i\Big). \]

The figure mirrors DistMult's: element-wise product of \(\mathbf{h}\), \(\mathbf{r}\), \(\bar{\mathbf{t}}\), then sum, then take the real part. The only changes are complex numbers and the conjugate on the tail.

Slide 55 expands it into real DistMult-like terms (writing \(\mathbf{h}_i=\mathrm{Re}(\mathbf{h}_i)+i\,\mathrm{Im}(\mathbf{h}_i)\) etc.):

\[ f_r(h,t)=\langle\mathrm{Re}\,\mathbf{h},\mathrm{Re}\,\mathbf{r},\mathrm{Re}\,\mathbf{t}\rangle+\langle\mathrm{Im}\,\mathbf{h},\mathrm{Re}\,\mathbf{r},\mathrm{Im}\,\mathbf{t}\rangle+\langle\mathrm{Re}\,\mathbf{h},\mathrm{Im}\,\mathbf{r},\mathrm{Im}\,\mathbf{t}\rangle-\langle\mathrm{Im}\,\mathbf{h},\mathrm{Im}\,\mathbf{r},\mathrm{Re}\,\mathbf{t}\rangle. \]

Read it this way: the first two terms (with \(\mathrm{Re}\,\mathbf{r}\)) are symmetric in \(h\) and \(t\). The last two (with \(\mathrm{Im}\,\mathbf{r}\)) change sign when \(h\) and \(t\) swap: \(\langle\mathrm{Re}\,\mathbf{h},\mathrm{Im}\,\mathbf{r},\mathrm{Im}\,\mathbf{t}\rangle-\langle\mathrm{Im}\,\mathbf{h},\mathrm{Im}\,\mathbf{r},\mathrm{Re}\,\mathbf{t}\rangle\) is antisymmetric. So \(\mathrm{Re}\,\mathbf{r}\) is the symmetric part of the relation and \(\mathrm{Im}\,\mathbf{r}\) is the antisymmetric part.

Check yourself: compute one coordinate by hand with \(h=1+i\), \(r=i\), \(t=1\). show answer

\(h\,r\,\bar t=(1+i)i=-1+i\), real part \(-1\). Swap: \(t\,r\,\bar h=i(1-i)=1+i\), real part \(+1\). Different scores for the two directions.

What ComplEx can and cannot express slides 56-59 ↗

Slides 56–59: ComplEx keeps DistMult's strengths and regains antisymmetry and inverses. slides 56-59 ↗
  • Slide 56, antisymmetric ✓: the model is expressive enough to learn a high \(f_r(h,t)=\mathrm{Re}(\sum_i\mathbf{h}_i\mathbf{r}_i\bar{\mathbf{t}}_i)\) and a low \(f_r(t,h)=\mathrm{Re}(\sum_i\mathbf{t}_i\mathbf{r}_i\bar{\mathbf{h}}_i)\), thanks to the asymmetric modelling with the complex conjugate.
  • Slide 57, symmetric ✓: when \(\mathrm{Im}(\mathbf{r})=0\), \[ f_r(h,t)=\mathrm{Re}\Big(\sum_i\mathbf{h}_i\mathbf{r}_i\bar{\mathbf{t}}_i\Big)=\sum_i\mathbf{r}_i\,\mathrm{Re}(\mathbf{h}_i\bar{\mathbf{t}}_i)=\sum_i\mathbf{r}_i\,\mathrm{Re}(\bar{\mathbf{h}}_i\mathbf{t}_i)=\sum_i\mathrm{Re}(\mathbf{r}_i\bar{\mathbf{h}}_i\mathbf{t}_i)=f_r(t,h), \] using that a complex number and its conjugate have the same real part.
  • Slide 58, inverse ✓: set \(\mathbf{r}_1=\bar{\mathbf{r}}_2\). The complex conjugate of \(\mathbf{r}_2=\arg\max_{\mathbf{r}}\mathrm{Re}(\langle\mathbf{h},\mathbf{r},\bar{\mathbf{t}}\rangle)\) is exactly \(\mathbf{r}_1=\arg\max_{\mathbf{r}}\mathrm{Re}(\langle\mathbf{t},\mathbf{r},\bar{\mathbf{h}}\rangle)\). Reason: \(\mathrm{Re}(\langle\mathbf{t},\bar{\mathbf{r}},\bar{\mathbf{h}}\rangle)=\mathrm{Re}\big(\overline{\langle\bar{\mathbf{t}},\mathbf{r},\mathbf{h}\rangle}\big)=\mathrm{Re}(\langle\mathbf{h},\mathbf{r},\bar{\mathbf{t}}\rangle)\): conjugating the relation is the same as swapping head and tail.
  • Slide 59, composition ✗ and 1-to-N ✓: ComplEx shares these properties with DistMult. It cannot model composition relations, and it can model 1-to-N relations.

In one sentence: ComplEx = DistMult + an antisymmetric part. That one change buys antisymmetry and inverses, while keeping symmetry and 1-to-N. Composition still fails, because the score is still a "one-shot" bilinear test rather than a chainable map.

The full table, RotatE, and how to choose a model slides 60-62 ↗

modelscore f_r(h, t)spaceSAIC1-N
TransE−‖h + r − t‖ℝᵏ✗✓✓✓✗
TransR−‖M_r h + r − M_r t‖ℝᵈ, M_r ∈ ℝ^{k×d}✓✓✓✓✓
DistMult⟨h, r, t⟩ℝᵏ✓✗✗✗✓
ComplExRe(⟨h, r, t̄⟩)ℂᵏ✓✓✓✗✓
RotatE−‖h ∘ r − t‖ℂᵏ✓✓✓✓✗

Slide 60: S symmetric, A antisymmetric, I inverse, C composition, 1-N one-to-many. RotatE is TransE in complex space: each coordinate is rotated by a unit complex number, so relations invert and compose like angles.

Slide 62 (RotatE paper): MRR (green) and Hits@10 (navy) on two benchmarks. RotatE leads on both; DistMult and ComplEx do relatively better on WN18RR, whose relations are more symmetric. slide 62 ↗

Slide 60 gives the complete expressiveness table (S = symmetric, A = antisymmetric, I = inverse, C = composition, N = 1-to-N):

  • TransE: \(-\|\mathbf{h}+\mathbf{r}-\mathbf{t}\|\), \(\mathbf{h},\mathbf{t},\mathbf{r}\in\mathbb{R}^k\). S ✗, A ✓, I ✓, C ✓, N ✗.
  • TransR: \(-\|\mathbf{M}_r\mathbf{h}+\mathbf{r}-\mathbf{M}_r\mathbf{t}\|\). S ✓, A ✓, I ✓, C ✓, N ✓. (On this slide the dimensions are written \(\mathbf{h},\mathbf{t}\in\mathbb{R}^k\), \(\mathbf{r}\in\mathbb{R}^d\), \(\mathbf{M}_r\in\mathbb{R}^{d\times k}\): the letters \(d,k\) are swapped relative to slides 31–41, but the meaning is the same.)
  • DistMult: \(\langle\mathbf{h},\mathbf{r},\mathbf{t}\rangle\), \(\mathbb{R}^k\). S ✓, A ✗, I ✗, C ✗, N ✓.
  • ComplEx: \(\mathrm{Re}(\langle\mathbf{h},\mathbf{r},\bar{\mathbf{t}}\rangle)\), \(\mathbb{C}^k\). S ✓, A ✓, I ✓, C ✗, N ✓.
  • RotatE: \(-\|\mathbf{h}\circ\mathbf{r}-\mathbf{t}\|\), \(\mathbf{h},\mathbf{t},\mathbf{r}\in\mathbb{C}^k\). S ✓, A ✓, I ✓, C ✓, N ✗.

Here \(\circ\) is the Hadamard product; the slide's example: \(\begin{bmatrix}3&5&7\\4&9&8\end{bmatrix}\circ\begin{bmatrix}1&6&3\\0&2&9\end{bmatrix}=\begin{bmatrix}3\times1&5\times6&7\times3\\4\times0&9\times2&8\times9\end{bmatrix}\). RotatE is "TransE in complex space": each coordinate of the head is multiplied by a complex number. In the RotatE paper each \(\mathbf{r}_i\) has modulus 1, so the relation is a rotation of every coordinate. Rotations are invertible (inverse = rotate back), composable (angles add), and a rotation by \(\pi\) is its own inverse (symmetry). The slide's footnote: TransE and RotatE both satisfy a weaker notion of 1-to-N: many tails can be equidistant to \(\mathbf{r}\circ\mathbf{h}\) or \(\mathbf{r}+\mathbf{h}\), even if they cannot all be exactly there.

Slide 61 [Sun et al., ICLR 2019], KG embeddings in practice:

  1. Different KGs may have drastically different relation patterns.
  2. There is no general embedding that works for all KGs; use the table to select models.
  3. Try TransE for a quick run if the target KG does not have many symmetric relations.
  4. Then use more expressive models, e.g. ComplEx or RotatE (TransE in complex space).

Slide 62, empirical comparison (MR = mean rank, lower is better; MRR = mean reciprocal rank and H10 = Hits@10, higher is better). The TransR row is empty.

  • FB15k-237: TransE MR 357, MRR .294, H10 .465; DistMult 254, .241, .419; ComplEx 339, .247, .428; RotatE 177, 0.338, 0.533.
  • WN18RR: TransE MR 3384, MRR .226, H10 .501; DistMult 5110, .43, .49; ComplEx 5261, .44, .51; RotatE 3340, 0.476, 0.571.

Read the table with the expressiveness lens. WN18RR (from WordNet) is full of hierarchical, antisymmetric relations (hypernym) and some symmetric ones; TransE's MRR is lowest there, and RotatE, which handles all of S/A/I/C, is best on both datasets. MR and MRR can disagree (DistMult has the best MR among the non-RotatE models on FB15k-237 but the worst MRR), because MR is dominated by the worst-ranked queries while MRR is dominated by the top positions.

Summary, and a look ahead: reasoning with embeddings (from the notes) slides 63 ↗

Slide 63 summarises:

  • Link prediction / graph completion is one of the prominent tasks on knowledge graphs.
  • We introduced TransE / TransR / DistMult / ComplEx, models with different embedding spaces and different expressiveness (plus RotatE in the final table).
  • Next: reasoning in knowledge graphs.

From the notes (Chapter 11, not on these slides). The notes continue directly into multi-hop reasoning, which builds on this lecture's embeddings. KG completion is 1-hop reasoning: "is \(t\) the answer to \((h,r)\)?". A path query \(q=(v_a,(r_1,\dots,r_n))\) starts at an anchor entity and follows \(n\) relations; e.g. "what protein is associated with the adverse event caused by Fulvestrant?" is (e:Fulvestrant, (r:Causes, r:Assoc)). Just traversing the KG fails because the KG is incomplete; completing the KG first and then traversing fails because the completed probabilistic KG is dense, and traversal cost grows exponentially with the path length.

Lecture notes figure, page 61
Notes Figure 11.4: TransE for KG completion (left: \(\mathbf{h}+\mathbf{r}\) lands near \(\mathbf{t}\)) and for path queries (right: \(\mathbf{h}+\mathbf{r}_1+\mathbf{r}_2+\dots+\mathbf{r}_n\) lands near the answer). notes p.61 ↗
Lecture notes figure, page 62
Notes Figures 11.6 and 11.7: a query box that encloses its answer entities (Short of Breath, Kidney Infection, Headache), and the projection operator that moves and enlarges a box \(q\) along relation \(r\) to give \(q'\). notes p.62 ↗

Not shown (no new content): slides 1,2,12,18,30,42,52 — title page, announcements (Colab 2/3, Homework 1 grades, Homework 2) and section-divider title pages.

Review: what to keep from this session

Carried forward. Lecture 15 builds foundation models for KGs that generalise to unseen entities and relations, starting from the limits of these shallow embeddings; Lecture 16 and 17 use KGs as the structured side of retrieval for LLMs.

Quick self-check

Why can TransE not model symmetric relations?

h + r ≈ t and t + r ≈ h together force r ≈ 0, which makes h ≈ t for every pair.

Which pattern does DistMult fail and ComplEx fix?

Antisymmetry (and inversion): DistMult's score is symmetric in h and t; the complex conjugate in ComplEx breaks that symmetry.

What does "1-to-N" mean and which models handle it?

One head and relation with many true tails (a person's students); TransE collapses all tails to one point, TransR and DistMult can keep them apart.

Key idea

A KG embedding turns each relation into a geometric operation; the algebra of that operation (commutative or not, invertible or not, composable or not) decides which logical patterns of relations the model can represent.

\[ \text{TransE: } \mathbf h+\mathbf r\approx\mathbf t\qquad \text{DistMult: } \textstyle\sum_i h_ir_it_i\qquad \text{ComplEx: } \mathrm{Re}\sum_i h_ir_i\bar t_i\qquad \text{RotatE: } \mathbf h\circ e^{i\boldsymbol\theta_r}\approx\mathbf t \]

Worked example: scoring and ranking by hand

TransE in 2-D. \(\mathbf h_{\text{Obama}}=(0,0)\), \(\mathbf r_{\text{nationality}}=(2,1)\). Candidates: USA \((2,1)\), France \((3,-1)\), Kenya \((1.5,1.5)\).

  1. \(\mathbf h+\mathbf r=(2,1)\).
  2. Distances: USA 0; Kenya \(\sqrt{0.25+0.25}=0.71\); France \(\sqrt{1+4}=2.24\).
  3. Ranking: USA (rank 1), Kenya, France → Hits@1 = 1, reciprocal rank = 1.
  4. Symmetry test: "roommate" with \(\mathbf r=(1,0)\): if \(\mathbf h+\mathbf r=\mathbf t\) then \(\mathbf t+\mathbf r=\mathbf h+2\mathbf r\ne\mathbf h\) — TransE can't make roommates mutual unless \(\mathbf r=0\).
  5. RotatE fix: one complex coordinate, \(r=e^{i\pi}=-1\): \(h\cdot(-1)=t\) and \(t\cdot(-1)=h\). A half-turn is its own inverse ⇒ symmetric.

Strictly speaking: "can model" means the pattern is representable; whether training finds it depends on data and loss. Also, modern evaluation uses filtered ranking (other known true tails are removed from the candidate list), as in Lecture 9.

In code (PyG)

from torch_geometric.nn.kge import TransE, DistMult, ComplEx, RotatE
model = RotatE(num_nodes=ds.num_nodes, num_relations=ds.num_relations, hidden_channels=50, margin=9.0)
loader = model.loader(head_index=h, rel_type=r, tail_index=t, batch_size=1000, shuffle=True)
opt = torch.optim.Adam(model.parameters(), lr=0.01)
for hb, rb, tb in loader:
    opt.zero_grad(); loss = model.loss(hb, rb, tb); loss.backward(); opt.step()
mean_rank, mrr, hits10 = model.test(h_te, r_te, t_te, batch_size=20000, k=10)

Proof workshop: Which geometry can say "symmetric" and "antisymmetric"?

One problem in olympiad style. Give it fifteen minutes on paper before you open the first hint; the hints are graded, so open them one at a time.

Problem. Prove: (a) DistMult, \(f(h,r,t)=\sum_ih_ir_it_i\), gives \(f(h,r,t)=f(t,r,h)\) for every relation, so it cannot model an antisymmetric relation; (b) if TransE fits a symmetric relation exactly (\(h+r=t\) and \(t+r=h\)), then \(r=0\) and \(h=t\); (c) RotatE, \(t=h\circ r\) with \(|r_i|=1\), fits a symmetric relation exactly if and only if every \(r_i\in\{1,-1\}\).

Hint 1
(a) The sum is symmetric in two of its arguments. (b) Add the two equations.
Hint 2
(c) Apply the relation twice: \(h=t\circ r=h\circ r\circ r\), coordinate by coordinate, assuming \(h_i\ne0\).
Solution
(a) \(\sum_ih_ir_it_i=\sum_it_ir_ih_i\). An antisymmetric relation needs \(f(h,r,t)\) high and \(f(t,r,h)\) low for some pair, which is impossible. (b) Adding gives \(2r=0\); then \(h=t\), so every pair of entities in the relation collapses to one point. (c) Symmetry means \(t=h\circ r\) and \(h=t\circ r\), so \(h_i=h_ir_i^2\) and \(r_i^2=1\) wherever \(h_i\ne0\); with \(|r_i|=1\) that is \(r_i=\pm1\), a rotation by 0 or \(\pi\). Conversely \(r_i=\pm1\) gives \(r\circ r=\mathbf 1\), so \(t\circ r=h\circ r\circ r=h\). \(\square\)

Why it matters. Each score function is a choice of algebra: the relation patterns it can express are properties of that algebra (commutativity, inverses, idempotents). This is the neuro-symbolic reading of the cheat-sheet table: choosing an embedding is choosing which logical rules your model can satisfy, and the notebook's family-KG results follow these proofs exactly.

Sharper lens: the algebra behind the table

Not on the slides. Three or four points where the lecture rounds something off: the exact statement, a second way to see it, and where it stops being true. Each is short and has a pointer for going further.

second viewpoint

Every model on this page factorises one tensor

A knowledge graph is a binary tensor \(\mathcal X\in\{0,1\}^{|E|\times|R|\times|E|}\) with \(\mathcal X_{hrt}=1\) for true triples; each relation is one slice \(X_r\). The bilinear models are factorisations of it:

  • RESCAL (Nickel et al. 2011): \(X_r\approx EW_rE^\top\), a full matrix per relation.
  • DistMult: \(W_r\) diagonal. Then \(EW_rE^\top\) is a symmetric matrix, which is proof (a) of the workshop in one line.
  • CP (Hitchcock 1927): separate head and tail vectors for each entity. No forced symmetry, but an entity's two vectors never share what they learn.
  • ComplEx: \(X_r\approx\mathrm{Re}\big(E\,\mathrm{diag}(\mathbf r)\,\bar E^\top\big)\). Conjugation ties head and tail roles without forcing symmetry: the real part of \(\mathbf r\) carries the symmetric part of the relation and the imaginary part the antisymmetric part.

This view gives the property the cheat-sheet lacks. A model is fully expressive if, with enough dimensions, it can fit any assignment of true and false to all triples. ComplEx, CP and RESCAL are (Trouillon et al. 2017); DistMult is not (symmetric slices only); TransE and RotatE are not either (Kazemi and Poole 2018): in TransE, for example, a relation that is reflexive on a set of entities is forced to be symmetric on it.

where it breaks

"My mother's husband is my father" does not commute

The table gives TransE and RotatE a tick for composition. Look at how they compose: \(\mathbf r_3=\mathbf r_1+\mathbf r_2\), or \(\theta_3=\theta_1+\theta_2\). Addition commutes, so both models must also satisfy \(r_2\circ r_1=r_1\circ r_2\): "my mother's husband" and "my husband's mother" get the same embedding. The slide's own example is a composition these models cannot represent faithfully.

The reason is structural: translations of \(\mathbb R^k\) and rotations of independent complex coordinates are abelian groups. Order-sensitive composition needs a non-abelian one: full matrices (RESCAL, where \(W_{r_1}W_{r_2}\ne W_{r_2}W_{r_1}\)), quaternions (QuatE, Zhang et al. 2019), or dihedral groups (DihEdral, Xu and Li 2019). Read each tick in the table as "can express some instances of this pattern", and ask which group the relation operations form.

precise statement

What a tick does not tell you, and what the leaderboards did

"Can model" is existential: some embedding exists, for one pattern at a time, at whatever dimension it takes. It says nothing about fitting all the patterns of a real KG jointly at dimension 200, or about whether training finds that embedding.

Two audits belong next to the table. Ruffinelli, Broscheit and Gemulla (ICLR 2020, "You CAN teach an old dog new tricks") retrained the classic models with one modern recipe (reciprocal relations, scoring against all entities, a tuned loss and a proper hyperparameter search): RESCAL from 2011 matched models published eight years later, and the training recipe explained more variance than the score function. Sun et al. (ACL 2020) found that part of the reported gains of several neural models came from tie-breaking: a model that gives all candidates the same score was ranked first. The algebra tells you what a model cannot do. For what it does, trust only comparisons with equal tuning and a tie-aware ("random" or "mean") rank.

Lab: hands-on and practical tips

Try it live. In the Graph Explorer: train TransE, DistMult, ComplEx and RotatE on a family knowledge graph and see which relation patterns each one can express. Each lesson has predict-then-check questions.

notebook · L10 knowledge graphs implements TransE, DistMult, ComplEx and RotatE, verifies the relation-pattern table on a family knowledge graph with known patterns, and compares raw and filtered rankings.

Read the pattern table before choosing a modelClick the cells of the figure above. On the family KG the measured behaviour matches the theory: TransE fails symmetric relations (about 0.45 MRR), DistMult confuses directions (0.6–0.7), ComplEx and RotatE are best. Inventory the patterns of your relations first (follows: antisymmetric; mutual: symmetric; retweets-the-retweeter-of: compositional).
Use filtered rankingIf (Obama, nationality, USA) and (Obama, nationality, Kenya-born-ancestry) are both true, raw ranking punishes the model for ranking one above the other. Remove other true triples from the candidate list before computing the rank.
Negatives: corrupt head or tail, not bothStandard practice samples one corrupted entity per positive and uses a margin or self-adversarial loss (RotatE). Corrupting relations is rarely useful and makes training unstable.
Shallow means transductiveA new entity or relation has no embedding. If your KG grows (new accounts, new hashtags every day), you need Lecture 15's inductive models or periodic retraining; budget for it from the start.

Readings & lookup guide

lecture notes CS224W course notes (Leni Aniva, Autumn 2024 — in your course folder)

Concise written version of the lectures; the walkthrough above cites it page by page.

Strategy: five papers, one idea each. Read TransE fully, then for the others read only the score function, the expressiveness claims and one figure. Then read RotatE carefully — it contains the cleanest pattern analysis.

read fully Bordes et al. — Translating Embeddings for Modeling Multi-relational Data (TransE, NeurIPS 2013)

NeurIPS PDF · 9 pages · ~1 h
  • §2 + Algorithm 1 (the whole method), §4.2 evaluation protocol (raw vs filtered ranking — still standard).

skim Lin et al. — Learning Entity and Relation Embeddings for KG Completion (TransR, AAAI 2015)

PDF · 30 min
  • Fig. 1 (projection into relation space) and the score function; CTransR (clustered relations) is optional.

skim Yang et al. — Embedding Entities and Relations for Learning and Inference in KBs (DistMult, ICLR 2015)

arXiv:1412.6575 · 45 min
  • §3 unifies earlier models as bilinear forms; §5 mines Horn rules from embeddings — an early neuro-symbolic move worth noticing.

core Trouillon et al. — Complex Embeddings for Simple Link Prediction (ComplEx, ICML 2016)

  • §2 (why complex numbers: the sign-rank argument for antisymmetric matrices) — elegant linear algebra.

read carefully Sun et al. — RotatE: KG Embedding by Relational Rotation in Complex Space (ICLR 2019)

  • §3.1 (the pattern table and proofs — Lemmas 1–3), §3.3 self-adversarial negative sampling (weights negatives by their current score; widely reused).

Homework & Colabs

Colab 3 out Thu 10/22 · due Thu 11/5

Released with this lecture; link on the course site. In earlier offerings the third Colab had students implement GNN layers (e.g. GraphSAGE / GAT) with PyG's MessagePassing class and/or heterogeneous message passing — good practice for Lectures 4 and 9. Save it into the course folder when you open it.

Homework 2 due Thu 10/29

KG embedding pattern questions ("can model X represent symmetric/inverse/compositional relations? prove it") are classic CS224W written-homework and exam material. Practise with the think-first boxes on this page.

Self-check

Why can't TransE model symmetric relations?

  1. It has no projection matrix
  2. It uses real numbers
  3. h + r = t and t + r = h force r = 0 and h = t
  4. It's trained with a margin loss

Adding both equations gives 2r = 0.

DistMult always assigns f_r(h,t) = f_r(t,h). Which pattern can it therefore NOT model?

  1. Antisymmetric
  2. Symmetric
  3. 1-to-N
  4. None — it models all

Commutativity of the elementwise product.

What ingredient lets ComplEx model antisymmetric relations?

  1. Unit-modulus relations
  2. A projection matrix
  3. A margin loss
  4. The complex conjugate on the tail embedding

\(\mathrm{Re}(h r \bar t)\ne\mathrm{Re}(t r\bar h)\) in general.

In RotatE, composition of relations corresponds to …

  1. multiplying moduli
  2. adding rotation angles
  3. concatenating vectors
  4. projecting into a new space

\(e^{i\theta_1}e^{i\theta_2}=e^{i(\theta_1+\theta_2)}\).

Which model in the table handles all five patterns?

  1. TransE
  2. DistMult
  3. TransR
  4. ComplEx

TransR ✓ on all; RotatE only weakly on 1-to-N.

Flashcards (click to flip)

KG completion task
Given (head, relation), rank all entities as candidate tails.
TransE loss
\(\sum[\gamma+d(h+r,t)-d(h'+r,t')]_+\) with corrupted triples.
TransR
Project with \(M_r\) into a relation space, then translate.
DistMult
\(\sum_i h_ir_it_i\): symmetric by construction.
ComplEx
\(\mathrm{Re}\sum_i h_ir_i\bar t_i\): all but composition.
RotatE
\(t=h\circ r,\ |r_i|=1\): rotations; all but strong 1-to-N.

History & the bigger picture

  • 1970s–1990s — symbolic knowledge bases: semantic networks (Quillian), Cyc (1984–), description logics. Rich logic, brittle, hand-built.
  • 2000s — the Semantic Web (RDF triples, OWL), DBpedia (2007), Freebase (2007, acquired by Google 2010 → Google Knowledge Graph 2012: "things, not strings"). Wikidata (2012).
  • 2011–2019 — embedding era: RESCAL (2011), TransE (2013), DistMult, TransR (2015), ComplEx (2016), RotatE (2019). Then GNN-based (R-GCN, CompGCN) and, in Lecture 15, inductive KG foundation models.
Political economy & philosophy · who owns the facts?

Knowledge graphs are among the most valuable assets of big tech: they decide what appears in a search box as "the answer". Freebase was a community project that ended inside a corporation. KG completion is thus not just a technical task — "predicting missing facts" about people (nationality, religion, affiliation) can be a surveillance capability. Epistemologically, embeddings treat facts as degrees of geometric plausibility, not truth: a shift from logic's bivalence to a probabilistic, coherence-based notion of knowledge — closer to Quine's "web of belief" than to a database.

Classic SNA bridge

In this lectureClassic SNA counterpartWhat carried over, what changed
Triples \((h, r, t)\)Multirelational sociomatrices; semantic networks in cognitive sociology (Carley 1994)A KG is a multiplex network with named relations and typed entities. SNA stored the same data as a stack of matrices; the embedding view adds a geometry per relation.
Symmetry, antisymmetry, inversionReciprocity (Holland & Leinhardt 1981, p₁ model); mutual vs asymmetric dyadsThe MAN dyad census (mutual, asymmetric, null) is the empirical side of the pattern table: a relation with high reciprocity needs a model that can express symmetry; "follows" with low reciprocity needs antisymmetry.
Composition \(r_3 = r_1 \circ r_2\)Compound relations and role algebras (Boorman & White 1976; Pattison 1993)Role algebra composed relations with Boolean products to find the "grammar" of a social structure. RotatE composes rotations; TransE adds translations. Same question, continuous answer.
1-to-N relationsDegree heterogeneity; "star" structures; broadcasting tiesTransE cannot give one head many tails, which rules it out for retweet-style relations where one source reaches thousands. SNA's degree distributions tell you in advance which models to exclude.
KG completion as prediction of missing factsMissing-data problems in network surveys; imputation of unobserved ties (Huisman 2009)Both infer unobserved ties from observed structure. Sociology also asked who is affected by the imputed tie, the ethics question this lecture raises about predicting attributes of people.
Bridge idea

Compute the dyad census (reciprocity) and the triad census for each relation in a social KG built from X. Use them to predict, before training, which embedding model should win per relation (RotatE for antisymmetric and compositional relations, DistMult where reciprocity is high). Then train and check. The census becomes a model-selection tool, and the mismatch cases are where your data violates the lecture's assumptions.

Go deeper in the SNA course. The twelve-unit course on social network analysis in this repository gives the classic side of this lecture in full, with labs and real-data analyses: Unit 9 · Beyond pairwise: higher-order, temporal, multilayer (knowledge graphs: multi-relational data as triples).

Connecting to your research

Neuro-symbolic and SNA angles
  • This lecture is neuro-symbolic: relation patterns are first-order rules (symmetry, inversion, Horn-style composition) and each model is a geometric semantics for a fragment of logic. Research direction: characterise exactly which rule sets a geometry can satisfy (cf. BoxE, which embeds rules as boxes — Abboud et al. 2020).
  • Social KGs: build a KG from Twitter (user — follows/retweets/mentions — user; user — uses — hashtag; hashtag — co-occurs — hashtag). "Follows" is antisymmetric, "mutual" symmetric, "retweets-the-retweeter-of" compositional — choose RotatE/ComplEx accordingly.
  • Ethics: completing a social KG predicts sensitive attributes of people. If your project does this, discuss it explicitly.

Frontier 2025–26: what changed since the slides

ICLR 2026The largest cluster at ICLR 2026 is KGs and GraphRAG — 47 atlas papers. After the 2024 GraphRAG rush the 2026 papers are more sceptical: when do graphs help retrieval at all, how to add retrieval guarantees, and how to train agentic retrievers end-to-end with RL (Graph-R1, ICML 2026, retrieves over a hypergraph).sources: arXiv:2507.21892
NeurIPS 2025 spotlightCan KG-based RAG really retrieve what you need? — A direct audit of knowledge-graph retrieval. Together with GraphRAG-Bench (ICLR 2026): graphs help multi-hop reasoning and hurt simple lookups, at token costs that differ by more than two orders of magnitude between systems.sources: arXiv:2506.05690
2021 → 2024NBFNet and ULTRA: link prediction as a generalised path sum — Replace (+, ×) by a semiring (⊕, ⊗) over paths and learn edge weights: Katz, PageRank and shortest-path heuristics are special cases; a learned Bellman–Ford computes it. ULTRA builds on it to generalise to unseen relations. The geometric score functions of this lecture are the transductive ancestors of this line.sources: arXiv:2310.04562 · arXiv:2106.06935
NeurIPS 2025 spotlightsUncertain KG completion; multi-agent LLM KG enrichment (KARMA) — Completion now comes with confidence distributions, and LLM agents build and enrich KGs from text (also TXT2KG in Lecture 16). "KGs are massive and incomplete" is being attacked from both ends.sources: arXiv:2502.06472
2026 · logicRules, paths, semiring DP and conditional message passing are one idea — A Horn rule r(x,y) ← r₁(x,z)∧…∧r_T(z,y) is a matrix product M_{{r₁}}⋯M_{{r_T}}v_x; soften the relation choice and you get Neural LP; generalise the semiring and you get NBFNet; condition message passing on the source and you get the conditional MPNNs whose logic Huang et al. (NeurIPS 2023) characterised. The composition pattern of this lecture is where neuro-symbolic AI and KG embeddings meet.sources: arXiv:2106.06935
Open question to carryEach embedding geometry is a semantics for a fragment of logic (BoxE embeds rules as boxes). Can you characterise exactly which rule sets a geometry can satisfy, and does that predict which model wins on a social KG whose relations you know are antisymmetric, compositional and 1-to-N?
Frontiers · KG embeddings are geometry for relations · rules = paths = semiring DP

Words & phrases

TermSimple meaningPersianIn context
hypernymn.a word with a broader meaning that includes another (dog is a hypernym of poodle)واژهٔ شامل، ابَرواژه"Hypernym (poodle → dog)"
complex conjugaten.for a + bi, the number a − biمزدوج مختلط"The complex conjugate on the tail breaks the head–tail symmetry"
antisymmetricadj.if it holds from a to b, it does not hold from b to a (for a ≠ b)پادمتقارن"parent_of is antisymmetric"
compositionn.chaining two relations into a third, as parent of a parent is a grandparentترکیب"TransE models composition by adding vectors"
triplen.a fact written as (head, relation, tail)سه‌تایی"score every candidate triple"
filtered rankingn.ranking the true answer only against candidates that are not other true answersرتبه‌بندی پالایش‌شده"report filtered MRR"

Go deeper

  • Next: Lecture 11 — GNNs for recommender systems (a user–item graph is a two-type KG with one relation).
  • Reasoning: Query2Box / BetaE (Ren & Leskovec) — answering multi-hop logical queries (∧, ∨, ¬) in embedding space. A beautiful neuro-symbolic line, covered in earlier editions of CS224W.
  • Open question: shallow KG embeddings are transductive — a new entity or a new relation has no embedding. Lecture 15 (ULTRA, InGram) shows how to embed relations by their interactions with other relations.