How not to Stitch Representations to Measure Similarity: Task Loss Matching versus Direct Matching

András Balogh, Márk Jelasity

Abstract

Measuring the similarity of the internal representations of deep neural networks is an important and challenging problem. Model stitching has been proposed as a possible approach, where two half-networks are connected by mapping the output of the first half-network to the input of the second one. The representations are considered functionally similar if the resulting stitched network achieves good task-specific performance. The mapping is normally created by training an affine stitching layer on the task at hand while freezing the two half-networks, a method called task loss matching. Here, we argue that task loss matching may be very misleading as a similarity index. For example, it can indicate very high similarity between very distant layers, whose representations are known to have different functional properties. Moreover, it can indicate very distant layers to be more similar than architecturally corresponding layers. Even more surprisingly, when comparing layers within the same network, task loss matching often indicates that some layers are more similar to a layer than itself. We argue that the main reason behind these problems is that task loss matching tends to create out-of-distribution representations to improve task-specific performance. We demonstrate that direct matching (when the mapping minimizes the distance between the stitched representations) does not suffer from these problems. We compare task loss matching, direct matching, and well-known similarity indices such as CCA and CKA. We conclude that direct matching strikes a good balance between the structural and functional requirements for a good similarity index.

Introduction

Understanding the internal representations of deep neural networks (DNNs) is crucial for interpretability. Similarity indices typically fall into two categories: structural (geometric properties of activations) and functional (task-specific performance). Model stitching is a popular functional approach where a "stitching layer" connects two models. Traditionally, this layer is trained via Task Loss Matching (TLM) to minimize final task error. However, TLM often yields counter-intuitive results, such as suggesting high similarity between early and late layers.

The Failure of Task Loss Matching

The primary issue with TLM is the "fooling" phenomenon. The stitching layer often learns to produce out-of-distribution (OOD) representations that the receiver network can still classify correctly, even if they bear no true resemblance to the original target representations. This "hacking" of the receiver network leads to high task performance (and thus high similarity scores) without actual representational alignment.

Comparison of TLM and DM across architectures and metrics. TLM shows high similarity and high OOD separability for distant layers, while DM remains more localized and in-distribution.

As shown in the heatmaps above, TLM similarity scores often remain high far from the diagonal, particularly in Vision Transformers (ViT). Correspondingly, the OOD separability (AUROC) for these stitches is often near 1.0, indicating that the stitched representations are easily distinguishable from real data.

Methodology: Direct Matching

To address these flaws, the authors propose Direct Matching (DM). Instead of optimizing for task loss, DM trains the stitching layer to minimize the Frobenius distance between the source and target representations. This forces the stitch to stay close to the target distribution, acting as a natural regularizer against adversarial-like OOD representations.

Results and Discussion

The effectiveness of DM is most apparent in "sanity checks" (intra-network stitching). A robust similarity index should identify a layer as being most similar to itself.

Sanity check results for various similarity indices. DM achieves 100% accuracy in identifying corresponding layers, whereas TLM fails significantly, especially in ViT models.

The quantitative results demonstrate that DM achieves 100% accuracy in intra-network layer identification across both ResNet-18 and ViT-Ti. In contrast, TLM drops to 24.17% for ViT-Ti. Furthermore, DM maintains significantly higher accuracy in inter-network identification (matching corresponding layers between different models) compared to TLM.

Conclusion

While TLM is a common standard for functional similarity, its tendency to create OOD representations makes it a deceptive metric. Direct Matching provides a superior alternative by balancing structural constraints with functional goals. It passes critical sanity checks that TLM fails and ensures that stitched representations remain within the distribution of the receiver network.

Summary updated

BibTeX
@inproceedings{balogh2025how,
  author = {András Balogh and Márk Jelasity},
  title = {{How not to Stitch Representations to Measure Similarity: Task Loss Matching versus Direct Matching}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2025},
  doi = {10.1609/aaai.v39i15.33698},
}
Figure