Similarity of Neural Networks with Gradients

Shuai Tang, Wesley J. Maddox, Charlie Dickens, Tom Diethe, Andreas C. DamianouarXiv 2020

TL;DR

This paper introduces a novel method for comparing neural networks by combining both feature activations and loss gradients into a unified kernel representation. By incorporating sketching techniques (CountSketch), the proposed approach can efficiently compute network similarities across different datasets and varying dataset sizes, outperforming existing feature-only or gradient-only metrics.

What Problem Does This Paper Address?

Understanding and quantifying the similarity between neural networks is crucial for transfer learning, meta-learning, and model diagnostics. For instance, how similar are two identical architectures trained on different datasets, or two different architectures trained on the same data?

Existing similarity metrics, such as Centred Kernel Alignment (CKA), primarily focus on intermediate feature maps (activations). However, feature-only methods ignore the underlying learning task (the loss landscape) and completely break down when comparing models trained on datasets of different sizes. Other approaches, like Task2Vec, capture task information via the Fisher Information Matrix but require an expensive reference model and are difficult to parallelize. This paper tackles these challenges by creating a scalable representation that captures both data features and task gradients.

Methods

The authors propose a two-step framework: representation construction and similarity indexing.

  1. Representation via Features and Gradients: For a given layer, the method extracts both the feature vectors $(f_l)_n$ and the gradients of the loss function with respect to those features $(g_l)_n$.
  2. Two separate positive semi-definite (PSD) kernel matrices are constructed: $K_f$ (for features) and $K_g$ (for gradients).
  3. These matrices are merged using the Hadamard product ($K = K_f \circ K_g$), which preserves the PSD property and implicitly defines a joint feature-task mapping.
  4. Sketching for Scalability and Invariance: Storing and multiplying full kernel matrices scales poorly ($O(N^2)$ memory and $O(N^3)$ time, where $N$ is the number of samples). To solve this, the authors leverage CountSketch (a random linear projection technique) to compress the representations into a fixed dimension $M \ll N$. This dimensionality reduction not only speeds up computation to $O(M^3)$ but also enables the comparison of models trained on entirely different dataset sizes.

Finally, the similarity of these sketched matrices is evaluated using either Centred Kernel Alignment (CKA) or Normalised Bures Similarity (NBS).

Datasets

The method was evaluated using ResNet-18 and ResNet-50 architectures trained on three classic image classification benchmarks:

  • CIFAR-10: 10 classes of natural images.
  • CIFAR-100: 100 classes of natural images.
  • SVHN (Street View House Numbers): 10 classes of digit images.

Pretrained ImageNet models were also utilized as base models to evaluate task adaptation and kernel mean embeddings.

Key Insights and Ideas

  • The "Location + Direction" Intuition: Feature vectors $f_n$ encode where the projected samples reside in the latent space, while gradient vectors $g_n$ encode where they need to move and how intensely to minimize the loss. Combining them provides a holistic view of both data distribution and task geometry.
  • Sketching as an Alignment Tool: Traditionally, sketching is used purely for speed. Here, it serves a dual purpose: by projecting different-sized datasets into a fixed number of "buckets" ($M$), it acts as a mathematical bridge allowing CKA to compare models across entirely different datasets.
  • Harder Tasks Generalize Better: Models trained on the more complex CIFAR-100 dataset show high similarity to the early and intermediate layers of models trained on simpler datasets (CIFAR-10 and SVHN). This implies that harder tasks force the network to learn more generalized, transferable features.

Key Results

The authors validated their method against three intuitive desiderata: models with different initializations on the same dataset should be highly similar; natural image models (CIFAR-10/100) should be more similar to each other than to a digit model (SVHN); and CIFAR-10 should be closer to SVHN than CIFAR-100 due to sharing the same number of classes (10).

As shown below, combining features and gradients satisfies all three criteria, whereas feature-only or gradient-only methods yield uninterpretable results across different datasets:

Figure 1. Comparison among models trained on individual tasks. Rows indicate feature-only, gradient-only and our representations respectively. First three columns are using CKA as the similarity index and the last three NBS. As ten models are trained on each task, each marker is the mean of similarity scores, and each vertical bar represents the standard deviation. Combining gradients with the features as the representations helps both similarity indices to give intuitive similarity scores that meet the desiderata described in the main paper. (The x-axis represents the indices of residual blocks, and larger the index, closer the ResBlock is to the output layer.)

When analyzing individual layers, the authors demonstrated that CKA with the proposed representation clearly maps out how similarity shifts across different residual blocks:

Figure 5. Comparison between every two ResBlocks of ResNet-18 trained on three chosen tasks. The scores are computed using CKA with our representations.

Additionally, the authors showed that CKA consistently outperforms NBS because the normalization factor in NBS introduces ambiguity when differentiating source and target datasets.

Limitations and Future Work

While sketching makes the framework highly scalable, the authors note that applying sketching to certain downstream applications—like casting classification as kernel ridge regression—can lead to suboptimal analytical solutions compared to exact methods. Furthermore, providing strict theoretical guarantees on similarity accuracy under sketching for arbitrary data distributions remains a open challenge.

For future work, the authors propose leveraging this framework to build a model recommendation system that can automatically match query datasets with the most compatible pre-trained models from a large repository.

Summary updated

BibTeX
@misc{tang2020similarity,
  author = {Shuai Tang and Wesley J. Maddox and Charlie Dickens and Tom Diethe and Andreas C. Damianou},
  title = {{Similarity of Neural Networks with Gradients}},
  year = {2020},
  eprint = {2003.11498},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2003.11498},
}
Figure