Can Language Models Capture Graph Semantics? From Graphs to Language Model and Vice-Versa

Tarun Garg, Kaushik Roy, Amit ShetharXiv 2022

TL;DR

While infusing Knowledge Graphs (KGs) into Transformer-based language models improves performance on downstream tasks, this paper reveals that the models fail to truly compress and retain the graph's underlying semantics. The authors demonstrate that this limitation stems from a fundamental architectural mismatch: directed, sparse, and typed Knowledge Graphs cannot be adequately captured by the undirected, fully connected nature of Transformer attention matrices.

What Problem Does This Paper Address?

Knowledge Graphs are excellent at representing structured, relational, and directional data (for example, showing that a specific drug treats a particular disease). However, modern deep learning models require data to be flattened into continuous vector embeddings. While this vector compression often helps models make better predictions, it has remained an open question whether the language models actually "understand" or retain the full semantic structure of the graph.

The authors investigate a core question: If a deep learning model compresses a graph into a vector space, can it accurately reconstruct that same graph with its semantics intact?

Methods

The authors base their study on the Graph Convolution Transformer (GCT), an architecture designed to blend domain knowledge graphs with Transformer attention mechanisms. Typically, GCT treats the Transformer's attention matrix as a fully connected graph of tokens.

To rigorously test whether the model truly preserves graph semantics, the authors introduced two key modifications to the standard GCT training pipeline:

  1. Layer-by-Layer Regularization: They enforced Kullback-Leibler (KL) divergence minimization at every single transformer block (rather than just globally). This penalizes drastic changes in attention weights between consecutive layers, forcing the model to try and preserve the graph's structure as information flows deeper.
  2. Isolating the Loss Function: They created a Modified Loss Function consisting only of the KL divergence regularization term, completely removing the task-specific cross-entropy loss. This allowed them to evaluate the model's pure capacity for information retention without any feedback from prediction labels.

Finally, they attempted to reverse-engineer and reconstruct the original Knowledge Graph directly from the trained model's final attention matrix using token transition probabilities.

Figure 1: An illustration showing the procedure to generate the triple

Datasets

The authors evaluated their approach using the eICU Collaborative Research Database, a prominent, freely available multi-center database containing anonymized clinical data. They extracted patient-specific details regarding diagnoses and treatments to build medical knowledge graphs capturing critical clinical relationships (e.g., diagnosis-to-procedure mappings).

Key Insights and Ideas

  • The Architectural Mismatch: The primary reason Transformers fail to capture graph semantics is structural. Knowledge Graphs are directed, sparse, and typed (edges have specific meanings and directions). Transformer attention matrices, however, represent fully connected, weighted, and undirected graphs. Forcing a strict, directed schema into a fully connected undirected space inherently dilutes the semantics.
  • The "Max Attention" Flaw: Traditional methods of extracting graphs from language models rely on finding the maximum attention weight to determine the next entity in a triple. However, real-world KGs feature multiple valid relationships for a single entity. Relying solely on the maximum attention value inherently discards alternative, equally valid semantic paths.
  • The Illusion of Structural Preservation: Even when the model is forced to keep its layer-to-layer structural changes minimal, the actual semantic meaning of the graph is lost in translation.
Figure 2: (a) The first transition transition talks about calculating conditional probabilities based on the occurrence of entity relation pairs. This results in the prior attention matrix.(b) Second transition shows the relational mapping of the context through Graph Convolution Transformer. The darker edged signifies strong relationship between the linked entities. (c) Attention matrix is then used to reproduce the Knowledge Graph based on the probabilities

Key Results

The quantitative experiments confirmed the authors' hypothesis regarding the limitations of Transformers:

  • When using the Modified Loss Function (KL-divergence only), the layer-to-layer loss dropped significantly (stabilizing around 0.112), meaning the model successfully kept the attention matrix structure uniform across layers.
  • However, because the model was no longer guided by task labels, its predictive capabilities tanked: AUC-ROC scores dropped sharply to a range of 0.236–0.354, compared to the 0.408–0.608 achieved under the Original Loss Function.
  • Most importantly, despite the low structural loss, the reconstructed graphs failed to capture the true underlying semantics of the input graph, frequently hallucinating unwanted or incorrect relations when data was incomplete.
Precision, Recall and loss values with edited vs original loss function

Limitations and Future Work

Limitations

The study is constrained by the current design of attention-extraction mechanisms. Choosing a single "maximum attention" value prevents the model from capturing one-to-many relationships. Furthermore, when faced with incomplete data, the attention weights still generate significant values, leading the model to predict false or irrelevant relationships.

Future Work

To overcome these boundaries, the authors point toward neurosymbolic AI as a vital path forward. Future architectures need to move away from flattening graphs into standard token-to-token attention maps. Instead, researchers must develop graph neural networks and language models capable of natively preserving the rich, directional, and typed constraints of symbolic knowledge graphs.

Summary updated

Abstract

Knowledge Graphs are a great resource to capture semantic knowledge in terms of entities and relationships between the entities. However, current deep learning models takes as input distributed representations or vectors. Thus, the graph is compressed in a vectorized representation. We conduct a study to examine if the deep learning model can compress a graph and then output the same graph with most of the semantics intact. Our experiments show that Transformer models are not able to express the full semantics of the input knowledge graph. We find that this is due to the disparity between the directed, relationship and type based information contained in a Knowledge Graph and the fully connected token-token undirected graphical interpretation of the Transformer Attention matrix.

Empirical ResultsBrowse all benchmarks →

BenchmarkMethodMetricScoreProtocol / Evidence
eICU · dataGCT, Modified Loss functionAUC-PR0.086Mortality Prediction · loss-variant=modified · training-steps=2000
Source quote
L136: 2000 │ 0.086 │ 0.328 │ 0.112 │ 2000 │ 0.145 │ 0.548 │ 1.332
eICU · dataGCT, Original Loss functionAUC-PR0.145Mortality Prediction · loss-variant=original · training-steps=2000
Source quote
L136: 2000 │ 0.086 │ 0.328 │ 0.112 │ 2000 │ 0.145 │ 0.548 │ 1.332
eICU · dataGCT, Modified Loss functionAUC-ROC0.328Mortality Prediction · loss-variant=modified · training-steps=2000
Source quote
L136: 2000 │ 0.086 │ 0.328 │ 0.112 │ 2000 │ 0.145 │ 0.548 │ 1.332
eICU · dataGCT, Original Loss functionAUC-ROC0.548Mortality Prediction · loss-variant=original · training-steps=2000
Source quote
L136: 2000 │ 0.086 │ 0.328 │ 0.112 │ 2000 │ 0.145 │ 0.548 │ 1.332
eICU · dataGCT, Modified Loss functionloss0.112Mortality Prediction · loss-variant=modified · training-steps=2000
Source quote
L136: 2000 │ 0.086 │ 0.328 │ 0.112 │ 2000 │ 0.145 │ 0.548 │ 1.332
eICU · dataGCT, Original Loss functionloss1.332Mortality Prediction · loss-variant=original · training-steps=2000
Source quote
L136: 2000 │ 0.086 │ 0.328 │ 0.112 │ 2000 │ 0.145 │ 0.548 │ 1.332
BibTeX
@misc{garg2022can,
  author = {Tarun Garg and Kaushik Roy and Amit Sheth},
  title = {{Can Language Models Capture Graph Semantics? From Graphs to Language Model and Vice-Versa}},
  year = {2022},
  eprint = {2206.09259},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2206.09259},
}
Figure