---
title: 'HypKG: Contextualizing EHRs in Biomedical KGs'
url: https://www.emergentmind.com/topics/hypkg
type: topic
---

# HypKG: Contextualizing EHRs in Biomedical KGs

HypKG is a framework for integrating patient information from electronic health records (EHRs) into knowledge graphs (KGs) to generate contextualized knowledge representations for healthcare prediction tasks. It is designed for precision healthcare settings in which general-purpose biomedical KGs encode large amounts of factual knowledge but do not account for the context of a specific patient, while EHRs contain diagnoses, prescriptions, labs, and demographics that can supply that context. The framework combines entity linking, hypergraph construction, and hypergraph transformers guided by downstream prediction tasks so that representations of both patients and KG concepts are jointly adapted to patient-specific contexts [2507.19726].

## 1. Motivation and problem formulation

HypKG is motivated by a mismatch between the structure of general biomedical KGs and the requirements of precision healthcare. General-purpose KGs such as UMLS and iBKH encode millions of static facts as entities and relations, but they are described as being oblivious to the “who, when, and how” of a specific patient’s journey. In the formulation of HypKG, this matters because a drug–disease edge in a KG may be contraindicated for certain patient subpopulations, and interaction effects may emerge only when a patient’s existing medications and diagnoses are considered together [2507.19726].

EHRs provide the contextual signals needed to specialize such knowledge. The framework therefore treats patient records not as independent feature vectors, but as context carriers that can activate relevant portions of a KG. This design is explicitly intended to let patient contexts from EHRs “light up” the relevant subgraphs and to learn embeddings that remain faithful to the KG while also being adapted to patient context.

The underlying representational problem is therefore not only one of prediction, but of contextualization. HypKG does not merely attach EHR-derived features to KG embeddings; it constructs a joint structure in which patient visits become higher-order relational objects. This suggests that the method is aimed at preserving the combinatorial structure of encounters rather than reducing them immediately to pairwise associations.

## 2. Entity linking between EHR attributes and KG entities

The first stage of HypKG is entity linking. Each EHR attribute \(a\), such as an ICD code or drug name, is normalized by lowercasing and removing punctuation, then encoded with a biomedical language model, SAPBERT, to obtain a dense vector \(Z_{lm,a}\). KG entities \(X_k\) are embedded in the same way as \(Z_{lm,X_k}\). Candidate matches are scored by cosine similarity,

\[
\mathrm{SIM}(a, X_k)=\frac{Z_{lm,a}\cdot Z_{lm,X_k}}{\|Z_{lm,a}\|\,\|Z_{lm,X_k}\|}.
\]

The method selects the top-\(LC\) candidates, with \(LC=10\), and then invokes a large language model, GPT-4, using a prompt that combines embedding scores and semantic context to produce a final 1-to-1 match,

\[
LK(a,X_k)=\mathrm{PromptLink}(a,X_k).
\]

This entity-linking stage has two roles. Factually, it maps heterogeneous EHR attributes into the ontology of a biomedical KG. Methodologically, it creates the bridge that allows patient observations to be expressed in the same representational space as pre-trained KG concepts. The reported ablations indicate that replacing PromptLink with BM25, BioBERT, or even random shuffling drops performance by only \(\sim 1\)–\(3\%\), which is presented as evidence of robustness in the overall framework [2507.19726].

A common misunderstanding would be to treat this stage as the entire contribution. The reported design instead places entity linking upstream of a contextualization mechanism; linked entities are only the substrate on which later hypergraph-based updates operate.

## 3. Hypergraph construction and contextualization mechanism

After entity linking, HypKG constructs a hypergraph \(\mathcal{H}=(V,E)\). The vertex set \(V\) is the union of all KG entities linked to any EHR attribute. Each patient visit or encounter \(\rho_p\) becomes a hyperedge \(e\subseteq V\) connecting the subset of entities observed in that visit. Formally,

