# Session 11 — Python demos

Two small, self-contained PyTorch Geometric demos that illustrate ideas from
`11_knowledge_modeling.html`:

- **Part A — Heterogeneous / multi-relational GCN.** A synthetic academic graph
  (papers, authors, venues; `cites`, `writes`, `published_in`) classified by an
  **R-GCN**, compared against a relation-blind baseline. → `fig_hetero_gcn.png`
- **Part B — KG embeddings, inference & queries.** A readable knowledge graph
  embedded with **TransE**; link-prediction inference, 1-hop ranking, and a
  **2-hop query answered in vector space** (the Query2box intuition).
  → `fig_kg_transe.png`

Both parts use independent datasets and can be run in either order. Everything
runs on a CPU laptop in a few seconds.

## Setup with `uv`

```bash
cd docs/11_knowledge_modeling/demos
uv venv
uv pip install -r requirements.txt
```

Run the notebook:

```bash
uv run jupyter lab kg_modeling_demo.ipynb
```

Or run the script directly (regenerates both figures):

```bash
uv run python kg_modeling_demo.py
```

### One-file alternative

If you prefer not to manage an environment, the script carries inline
dependency metadata-free deps in `requirements.txt`; `uv run --with torch --with
torch_geometric --with matplotlib python kg_modeling_demo.py` also works.

## Notes

- `torch_geometric` is pure-Python for the layers used here (`RGCNConv`,
  `TransE`); the optional compiled extensions (`torch-scatter`, etc.) are **not**
  required.
- `kg_modeling_demo.py` and `kg_modeling_demo.ipynb` are the same content. The
  notebook adds the narrative markdown between steps.
- Seeds are fixed, so numbers are reproducible run to run.