\[
\mathcal{H}=(V,E),\quad E=\{e_1,\dots,e_M\},\quad e_i\subseteq V.
\]

This choice is central: the framework uses hyperedges to encode \(n\)-way relations, such as a patient encounter involving multiple diagnoses and medications, rather than collapsing encounters into pairwise edges. In the language of the paper, this directly encodes patient-to-multiple-diagnosis/medication structure [2507.19726].

Contextualization is then performed by a hypergraph transformer. Each node \(v\in V\) is initialized with an embedding \(h_v^{(0)}\in\mathbb{R}^{d_{KG}}\) from a pre-trained KG embedding, such as ComplEx. Each hyperedge \(e\) receives a learnable embedding \(h_e^{(0)}\in\mathbb{R}^{d_{KG}}\). HypKG stacks \(L\) layers of message passing in a set-transformer style with alternating updates:

\[
h_v^{(l+1)}=\sigma\!\Bigl(\sum_{e\ni v} W_1^{(l)}h_e^{(l)} + b_1^{(l)}\Bigr),
\]

\[
h_e^{(l+1)}=\sigma\!\Bigl(\sum_{v\in e} W_2^{(l)}h_v^{(l)} + b_2^{(l)}\Bigr).
\]

Within each aggregation, plain linear combination is replaced by multi-head attention over the relevant set of inputs. For a set \(S\), with stacked embeddings \(\mathbf{Z}_S\in\mathbb{R}^{|S|\times d}\), one head computes

\[
\mathrm{Attention}_i(\mathbf{Z}_S)
=
\mathrm{softmax}\!\Bigl(\frac{Q_iK_i^\top}{\sqrt{d/H}}\Bigr)V_i,
\]

where \(Q_i=\mathbf{Z}_SW_{Q,i}\), \(K_i=\mathbf{Z}_SW_{K,i}\), and \(V_i=\mathbf{Z}_SW_{V,i}\). The full output is the concatenation \(\big\Vert_{i=1}^H \mathrm{Attention}_i(\mathbf{Z}_S)\).

The final node embeddings \(h_v^{(L)}\), or pooled versions of them, become patient or KG-concept representations for downstream prediction. The attention weights can also be modulated by task-specific queries so that the transformer is guided by the classification objective. This suggests that contextualization in HypKG is not purely unsupervised structural smoothing; it is partially shaped by the target task.

## 4. Optimization and downstream prediction

HypKG is trained for downstream healthcare tasks by placing a small multilayer perceptron on top of the final patient embedding \(z_p\) to predict \(\hat y\). The tasks described are multi-label phenotyping in MIMIC-III and binary PSCI prediction in PROMOTE [2507.19726].

The per-sample prediction loss is binary cross-entropy over labels \(k\),

\[
\mathcal{L}_{\mathrm{BCE}}
=
-\sum_k \Bigl[y_k\log \hat y_k + (1-y_k)\log(1-\hat y_k)\Bigr].
\]

For multi-label settings, the loss is summed over all labels. A regularization term, such as weight decay or \(L_2\) regularization on embeddings, is added as \(\mathcal{L}_{\mathrm{reg}}\). The joint objective is

\[
\mathcal{L}
=
\lambda_1\,\mathcal{L}_{\mathrm{prediction}}
+
\lambda_2\,\mathcal{L}_{\mathrm{reg}},
\]

with \(\lambda_1=1\) in practice and \(\lambda_2\) chosen to stabilize training.

The optimization design reflects the framework’s stated goal of jointly learning contextualized representations for both KGs and patients. This suggests that HypKG is intended not only to improve patient-level prediction, but also to modify the embedding geometry of the linked KG in ways that reflect clinical context.

## 5. Experimental configuration and empirical results

The biomedical KG used in the reported experiments is iBKH from Su et al., containing 2.38 million entities across 11 semantic types and 48 million relation triples. For tractability, each node is subsampled to retain its top-\(K=800\) highest-degree relations, and ComplEx embeddings with \(d_{KG}=128\) are trained via PyKEEN [2507.19726].

Two real-world EHR datasets are used. MIMIC-III contains 12,353 encounters, 7,423 medical codes spanning diagnoses, prescriptions, and procedures, and 25 binary phenotyping labels. PROMOTE contains 7,780 stroke patients, 2,595 codes composed of ICD-10 codes and medications, and 1 binary PSCI label. Evaluation metrics are Accuracy, AUROC, AUCPR, and Macro-F1, with Macro-F1 macro-averaged for MIMIC-III. Test scores are reported at the epoch with highest validation AUROC and averaged over 5 runs.

Three embedding baselines are compared: a binary embedding represented as a 0/1 vector of length equal to the number of codes; a mean-pooled KG embedding formed by averaging the linked 128-dimensional KG vectors; and the full HypKG embedding, also 128-dimensional.

HypKG is reported to achieve the highest AUROC, AUCPR, and Macro-F1 on both datasets. On MIMIC-III, the hypergraph model reaches AUROC \(\approx 84.3\%\), compared with \(80.7\%\) for the binary baseline and \(80.8\%\) for the mean-pooled KG baseline, corresponding to an \(\approx 12.2\%\) relative gain over KG alone. On PROMOTE, HypKG reaches AUROC \(\approx 67.4\%\), compared with \(63.1\%\) and \(60.6\%\) for the alternative embeddings, an \(\approx 9.7\%\) relative gain [2507.19726].

Ablation results are used to attribute performance to specific components. Replacing PromptLink with BM25, BioBERT, or random shuffling changes performance by only \(\sim 1\)–\(3\%\). Changing the KG embedding model among TransE, ComplEx, and CompGCN yields similar behavior, with ComplEx+Large reported as best. Joint-modeling ablations, namely “KG Only” with random hyperedges and “EHR Only” with random embeddings, each lose \(\sim 5\)–\(15\%\) AUROC. Among hypergraph variants, HypKG outperforms HGTN, HyperGCN, HCHA, and HypEHR by \(1\)–\(4\%\) AUROC on MIMIC-III, which the paper attributes to the task-guided transformer.

## 6. Interpretation, representational effects, and extensions

The paper presents HypKG as a form of hypergraph-based contextualization with three main implications: it adapts KG embeddings to patient subpopulations, refines the global KG by down-weighting edges that are never reinforced by patient context, and yields richer, lower-dimensional patient representations for downstream tasks [2507.19726].

A qualitative case study is offered in terms of embedding geometry. After training, cosine similarities among certain diagnosis–drug pairs, including “Insulin” and “Acetaminophen” in diabetic patients, increase by more than \(0.1\) even though no direct KG edge exists. In the interpretation given, this indicates that HypKG pulls together entities with strong co-occurrence in patient visits. This suggests that the model is not limited to preserving explicit KG topology; it can also induce clinically contextual associations in the learned space.

The framework is also described as general beyond healthcare. The same pipeline—entity linking, hypergraph construction, and set-transformer contextualization—could be applied to e-commerce, social networks, or recommender systems, with PromptLink replaced by a domain-appropriate linker and the hypergraph transformer retrained for target tasks. This is presented as a prospective extension rather than as an experimentally established result.

Future directions named for the framework include continual adaptation as new EHR data arrive through incremental hypergraph updates, contrastive losses between KG-only and HypKG embeddings, and incorporation of rich literal features such as lab values and time series into node embeddings or edge features. These proposals indicate that HypKG is positioned as a modular contextualization framework rather than a closed architecture. Its open-source implementation and supplementary materials are cited as containing pseudocode, notation tables, dataset details, and ablation-study tables, reinforcing that the reported method is intended to be reproducible and extensible [2507.19726].

Source: https://www.emergentmind.com/topics/hypkg