Venue: NAACL 2024

# ContraSim - Analyzing Neural Representations Based on Contrastive Learning

Adir Rahamim

adir.rahamim@cs.technion.ac.il

Yonatan Belinkov

belinkov@technion.ac.il

Technion - Israel Institute of Technology

###### Abstract

Recent work has compared neural network representations via similarity-based analyses to improve model interpretation. The quality of a similarity measure is typically evaluated by its success in assigning a high score to representations that are expected to be matched. However, existing similarity measures perform mediocrely on standard benchmarks. In this work, we develop a new similarity measure, dubbed ContraSim, based on contrastive learning. In contrast to common closed-form similarity measures, ContraSim learns a parameterized measure by using both similar and dissimilar examples. We perform an extensive experimental evaluation of our method, with both language and vision models, on the standard layer prediction benchmark and two new benchmarks that we introduce: the multilingual benchmark and the image-caption benchmark. In all cases, ContraSim achieves much higher accuracy than previous similarity measures, even when presented with challenging examples. Finally, ContraSim is more suitable for the analysis of neural networks, revealing new insights not captured by previous measures.1

Footnote 1: Code is available at: [https://github.com/technion-cs-nlp/ContraSim](https://github.com/technion-cs-nlp/ContraSim).

+
Footnote †: Proceedings of the 2024 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies (Volume 1: Long Papers), pages 6325–6339

+
Footnote †: Proceedings of the 2024 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies (Volume 1: Long Papers), pages 6325–6339

## 1 Introduction

Representation learning has been key in the success of deep neural networks (NNs) on many tasks. However, the resulting representations are opaque and not easily understood. A recent line of work analyzes internal representations by comparing two sets of representations, for instance from two different models. The choice of similarity measure is crucial and much work has been devoted to developing various such measures Raghu et al. (2017); Morcoso et al. (2018); Kornblith et al. (2019); Wu et al. (2020). Similarity-based analyses may shed light on how different datasets, architectures, etc., change the model's learned representations, and improve their interpretability. For example, a similarity analysis showed that lower layers in different language models are more similar to each other, while fine-tuning affects mostly the top layers Wu et al. (2020).

Various similarity measures have been proposed for comparing representations, among them the most popular ones are based on centered kernel alignment (CKA) Kornblith et al. (2019) and canonical correlation analysis CCA Hotelling (1936); Morcoso et al. (2018). They all share a similar methodology: given a pair of feature representations _of the same input_, they estimate the similarity between them, without considering other examples. However, they all perform mediocrely on standard benchmarks. Thus, we might question the reliability of their similarity scores, as well as the validity of interpretability insights derived from them.

Motivated by this, we introduce ContraSim, a new similarity measure for interpreting NNs, based on contrastive learning (CL) Chen et al. (2020); He et al. (2020). Contrary to prior work (e.g., Raghu et al., 2017; Kornblith et al., 2019), which defines closed-form general-purpose similarity measures, ContraSim is a learnable similarity measure that uses examples with a high similarity (the _positive_ set) and examples that have a low similarity (the _negative_ set), to train an encoder that maps representations to the space where similarity is measured. In the projected space, representation similarity is maximized with positive examples and minimized with negative examples. Our approach allows specializing the similarity measure to a particular domain, to obtain a more reliable and specific analysis. The similarity between projected representations is determined using a simpler closed-form measure.

We experimentally evaluate ContraSim on standard benchmark for similarity measures - the layer prediction benchmark Kornblith et al. (2019), and two new benchmarks we introduce in this paper:

the multilingual benchmark and the image-caption benchmark. In experiments with both language and vision models and multiple datasets, ContraSim outperforms common similarity measures. In addition, we investigate a more challenging scenario, where during evaluation instead of choosing a random sentence, we retrieve a highly similar sentences as confusing examples, using the Facebook AI Similarity Search (FAISS) library Johnson et al. (2019). While other similarity measures are highly affected by this change, our method maintains a high accuracy with a very small degradation. We attribute this to the highly separable representations that our method learns. Even when ContraSim is trained on data from one domain/task and evaluated on data from another domain/task, it achieves superior performance.

Through ablations, we demonstrate that the CL procedure is crucial to the success of the method and only maximizing the similarity of positive examples is not sufficient. Furthermore, we demonstrate that ContraSim reveals new insights not captured by previous similarity measures. For instance, CKA suggests that the BERT language model Devlin et al. (2019) is more similar to vision models than GPT-2 Radford et al. (2019). Our analysis indicates that both BERT and GPT-2 create representations that are equally similar to the vision ones.

## 2 Related Work

Many studies have used similarity measures for the interpretability of NNs. For instance, Kornblith et al. (2019) showed that adding too many layers to a convolutional neural network, trained for image classification, hurts its performance. Using CKA, they found that more than half of the network's layers are very similar to the last. They further found that two models trained on different image datasets (CIFAR-10 and CIFAR-100, Krizhevsky et al.2009) learn representations that are similar in the shallow layers. Similar findings were noted for language models by Wu et al. (2020). The latter also evaluated the effect of fine-tuning on language models and found that the top layers are most affected by fine-tuning. Kornblith et al. (2019) and Morcos et al. (2018) found that increasing the model's layer width results in more similar representations between models. Raghu et al. (2017) provided an interpretation of the learning process by examining how similar representations were during the training process compared to final representations. They found that networks converge from bottom to top. In all this line of work, the similarity is computed using only similar examples, using functional closed-form measures. In contrast, we use both positive and negative samples in a learnable similarity measure, which allows adaptation to specific tasks.

Separate work employs contrastive learning for representation learning He et al. (2020); Chen et al. (2020) and data retrieval Karpathy et al. (2014); Huang et al. (2008). In contrast to that line of work, we borrow the contrastive learning formulation for similarity-based interpretations of deep networks.

## 3 Problem Setup

Let \(\mathbb{X}=\{(x_{1}^{(i)},x_{2}^{(i)})\}_{i=1}^{N}\) denote a set of \(N\) examples, and \(\mathbb{A}=\{(\mathbf{a}_{1}^{(i)},\mathbf{a}_{2}^{(i)})\}_{i=1}^{N}\) the set of representations generated for the examples in \(\mathbb{X}\). A representation is a high-dimensional vector of neuron activations. Representations may be created by different models, different layers of the same model, etc. For instance, \(x_{1}^{(i)}\) and \(x_{2}^{(i)}\) may be the same input, with \(\mathbf{a}_{1}^{(i)}\) and \(\mathbf{a}_{2}^{(i)}\) representations of that input in different layers.

Our goal is to obtain a scalar similarity score, which represents the similarity between the two sets of representations, \(\mathbf{a}_{1}^{(i)}\) and \(\mathbf{a}_{2}^{(i)}\), and ranges from \(0\) (no similarity) to \(1\) (identical representations). That is, we define \(\mathbf{X}_{1}\in\mathbb{R}^{N\times p_{1}}\) as a matrix of \(p_{1}\)-dim activations of \(N\) data points, and \(\mathbf{X}_{2}\in\mathbb{R}^{N\times p_{2}}\) as another matrix of \(p_{2}\)-dim activations of \(N\) data points. We seek a similarity measure, \(s(\mathbf{X}_{1},\mathbf{X}_{2})\).

## 4 ContraSim

In this section we introduce ContraSim, a similarity index for measuring the similarity of neural network representations. Our method uses a trainable encoder, which first maps representations to a new space and then measures the similarity of the projected representations. Formally, let \(e_{\theta}\) denote an encoder with trainable parameters \(\theta\), and assume two representations \(\mathbf{a}_{1}\) and \(\mathbf{a}_{2}\). In order to obtain a similarity score between \(0\) and \(1\), we first apply L2 normalization to the encoder outputs: \(\mathbf{z}_{1}=e_{\theta}(\mathbf{a}_{1})/\|e_{\theta}(\mathbf{a}_{1})\|\) (and similarly for \(\mathbf{a}_{2}\)). Then their similarity is calculated as:

\[s(\mathbf{z}_{1},\mathbf{z}_{2})\] 

where \(s\) is a simple closed-form similarity measure for two vectors. Throughout this work we use dot product for \(s\).

For efficiency reasons, we calculate the similarity between batches of the normalized encoder representations, dividing by the batch size \(n\):

\[\frac{1}{n}\sum_{i=1}^{n}\left(\mathbf{z}_{1}^{i}\cdot\mathbf{z}_{2}^{i}\right) \tag{2}\]

If the representations \(a_{1}\) and \(a_{2}\) have the same dimensionality, ContraSim can be trained with a single encoder shared for both representations. In the case that \(a_{1}\) and \(a_{2}\) have different dimensions, two different encoders are trained, \(e_{\theta 1}\) and \(e_{\theta 2}\), one for each representation set. By that, ContraSim can calculate the similarity of representations with different dimensionality, as each encoder has a different input dimension, but both encoders share the same output dimension. Experiments with representations with different dimensionality are in Section6.3.3.

Training.None of the similarity measures commonly used in NNs analysis uses negative examples to estimate the similarity of a given pair (Section2). Given two examples, these measures output a scalar that represents the similarity between them, without leveraging data from other examples. However, based on knowledge from other examples, we can construct a better similarity measure. In particular, for a given example \(x^{(i)}\in\mathbb{X}\) with its encoded representation \(\mathbf{z}_{i}\), we construct a set of _positive_ example indices, \(P(i)=\{p_{1},...,p_{q}\}\), and a set of _negative_ example indices, \(N(i)=\{n_{1},...,n_{t}\}\). The choice of these sets is task-specific and allows one to add inductive bias to the training process.

We train the encoder to maximize the similarity of \(\mathbf{z}_{i}\) with all the positive examples, while at the same time making it dis-similar from the negative examples. We leverage ideas from contrastive learning Chen et al. (2020); He et al. (2020), and minimize the following objective:

\[\mathcal{L}=\sum_{i\in I}\frac{-1}{|P(i)|}\log\frac{\sum_{p\in P(i)}\exp(\mathbf{ z}_{i}\cdot\mathbf{z}_{p}/\tau)}{\sum_{n\in N(i)}\exp(\mathbf{z}_{i}\cdot\mathbf{z}_{n}/\tau)} \tag{3}\]

with scalar temperature parameter \(\tau>0\). Here \(\mathbf{z}_{p}\) and \(\mathbf{z}_{n}\) are normalized encoder outputs of representations from the positive and negative groups, respectively. It is important to notice that we do not alter the original model representations, and the only trainable parameters are the encoder \(e\) parameters, \(\theta\). Our work uses negative examples and a trainable encoder for constructing a similarity measure. We evaluate these two aspects in the experimental section and show that using negative examples is an important aspect of our method. Combining the two leads to a similarity measure that outperforms current measures.

## 5 Similarity Measure Evaluation

For evaluation, we use the known layer prediction benchmark and two new benchmarks we design: the multilingual benchmark and the image-caption benchmark. We further propose a strengthened version of the last two using the FAISS software.

### Layer prediction benchmark

Proposed by Kornblith et al. (2019), a basic and intuitive benchmark is to assess the invariance of a similarity measure against changes to a random seed. Given two models differing only in their weight initializations, for each layer in the first model, among all layers of the second model, one can expect that a good similarity measure assigns the highest similarity for the architecturally-corresponding layer. Formally, let \(f\) and \(g\) be two models with \(k\) layers, and define \(f_{i}\) and \(g_{i}\) as the \(i^{th}\) layer of \(f\) and \(g\) models, respectively. After calculating the similarity of \(f_{i}\) to each layer of \(g\) (\(g_{1},\dots,g_{k}\)), the pair with the highest similarity is expected to be \((f_{i},g_{i})\). This benchmark counts the number of layers for which this pair was indeed the most similar, and divides by the total number of pairs. An illustration is found in Figure1.

The intuition behind this benchmark is that each layer captures different information about the input data. For example, Jawahar et al. (2019) showed that different layers of the BERT model capture different semantic information.

Figure 1: Layer prediction benchmark. Given two models differing only in weight initialization, A and B, for each layer in the first model, among all layers of the second model, the highest similarity should be assigned to the architecturally-corresponding layer.

 



### Multilingual benchmark

Multilingual models, such as Multilingual-BERT (Devlin et al., 2019), learn to represent texts in different languages in the same representation space. Interestingly, these models show cross-lingual zero-shot transferability, where a model is fine-tuned in one language and evaluated in a different language (Pires et al., 2019). Muller et al. (2021) analyzed this transferability and found that lower layers of the Multilingual-BERT align the representations between sentences in different languages.

Since multilingual models share similarities between representations of different languages, we expect that a good similarity measure should assign a high similarity to two representations of a sentence in two different languages. In other words, we expect similarity measures to be invariant to the sentence source language. Consider a multilingual model \(f\) and dataset \(\mathbb{X}\), where each entry consists of the same sentence in different languages. Let \((x_{1}^{(i)},x_{2}^{(i)})\in\mathbb{X}\) be a sentence written in two languages - language A and language B. The similarity between \(f(x_{1}^{(i)})\) and \(f(x_{2}^{(i)})\) should be higher than the similarity between \(f(x_{1}^{(i)})\) and the representation of a sentence in language B randomly chosen from \(\mathbb{X}\), i.e., \(f(x_{2}^{(j)})\), where \((x_{1}^{(j)},x_{2}^{(j)})\in\mathbb{X}\) is a randomly chosen example from \(\mathbb{X}\). The benchmark calculates the fraction of cases for which the correct translation was assigned the highest similarity. An illustration is found in Figure 2.

Additionally, we suggest a strengthened version of the multilingual benchmark, using FAISS. Instead of sampling random sentences in language B, we use FAISS to find the pair \((x_{1}^{(j)},x_{2}^{(j)})\in\mathbb{X}\), where \(x_{2}^{(j)}\neq x_{2}^{(i)}\), with the representation \(f(x_{2}^{(j)})\) that is most similar to \(f(x_{2}^{(i)})\), out of a large set of vectors pre-indexed by FAISS. This leads to a more challenging scenario, as the similarity between \(x_{1}^{(i)}\) and FAISS-sampled \(x_{2}^{(j)}\) is expected to be higher than the similarity between \(x_{1}^{(i)}\) and randomly chosen \(x_{2}^{(j)}\), increasing the difficulty of identifying the pair \((x_{1}^{(i)},x_{2}^{(i)})\) as the highest-similarity pair. Note that FAISS only affects the evaluation step, and is not used during ContraSim's training.

### Image-caption benchmark

Let \(\mathbb{X}\) be a dataset of images and their textual descriptions (captions), \(f\) be a computer vision model and \(g\) a language model. Given a pair of an image and its caption, \((m^{(i)},c^{(i)})\in\mathbb{X}\), a good similarity measure is expected to assign a high similarity to their representations - \(f(m^{(i)}),g(c^{(i)})\). In particular, this similarity should be higher than that of the pair of the same image representation \(f(m^{(i)})\) and some random caption's representation \(g(c^{(j)})\), where \(c^{(j)}\) is a randomly chosen caption from \(\mathbb{X}\).

The intuition behind this benchmark is that an image and its caption represent the same scene in a different way. Thus, their representations should have a higher similarity than that of the same image and some random caption. An illustration is found in Figure 3. As in the multilingual benchmark, we also propose a strengthened variant for the image-caption benchmark using FAISS. Rather than sampling a random caption \(c^{(j)}\), we use FAISS to find the pair \((m^{(j)},c^{(j)})\in\mathbb{X}\), where \(c^{(j)}\neq c^{(i)}\), with the representation \(g(c^{(j)})\) that is most similar to \(g(c^{(i)})\).

## 6 Experiments

Baselines.We compare ContraSim with the following standard baselines.

* **Centered Kernel Alignment (CKA)**: Proposed by Kornblith et al. (2019), CKA computes a kernel matrix for each matrix represen

Figure 3: The image–caption benchmark. \(r_{1}^{C}\) and \(r^{i}\) denote the representations of the caption and the image pair, respectively, and \(S_{1}\) is their similarity. \(r_{2}^{C}\) denotes the random caption representation, and \(S_{2}\) is the similarity between it and \(r^{i}\). \(S_{1}\) should be greater than \(S_{2}\).

Figure 2: The multilingual benchmark. \(r_{1}^{E}\) and \(r_{1}^{G}\) denote the representations of the same sentence in different languages, and \(S_{1}\) is their similarity. \(r_{2}^{E}\) represents the random sentence representation, and \(S_{2}\) is the similarity between it and \(r_{1}^{G}\). We expect \(S_{1}\) to be higher than \(S_{2}\).



tation input, and defines the scalar similarity index as the two kernel matrices' alignment. We use a linear kernel for CKA evaluation, as the original paper reveals similar results for both linear and RBF kernels. CKA is our main point of comparison due to its success in prior work and wide applicability.
* **PWCCA**: Proposed by Morcos et al. (2018), PWCCA is an extension of Canonical Correlation Analysis (CCA). Given two matrices, CCA finds bases for those matrices, such that after projecting them to those bases the correlation between the projected matrices is maximized. While in CCA the scalar similarity index is computed as the mean correlation coefficient, in PWCCA that mean is weighted by the importance each canonical correlation has on the representation.2 Footnote 2: PWCCA and SVCCA require the number of examples to be larger than the feature vector dimension, which is not possible to achieve in all benchmarks. Therefore, we compare with them in a subset of our experiments.

See Appendix A.4 for more details on the current methods. In the main body we report results of the more successful methods: CKA and PWCCA. Additional baseline are reported in Appendix A.5.

Ablations.In addition, we report the results of two new similarity measures, which use an encoder to map representations to the space where similarity is measured. However, in both methods we train \(e_{\theta}\) to only maximize the similarity between positive pairs:

\[\mathcal{L}_{max}=-s(\mathbf{z}_{1},\mathbf{z}_{2}) \tag{4}\]

where \(\mathbf{z}_{1}\) and \(\mathbf{z}_{2}\) are representations whose similarity we wish to maximize. We experiment with two functions for \(s\)--dot-product and CKA--and accordingly name these similarity measures DeepDot and DeepCKA. These methods provide a point of comparison where the similarity measure is trained, but _without negative examples_, in order to assess the importance of contrastive learning.

Encoders details.In all experiments, the encoder \(e_{\theta}\) is a two-layer multi-layered perceptron with hidden layer dimensions of \(512\) and \(256\), and output dimension of \(128\). We trained the encoder for 50 epochs for the layer prediction and 30 epochs for the multilingual and image-caption benchmarks. We used the Adam optimizer Kingma and Ba (2015) with a learning rate of \(0.001\) and a batch size of \(1024\) representations. We used \(\tau=0.07\) for ContraSim training.

### Layer prediction benchmark

#### 6.1.1 Setup

Recall that this benchmark evaluates whether a certain layer from one model is deemed most similar to its architecturally-corresponding layer from another model, where the two models differ only in their weight initialization. We repeat this process for all layers and 5 different model pairs, and report average accuracy. We experiment with both language and vision setups.

Models.For language experiments, we use the MultiBERTs Sellam et al. (2021), a set of 25 BERT models, differing only in their weights initialization. For vision experiments, we pre-train 10 visual transformer (ViT) models Dosovitskiy et al. (2020) on the ImageNet-1k dataset Russakovsky et al. (2015). Then we fine-tune them on CIFAR-10 and CIFAR-100 datasets Krizhevsky et al. (2009). Further details are available in Appendix A.3.

Datasets.In language experiments, we use word-level contextual representations generated on two English text datasets: the Penn Treebank Marcus et al. (1993) and WikiText Merity et al. (2016). For Penn TreeBank we generate 5005/10019 test/training representations, respectively; for WikiText we generate 5024/10023 test/training representations. Vision experiments are conducted using representations generated on CIFAR-10 and CIFAR-100. For both we generate 5000 and 10000 test and training representations, respectively.

Positive and Negative sets.Given a batch of representations of some model \(i\) at layer \(j\), we define its positive set as the representations at the same layer \(j\) of all models that differ from \(i\). The negative set is all representations from layers that differ from \(j\) (including from model \(i\)).

#### 6.1.2 Results

The results are shown in Table 1. In both language and vision evaluations, CKA achieves better results than PWCCA, consistent with the findings by Ding et al. (2021). DeepDot and DeepCKA perform poorly, with much lower results than PWCCA and CKA, revealing that maximizing the similarity is not satisfactory for similarity measure purposes. Our method, ContraSim, achieves excellent results.

 

When trained on one dataset's training set and evaluated on the same dataset's test set, ContraSim achieves perfect accuracy under this benchmark, with a large margin over CKA results. This holds for both language and vision cases. Even when trained on one dataset and evaluated over another dataset, ContraSim surpasses other similarity measures, showing the transferability of the learned encoder projection between datasets. This is true both when transferring across domains (in text, between news texts from the Penn Treebank and Wikipedia texts), and when transferring across classification tasks (in images, between the 10-label CIFAR-10 and the 100-label CIFAR-100).

### Multilingual benchmark

#### 6.2.1 Setup

This benchmark assesses whether a similarity measure assigns a high similarity to multilingual representations of the same sentence in different languages. Given a batch of (representations of) sentences \(b^{(i)}\) in language \(L_{i}\) and their translations \(b^{(j)}\) in language \(L_{j}\), we compute the similarity between \(b^{(i)}\) and \(b^{(j)}\), and the similarities between \(b^{(i)}\) and 10 randomly chosen batches of representations in language \(L_{j}\). If \(b^{(i)}\) is more similar to \(b^{(j)}\) than to all other batches, we mark success. (Alternatively, in a more challenging scenario, we use FAISS to find for each representation in each layer the 10 most similar representations in that layer.) We repeat this process separately for representations from different layers of a multilingual model, over many sentences and multiple language pairs, and report average accuracy per layer.3 Appendix A.1 gives more details.

Footnote 3: For deep similarity measures (DeepCKA, DeepDot, and ContraSim), upon training the encoder on examples from a pair of languages, \((L_{r},L_{q})\), \(r\neq q\), we evaluate it over all other distinct pairs of languages.

Model and Data.We use two multilingual models: multilingual BERT Devlin et al. (2019)4 and XLM-R Conneau et al. (2020a). We use the XNLI dataset Conneau et al. (2018), which has natural language inference examples, parallel in multiple languages. Each example in our dataset is a sentence taken from either the premise or hypothesis sets. We experiment with 5 typologically-different languages: English, Arabic, Chinese, Russian, and Turkish. We created sentence-level representations, with 5000 test 10000 training representations. As a sentence representation, we experiment with [CLS] token representations and with mean pooling of token representations, since Del and Fishel delFishel (2021) noted a difference in similarity in these two cases. We report results with [CLS] representations in the main body and with mean pooling in Appendix A.1; the trends are similar.

Footnote 4: [https://huggingface.co/bert-base-multilingual-cased](https://huggingface.co/bert-base-multilingual-cased)

Positive and Negative sets.Given a pair of languages and a batch of representations at some layer, for each representation we define its positive pair as the representation of the sentence in the different language, and its negative set as all other representations in the batch.

#### 6.2.2 Results

Results with multilingual BERT representations in Table 2 show our method's effectiveness. (Trends with XLM-R are consistent; Appendix A.1.2). Under random sampling evaluation (left block), ContraSim shows superior results over other similarity measures, despite being evaluated on language

\begin{table}
\begin{tabular}{l r r|l r r} \hline \hline  & Language & \multicolumn{4}{c}{Vision} \\ \hline  & Penn TreeBank & WikiText & \multicolumn{2}{c}{CIFAR-10} & CIFAR-100 \\ \hline PWCCA & 38.33 & 55.00 & PWCCA & 47.27 & 45.45 \\ CKA & 71.66 & 76.66 & CKA & 78.18 & 74.54 \\ DeepDot & 15.55 \(\pm\) 1.69 & 14.00 \(\pm\) 2.26 & DeepDot & 14.90 \(\pm\) 1.78 & 14.18 \(\pm\) 2.67 \\ DeepCKA & 16.66 \(\pm\) 3.16 & 19.66 \(\pm\) 1.63 & DeepCKA & 17.09 \(\pm\) 2.95 & 13.09 \(\pm\) 4.20 \\ \hline ContraSim & & & ContraSim & & \\ Penn & 100 \(\pm\) 0 & 85.45 \(\pm\) 1.62 & CIFAR-10 & 100 \(\pm\) 0 & 90.54 \(\pm\) 2.90 \\ Wiki & 94.00 \(\pm\) 4.66 & 100 \(\pm\) 0 & CIFAR-100 & 85.81 \(\pm\) 5.68 & 100 \(\pm\) 0 \\ \hline \hline \end{tabular}
\end{table}
Table 1: Layer prediction benchmark accuracy results for language and vision cases. For encoder-based methods we report mean and std over 5 random initializations. For ContraSim, we experiment with training with different datasets (rows) and evaluating on same or different datasets (columns).

 

pairs it hasn't seen at training. Using FAISS sampling (right block) further extends the gaps. While CKA results dropped by \(\approx 45\%\), DeepCKA dropped by \(\approx 51\%\), and DeepDot dropped by \(\approx 40\%\), ContraSim was much less affected by FAISS sampling (\(\approx 17\%\) drop on average and practically no drop in most layers). This demonstrates the high separability between examples of ContraSim, enabling it to distinguish even very similar examples. For all other methods, mid-layers have the highest accuracy, whereas for our method almost all layers are near \(100\%\) accuracy, except for the first 3 or 4 layers.

To further analyze this, we compare the original multilingual representations from the last layer with their projections by ContraSim's trained encoder. Figure 4 shows UMAP [26] projections for 10 English sentences and 10 Arabic sentences, before and after ContraSim encoding. The ContraSim encoder was trained on Arabic and English languages. The original representations are organized according to the source language (by shape), whereas ContraSim projects translations of the same sentence close to each other (clustered by color).

### Image-caption benchmark

#### 6.3.1 Setup

Given a test set \(\mathbb{X}\), consisting of pairs of an image representation generated by a CV model and its caption representation from a LM, we split \(\mathbb{X}\) to batches of size 64. For each batch, we compute the similarity between the image representations and their corresponding caption representations. We then sample 10 different caption batches, either randomly or using FAISS (as before), and compute the similarity between the image representation and each random/FAISS-retrieved caption representation. If the highest similarity is between the image representation and the original caption representation, we mark a success. For trainable similarity measures, we train with 5 different random seeds and average the results.

Models and Data.We use two vision models for image representations: ViT and ConvNext [19]; and two language models for text representations: BERT and GPT2 [8]. We use the Conceptual Captions dataset [20]. We use 5000 and 10000 pairs as test and training sets, respectively.

Positive and Negative sets.Given a batch of image representations with their corresponding caption representations, for each image representation

Figure 4: Original representations (left) are clustered by the source language (by shape). ContraSim (right) projects representations of the same sentence in different languages close by (by color).

\begin{table}
\begin{tabular}{c c c c|c c c c} \hline \hline  & \multicolumn{4}{c|}{Random} & \multicolumn{4}{c}{FAISS} \\ Layer & CKA & DeepCKA & DeepDot & ContraSim & CKA & DeepCKA & DeepDot & ContraSim \\ \hline
1 & \(71.7\pm 5.3\) & \(82.0\pm 6.4\) & \(63.3\pm 10.4\) & \(95.5\pm 5.4\) & \(20.1\pm 4.0\) & \(10.7\pm 2.6\) & \(29.9\pm 8.7\) & \(36.0\pm 10.7\) \\
2 & \(78.7\pm 4.4\) & \(86.4\pm 4.1\) & \(68.5\pm 9.9\) & \(95.0\pm 7.2\) & \(27.2\pm 5.5\) & \(12.3\pm 2.9\) & \(46.9\pm 9.8\) & \(33.0\pm 14.8\) \\
3 & \(86.8\pm 3.0\) & \(87.1\pm 3.2\) & \(70.4\pm 9.7\) & \(96.4\pm 6.7\) & \(41.9\pm 8.7\) & \(17.6\pm 4.2\) & \(51.5\pm 10.3\) & \(45.4\pm 20.5\) \\
4 & \(92.6\pm 1.4\) & \(91.5\pm 2.4\) & \(95.4\pm 3.4\) & \(99.9\pm 0.2\) & \(33.4\pm 7.0\) & \(15.2\pm 3.7\) & \(52.2\pm 8.6\) & \(72.4\pm 9.8\) \\
5 & \(88.3\pm 3.2\) & \(83.5\pm 5.2\) & \(94.7\pm 4.8\) & \(99.9\pm 0.0\) & \(49.3\pm 4.3\) & \(36.9\pm 6.3\) & \(42.4\pm 12.9\) & \(99.1\pm 0.8\) \\
6 & \(88.6\pm 3.4\) & \(86.4\pm 5.2\) & \(92.5\pm 5.4\) & \(100\pm 0\) & \(51.4\pm 5.5\) & \(39.9\pm 7.2\) & \(42.1\pm 12.3\) & \(99.5\pm 0.4\) \\
7 & \(88.8\pm 3.7\) & \(86.9\pm 5.0\) & \(92.6\pm 5.0\) & \(100\pm 0\) & \(53.0\pm 5.8\) & \(41.1\pm 7.7\) & \(45.7\pm 11.7\) & \(99.6\pm 0.3\) \\
8 & \(89.3\pm 3.6\) & \(85.2\pm 5.7\) & \(91.4\pm 7.0\) & \(100\pm 0\) & \(56.1\pm 5.8\) & \(45.0\pm 8.7\) & \(43.8\pm 13.4\) & \(99.7\pm 0.3\) \\
9 & \(88.1\pm 3.8\) & \(82.4\pm 5.6\) & \(89.1\pm 9.5\) & \(100\pm 0\) & \(53.3\pm 4.9\) & \(42.7\pm 8.5\) & \(39.2\pm 12.9\) & \(99.6\pm 0.3\) \\
10 & \(87.0\pm 3.5\) & \(80.3\pm 5.9\) & \(85.3\pm 10.3\) & \(100\pm 0\) & \(51.5\pm 5.3\) & \(42.4\pm 7.8\) & \(34.3\pm 12.2\) & \(99.5\pm 0.4\) \\
11 & \(86.7\pm 4.2\) & \(76.6\pm 6.4\) & \(79.7\pm 13.9\) & \(99.9\pm 0.0\) & \(52.4\pm 5.3\) & \(43.3\pm 8.5\) & \(31.4\pm 12.8\) & \(99.3\pm 0.5\) \\
12 & \(86.4\pm 3.4\) & \(63.8\pm 7.9\) & \(64.3\pm 19.7\) & \(99.9\pm 0.0\) & \(52.8\pm 4.5\) & \(32.3\pm 8.7\) & \(26.1\pm 21.9\) & \(98.9\pm 0.8\) \\ \hline \hline \end{tabular}
\end{table}
Table 2: Multilingual benchmark accuracy results. With random sampling (left block), ContraSim outperforms other similarity measures. Using FAISS (right block) further extends the gaps.

 

we define as a positive set its corresponding caption representation, and as a negative set all other representations in the batch.

#### 6.3.2 Results

Figure 5 demonstrates the strength of ContraSim. Under random sampling (green boxes), DeepCKA achieves comparable results to ContraSim, while DeepDot and CKA achieve lower results. However, using FAISS (red boxes) causes a big decrease in DeepCKA accuracy, while ContraSim maintains high accuracy. Furthermore, in 3 of 4 pairs we tested, FAISS sampling yielded better CKA accuracy than random sampling. This contradicts the intuition that similar examples at the sampling stage should make it harder for similarity measures to distinguish between examples. This might indicate that CKA suffers from stability issues. Finally, we report results with the multi-modal CLIP model Radford et al. (2021) in Table 6 (Appendix A.2). Because the model was pre-trained with contrastive learning, simple dot-product similarity works very well, so there is no need to learn a similarity measure in this case.

#### 6.3.3 ContraSim on different dimensions

We further evaluated ContraSim with different representation dimensions. We performed the same image-caption benchmark with the exception that we used different vision model sizes: ViT-large and ConvNext-base, both with 1024-dimensional representation vectors. As language models we used the same GPT2 and BERT models, with a 768-dimensional representation vector.

We trained a different encoder for each model, as opposed to the single encoder we trained in all other experiments. This enables ContraSim to be used with representations with different dimensions. Results are summarized in Table 3. We report results with FAISS sampling. Across all pairs, ContraSim achieves superior results.

## 7 Interpretability insights

Having shown the superiority of our method, we now discuss a few interpretability insights that arise from our evaluations, and are not revealed by previous similarity measures.

In the multilingual benchmark (Table 2, FAISS results), we found a much greater difference in accuracy between shallow and deep layers in ContraSim compared to previous similarity measures. Using previous similarity measures we might infer that there is no difference in the ability to detect the correct pairs across different layers. However, ContraSim shows that the difference in the ability

\begin{table}
\begin{tabular}{c c c} \hline \hline  & CKA & ContraSim \\ \hline ViT-large/GPT2 & 45.57 & 98.73 \\ ViT-large/BERT & 83.54 & 98.73 \\ ConvNext-base/GPT2 & 39.24 & 100 \\ ConvNext-base/BERT & 74.68 & 98.73 \\ \hline \hline \end{tabular}
\end{table}
Table 3: Image–caption benchmark accuracy results for model pairs with different dimensions. We report results using FAISS sampling. Despite different model dimensions, ContraSim consistently works best.

Figure 5: Image–caption benchmark results for 4 different model pairs. ContraSim works best, and is the only measure robust to FAISS sampling.

 

to detect the correct pair dramatically changes from shallow to deep layers. This raises an interesting insight regarding the evolution of representations across layers. For instance, Conneau et al. (2020b) used CKA to measure the similarity of representations of bilingual models of different languages and the same language (using back-translation). From their results it can be observed that there is no much difference in similarity of sentences from different languages and from the same language at the shallow and deep layers. Our results show that this difference is higher than found before.

In the image-caption benchmark (Figure 5), from the CKA results we might infer that BERT representations are more similar to computer vision representations than GPT2 representations. That is because with CKA, it is easier to detect the matching image-caption pair with BERT than it is with GPT2. However, ContraSim achieves a high accuracy in both BERT pairs and GPT2 pairs, which means that both language models about as similar to vision models, in contrast to what we may infer from previous similarity measures. This reveals a new understanding regarding the relationship between language and vision models. To the best of our knowledge, no prior work has done such a similarity analysis.

## 8 Conclusion

We proposed a new similarity measure for interpreting neural networks, ContraSim. By defining positive and negative sets we learn an encoder that maps representation to a space where similarity is measured. Our method outperformed other similarity measures under the common layer prediction benchmark and two new benchmarks we proposed: the multilingual benchmark and the image-caption benchmark. It particularly shines in strengthened versions of said benchmarks, where random sampling is replaced with finding the most similar examples using FAISS. Moreover, we show that even when ContraSim is trained on data from one domain/task and evaluated on data from another domain/task, it achieves superior performance. Considering ContraSim's superiority in all evaluations, we believe it is a better tool for the interpretability of neural networks, and have discussed a few insights revealed by ContraSim and not captured by previous methods.

Our new similarity measure benchmarks can facilitate work on similarity-based analyses of deep networks. The multilingual benchmark is useful for work on multilingual language models, while the image-caption benchmark may help in multimodal settings. In addition, since our method learns a parameterized measure, it may help train models with similarity objectives.

## 9 Limitations

Compared to existing methods, ContraSim needs access to a training set for the encoder training procedure. The training procedure itself is efficient, typically a matter of minutes.

## 10 Ethics Statement

Our work adds to the body of literature on the interpretability of neural networks and may mitigate their opacity. We do not foresee major risks associated with this work. However, a malicious actor could train ContraSim adversarially, assign poor similarity estimates, and lead to false analyses.

### Acknowledgements

This work was supported by an AI alignment grant from Open Philanthropy, the Israel Science Foundation (grant No. 448/20), and an Azrieli Foundation Early Career Faculty Fellowship.

## References

* Chen et al. [2020] Ting Chen, Simon Kornblith, Mohammad Norouzi, and Geoffrey Hinton. 2020. A simple framework for contrastive learning of visual representations. In _International conference on machine learning_, pages 1597-1607. PMLR.
* Conneau et al. [2020a] Alexis Conneau, Kartikay Khandelwal, Naman Goyal, Vishrav Chaudhry, Guillaume Wenzek, Francisco Guzman, Edouard Grave, Myle Ott, Luke Zettlemoyer, and Veselin Stoyanov. 2020a. Unsupervised cross-lingual representation learning at scale. In _Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics_, pages 8440-8451, Online. Association for Computational Linguistics.
* Conneau et al. [2018] Alexis Conneau, Ruty Rinott, Guillaume Lample, Adina Williams, Samuel R. Bowman, Holger Schwenk, and Veselin Stoyanov. 2018. Xnli: Evaluating cross-lingual sentence representations. In _Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing_. Association for Computational Linguistics.
* Conneau et al. [2020b] Alexis Conneau, Shijie Wu, Haoran Li, Luke Zettlemoyer, and Veselin Stoyanov. 2020b. Emerging cross-lingual structure in pretrained language models. In _Proceedings of the 58th Annual Meeting of 

the Association for Computational Linguistics_, pages 6022-6034.
* Del and Fishel (2021) Maksym Del and Mark Fishel. 2021. Establishing interlingua in multilingual language models. _arXiv preprint arXiv:2109.01207_.
* Deng et al. (2009) Jia Deng, Wei Dong, Richard Socher, Li-Jia Li, Kai Li, and Li Fei-Fei. 2009. Imagenet: A large-scale hierarchical image database. In _2009 IEEE conference on computer vision and pattern recognition_, pages 248-255. Ieee.
* Devlin et al. (2019) Jacob Devlin, Ming-Wei Chang, Kenton Lee, and Kristina Toutanova. 2019. BERT: Pre-training of deep bidirectional transformers for language understanding. In _Proceedings of the 2019 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, Volume 1 (Long and Short Papers)_, pages 4171-4186, Minneapolis, Minnesota. Association for Computational Linguistics.
* Ding et al. (2021) F Ding, J-S Denain, and J Steinhardt. 2021. Grounding representation similarity with statistical testing. _Advances in neural information processing systems_.
* Dosovitskiy et al. (2020) Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohu Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, et al. 2020. An image is worth 16x16 words: Transformers for image recognition at scale. In _International Conference on Learning Representations_.
* He et al. (2020) Kaiming He, Haoqi Fan, Yuxin Wu, Saining Xie, and Ross Girshick. 2020. Momentum contrast for unsupervised visual representation learning. In _Proceedings of the IEEE/CVF conference on computer vision and pattern recognition_, pages 9729-9738.
* Hotelling (1936) Harold Hotelling. 1936. Relations between two sets of variates. _Biometrika_, 28:321-377.
* Huang et al. (2008) Thomas S Huang, Charlie K Dagli, Shyamsundar Rajaram, Edward Y Chang, Michael I Mandel, Graham E Poliner, and Daniel PW Ellis. 2008. Active learning for interactive multimedia retrieval. _Proceedings of the IEEE_, 96(4):648-667.
* Jawahar et al. (2019) Ganesh Jawahar, Benoit Sagot, and Djame Seddah. 2019. What does bert learn about the structure of language? In _ACL 2019-57th Annual Meeting of the Association for Computational Linguistics_.
* Johnson et al. (2019) Jeff Johnson, Matthijs Douze, and Herve Jegou. 2019. Billion-scale similarity search with GPUs. _IEEE Transactions on Big Data_, 7(3):535-547.
* Karpathy et al. (2014) Andrej Karpathy, Armand Joulin, and Li F Fei-Fei. 2014. Deep fragment embeddings for bidirectional image sentence mapping. _Advances in neural information processing systems_, 27.
* Kingma and Ba (2015) Diederik P. Kingma and Jimmy Ba. 2015. Adam: A method for stochastic optimization. In _3rd International Conference on Learning Representations, ICLR 2015, San Diego, CA, USA, May 7-9, 2015, Conference Track Proceedings_.
* Kornblith et al. (2019) Simon Kornblith, Mohammad Norouzi, Honglak Lee, and Geoffrey Hinton. 2019. Similarity of neural network representations revisited. In _International Conference on Machine Learning_, pages 3519-3529. PMLR.
* Krizhevsky et al. (2009) Alex Krizhevsky, Geoffrey Hinton, et al. 2009. Learning multiple layers of features from tiny images.
* Liu et al. (2022) Zhuang Liu, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, and Saining Xie. 2022. A convnet for the 2020s. In _Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition_, pages 11976-11986.
* Marcus et al. (1993) Mitchell P. Marcus, Beatricce Santorini, and Mary Ann Marcinkiewicz. 1993. Building a large annotated corpus of English: The Penn Treebank. _Computational Linguistics_, 19(2):313-330.
* McInnes et al. (2018) Leland McInnes, John Healy, Nathaniel Saul, and Lukas Grossberger. 2018. Umap: Uniform manifold approximation and projection. _Journal of Open Source Software_, 3(29):861.
* Merity et al. (2016) Stephen Merity, Caiming Xiong, James Bradbury, and Richard Socher. 2016. Pointer sentinel mixture models. In _International Conference on Learning Representations_.
* Morcos et al. (2018) Ari Morcos, Maithra Raghu, and Samy Bengio. 2018. Insights on representational similarity in neural networks with canonical correlation. _Advances in Neural Information Processing Systems_, 31.
* Muller et al. (2021) Benjamin Muller, Yanai Elazar, Benoit Sagot, and Djame Seddah. 2021. First align, then predict: Understanding the cross-lingual ability of multilingual BERT. In _Proceedings of the 16th Conference of the European Chapter of the Association for Computational Linguistics: Main Volume_, pages 2214-2231, Online. Association for Computational Linguistics.
* Pires et al. (2019) Telmo Pires, Eva Schlinger, and Dan Garrette. 2019. How multilingual is multilingual BERT? In _Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics_, pages 4996-5001, Florence, Italy. Association for Computational Linguistics.
* Radford et al. (2021) Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh, Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, et al. 2021. Learning transferable visual models from natural language supervision. In _International Conference on Machine Learning_, pages 8748-8763. PMLR 

Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei, Ilya Sutskever, et al. 2019. Language models are unsupervised multitask learners. _OpenAI blog_, 1(8):9.
* Raghu et al. (2017) Maithra Raghu, Justin Gilmer, Jason Yosinski, and Jascha Sohl-Dickstein. 2017. Svcca: Singular vector canonical correlation analysis for deep learning dynamics and interpretability. _Advances in neural information processing systems_, 30.
* Russakovsky et al. (2015) Olga Russakovsky, Jia Deng, Hao Su, Jonathan Krause, Sanjeev Satheesh, Sean Ma, Zhiheng Huang, Andrej Karpathy, Aditya Khosla, Michael Bernstein, Alexander C. Berg, and Li Fei-Fei. 2015. ImageNet Large Scale Visual Recognition Challenge. _International Journal of Computer Vision (IJCV)_, 115(3):211-252.
* Sellam et al. (2021) Thibault Sellam, Steve Yadlowsky, Jason Wei, Naomi Saphra, Alexander D'Amour, Tal Linzen, Jasmijn Bastings, Ilulia Turc, Jacob Eisenstein, Dipajnan Das, et al. 2021. The multicrits: Bert reproductions for robustness analysis. _arXiv preprint arXiv:2106.16163_.
* Sharma et al. (2018) Piyush Sharma, Nan Ding, Sebastian Goodman, and Radu Soricut. 2018. Conceptual captions: A cleaned, hypermymed, image alt-text dataset for automatic image captioning. In _Proceedings of ACL_.
* Wu et al. (2020) John Wu, Yonatan Belinkov, Hassan Sajjad, Nadir Durrani, Fahim Dalvi, and James Glass. 2020. Similarity analysis of contextual word representation models. In _Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics_, pages 4638-4655.

 

Appendix

### Multilingual benchmark

#### a.1.1 Evaluation parameters

We split the test set, \(\mathbb{X}\), into equally sized batches of size 8, \(\{b^{(1)},b^{(2)},...,b^{(n)}\}\), where each batch consists of multilingual BERT representations of the same sentence in 5 different languages: \(L=\{L_{1},...,L_{5}\}\). Given a pair of different languages, \((L_{i},L_{j}),i\neq j\), and a batch of representations, \(b\), we consider the representation of those languages in the batch, \((b[i],b[j])\), and compute the similarity between \(b[i]\) and \(b[j]\) as \(s_{0}\equiv s(b[i],b[j])\). We also compute the similarity between \(b[i]\) and 10 randomly chosen batches (or, 10 batches chosen using FAISS) of representations in language \(L_{j}\) as \(\{s_{t}\equiv s(b[i],b_{t}[j])\}_{t=1}^{10}\). If \(\arg\max_{t}\{s_{t}\}_{t=0}^{10}=0\), we count it as a correct prediction. Each layer's accuracy is defined as the number of successful predictions over the number of batches, \(n\). We average results over all possible pairs of different languages.

#### a.1.2 Further evaluations

In addition to using the [CLS] token representation as a sentence representation, we also evaluated the multilingual benchmark using mean pooling sentence representation. We used the same evaluation process as described in Section 6.2. The results, summarized in Table 4, are consistent with the results in the main paper (Table 2). Under random sampling, ContraSim outperforms all other similarity measures. Using FAISS causes a big degradation in all other methods' accuracy, while ContraSim maintains a high accuracy across all layers.

In addition, we evaluated the multilingual benchmark with another multilingual model - the XLM-R Conneau et al. (2020) model. Results, summarized in Table 5, show a similar pattern to Tables 2 and 4, with ContraSim achieving the highest accuracies across all layers, in both random sampling and FAISS sampling scenarios.

### Image-caption

In addition to the four model pairs we evaluated in Figure 5, we assessed the multi-modal vision and language CLIP model Radford et al. (2021), which was trained using contrastive learning on pairs of images and their captions. Results in Table 6 show interesting findings. Under random sampling, dot product, DeepCKA and ContraSim achieve perfect accuracy. However, using FAISS causes significant degradation in DeepCKA accuracy, and only a small degradation in dot product and ContraSim results, with equal accuracy for both. We attribute this high accuracy for simple dot product to the fact that CLIP training was done using contrastive learning, thus observing high separability between examples.

### ViT training details

We used the ViT-base Dosovitskiy et al. (2020) architecture. We pretrained 10 models on the ImgaeNet-1K dataset Deng et al. (2009), differing only in their weight initializations by using random seeds from 0 to 9. We used the AdamW optimizer Kingma and Ba (2015) with lr \(=0.001\), weight decay \(=1e-3\), batch size \(=128\), and a cosine learning scheduler. We trained each model for 150 epochs and used the final checkpoint.

Then, we fine-tuned the pretrained models on CIFAR-10 and CIFAR-100 datasets Krizhevsky et al. (2009). We used AdamW optimizer with lr \(=2e-5\), weight decay \(=0.01\), batch size \(=10\), and a linear scheduler. For models fine-tuned on CIFAR-10, the average accuracy on the CIFAR-10 test set is \(96.33\%\). For models fine-tuned on CIFAR-100, the average accuracy on the CIFAR-100 test set is \(78.87\%\).

### Details of Prior Similarity Measures

Canonical Correlation Analysis (CCA).Given two matrices, CCA finds bases for those matrices, such that after projecting them to those bases the projected matrices' correlation is maximized. For \(1\leq i\leq p_{1}\), the \(i^{\text{th}}\) canonical correlation coefficient \(\rho_{i}\) is given by:

\[\begin{split}\rho_{i}&=\underset{w^{x}_{X},w^{i}_{Y}} {\operatorname{max}}\text{corr}(Xw^{i}_{X},Yw^{i}_{Y})\\ &\quad\mathrm{s.t.}~{}~{}~{}\forall_{j<i}~{}~{}Xw^{i}_{X}\perp X w ^{j}_{X}\\ &\qquad\forall_{j<i}~{}~{}Yw^{i}_{Y}\perp Yw^{j}_{Y}.\end{split} \tag{5}\]

where \(\mathrm{corr}(X,Y)=\frac{\langle X,Y\rangle}{||X||\cdot||Y||}\). Given the vector of correlation coefficients \(\mathrm{corrs}=(\rho_{1},...,\rho_{p_{1}})\), the final scalar similarity index is computed as the mean correlation coefficient:

\[S_{\rm CCA}(X,Y)=\overline{\rho}_{CCA}=\frac{\sum_{i=1}^{p_{1}}\rho_{i}}{p_{1}} \tag{6}\]

as previously used in Raghu et al. (2017); Kornblith et al. (2019) 

Projection-Weighted CCA (PWCCA).Morcos et al. (2018) proposed a different approach to transform the vector of correlation coefficients, corrs, into a scalar similarity index. Instead of defining the similarity as the mean correlation coefficient, PWCCA uses a weighted mean and the similarity is defined as:

\[S_{PW}=\frac{\sum_{i=1}^{p_{1}}\alpha_{i}\rho_{i}}{\sum_{i}\alpha_{i}}\quad \alpha_{i}=\sum_{j}|\langle h_{i},x_{j}\rangle| \tag{7}\]

where \(x_{j}\) is the \(j^{\text{th}}\) column of \(X\), and \(h_{i}=Xw_{X}^{i}\) is the vector observed upon projecting \(X\) to the \(i^{\text{th}}\) canonical direction.

Singular Vector CCA (SVCCA).Another extension to CCA, proposed by Raghu et al. (2017), performs CCA on the truncated singular value decomposition (SVD) of the activation matrices. SVCCA keeps enough principal components to explain a fixed percentage of the variance.

Code available at: [https://github.com/google/svcca](https://github.com/google/svcca).

Centered Kernel Alignment (CKA).CKA, Proposed by Kornblith et al. (2019), suggests comput

\begin{table}
\begin{tabular}{l r c} \hline \hline  & Random & FAISS \\ \hline CKA & 93.67 & 25.31 \\ Dot Product & 100 & 98.73 \\ DeepCKA & 100 & 13.92 \\ DeepDot & 29.11 & 25.31 \\ ContraSim & 100 & 98.73 \\ \hline \hline \end{tabular}
\end{table}
Table 6: Image–caption benchmark accuracy results using CLIP model

\begin{table}
\begin{tabular}{c l r c|r c c c c} \hline \hline Layer & CKA & DeepCKA & DeepDot & ContraSim & CKA & DeepCKA & DeepDot & ContraSim \\ \hline
1 & 87.7 \(\pm\) 6.9 & 86.3 \(\pm\) 9.7 & 43.4 \(\pm\) 17.7 & 98.7 \(\pm\) 2.2 & 67.6 \(\pm\) 14.3 & 54.1 \(\pm\) 10.9 & 41.7 \(\pm\) 19.1 & 94.2 \(\pm\) 10.7 \\
2 & 89.0 \(\pm\) 6.3 & 88.7 \(\pm\) 7.0 & 51.5 \(\pm\) 20.0 & 99.5 \(\pm\) 0.8 & 68.2 \(\pm\) 13.2 & 49.0 \(\pm\) 8.3 & 38.9 \(\pm\) 17.2 & 96.6 \(\pm\) 14.8 \\
3 & 91.8 \(\pm\) 4.4 & 90.7 \(\pm\) 6.0 & 63.3 \(\pm\) 20.8 & 99.9 \(\pm\) 0.1 & 72.2 \(\pm\) 11.5 & 55.4 \(\pm\) 8.1 & 44.8 \(\pm\) 16.6 & 98.8 \(\pm\) 20.5 \\
4 & 93.7 \(\pm\) 3.3 & 91.3 \(\pm\) 5.0 & 73.1 \(\pm\) 19.4 & 99.9 \(\pm\) 0.0 & 74.3 \(\pm\) 10.0 & 55.1 \(\pm\) 8.0 & 45.7 \(\pm\) 16.5 & 99.5 \(\pm\) 7.1 \\
5 & 95.3 \(\pm\) 3.0 & 92.1 \(\pm\) 4.0 & 83.9 \(\pm\) 15.6 & 99.9 \(\pm\) 0.0 & 78.2 \(\pm\) 8.2 & 56.7 \(\pm\) 8.1 & 53.2 \(\pm\) 17.5 & 99.8 \(\pm\) 4.4 \\
6 & 95.9 \(\pm\) 2.4 & 91.8 \(\pm\) 3.9 & 91.2 \(\pm\) 10.6 & 100 \(\pm\) 0 & 77.6 \(\pm\) 7.9 & 54.2 \(\pm\) 8.1 & 60.1 \(\pm\) 18.2 & 99.8 \(\pm\) 1.7 \\
7 & 95.4 \(\pm\) 2.5 & 90.6 \(\pm\) 4.1 & 93.1 \(\pm\) 9.2 & 100 \(\pm\) 0 & 77.9 \(\pm\) 7.8 & 53.3 \(\pm\) 7.2 & 63.5 \(\pm\) 18.5 & 99.9 \(\pm\) 0.7 \\
8 & 94.8 \(\pm\) 3.2 & 89.7 \(\pm\) 4.3 & 90.3 \(\pm\) 12.0 & 100 \(\pm\) 0 & 76.7 \(\pm\) 8.1 & 52.4 \(\pm\) 7.4 & 61.0 \(\pm\) 19.8 & 99.9 \(\pm\) 0.3 \\
9 & 94.0 \(\pm\) 3.4 & 88.5 \(\pm\) 5.0 & 86.4 \(\pm\) 15.1 & 100 \(\pm\) 0 & 73.9 \(\pm\) 8.8 & 51.4 \(\pm\) 7.8 & 55.5 \(\pm\) 20.0 & 99.9 \(\pm\) 0.1 \\
10 & 92.6 \(\pm\) 4.2 & 85.6 \(\pm\) 5.9 & 80.7 \(\pm\) 18.8 & 100 \(\pm\) 0 & 72.2 \(\pm\) 8.4 & 49.3 \(\pm\) 8.4 & 49.2 \(\pm\) 20.6 & 99.9 \(\pm\) 0.1 \\
11 & 91.1 \(\pm\) 5.1 & 81.0 \(\pm\) 6.5 & 72.2 \(\pm\) 23.7 & 99.9 \(\pm\) 0 & 70.6 \(\pm\) 10.1 & 48.8 \(\pm\) 9.1 & 43.2 \(\pm\) 20.7 & 99.8 \(\pm\) 0.1 \\
12 & 90.8 \(\pm\) 5.8 & 71.3 \(\pm\) 7.6 & 71.0 \(\pm\) 21.0 & 99.9 \(\pm\) 0 & 72.7 \(\pm\) 11.3 & 40.3 \(\pm\) 8.7 & 42.7 \(\pm\) 17.0 & 99.4 \(\pm\) 0.1 \\ \hline \hline \end{tabular}
\end{table}
Table 4: Multilingual benchmark results with mean pooling.

\begin{table}
\begin{tabular}{c c c} \hline \hline  & Random & FAISS \\ \hline CKA & 93.67 & 25.31 \\ Dot Product & 100 & 98.73 \\ DeepCKA & 100 & 13.92 \\ DeepDot & 29.11 & 25.31 \\ ContraSim & 100 & 98.73 \\ \hline \hline \end{tabular}
\end{table}
Table 5: Multilingual benchmark results on XLM-R model.

 

ing a kernel matrix for each matrix representation input, and defining the scalar similarity index as the two kernel matrices' alignment. For linear kernel, CKA is defined as:

\[S_{\mathrm{CKA}}=\frac{\|Y^{T}X\|_{F}^{2}}{\|X^{T}X\|_{F}\|Y^{T}Y\|_{F}} \tag{8}\]

Code available at: [https://github.com/google-research/google-research/tree/master/representation_similarity](https://github.com/google-research/google-research/tree/master/representation_similarity).

Norm.For two representations, \(x\) and \(y\), we defined the dis-similarity measure as the norm of the difference between the normalized representations:

\[Dis_{\mathrm{Norm}}(x,y)=\left\lVert(x/\|x\|-y/\|y\|)\right\rVert \tag{9}\]

Since this is a dis-similarity measure, we defined the norm similarity measure as:

\[S_{\mathrm{Norm}}=1-Dis_{\mathrm{Norm}}(x,y) \tag{10}\]

For a batch of representations, we define batch similarity as the mean of pairwise norm similarity.

Dot-product.Measuring the similarity score between two feature vectors as their dot-product.

### Additional Evaluations

In this section, we report evaluation results with additional baselines. Furthermore, we evaluated ContraSim with a different similarity measure than the dot-product and replaced it with the norm similarity measure.

Similar to PWCCA, SVCCA requires that the number of examples is larger than the vector dimension, thus we could only evaluate it in the layer prediction benchmark. All other similarity measures were evaluated with all evaluations - the layer prediction benchmark, the multilingual benchmark and the image-caption benchmark.

Table 7 shows layer prediction benchmark results. We can observe that SVCCA achieves slightly better results than PWCCA, and lower than CKA and ContraSim. Both dot product and norm achieve low accuracies. ContraSim_norm achieves the same or better results than ContraSim.

Multilingual benchmark results, summarized in Table 8 show that both dot product and norm achieve better results than CKA, although achieve low results under layer prediction and image-caption benchmarks. This emphasizes the importance of multiple evaluations for similarity measures. Compared to ContraSim, both methods achieve lower results. ContraSim_norm achieves lower results compared to ContraSim, under both random and FAISS sampling.

Image-caption benchmark results, summarized in Table 9, show that under both random sampling and FAISS sampling dot product and norm achieve low accuracy. Under random sampling, ContraSim_norm achieves perfect accuracy, while using FAISS sampling shows slight degradation compared to ContraSim.

\begin{table}
\begin{tabular}{c c c} \hline \hline  & Penn TreeBank & WikiText \\ \hline SVCCA & 46.66 & 56.66 \\ Dot product & 8.33 & 6.66 \\ Norm & 10.00 & 11.66 \\ \hline ContraSim\_norm & & \\ Penn & 100.00 & 90.00 \\ Wiki & 100.00 & 100.00 \\ \hline \hline \end{tabular}
\end{table}
Table 7: Layer prediction benchmark with additional similarity measures.

 



\begin{table}
\begin{tabular}{c c c c|c c c} \hline \hline  & \multicolumn{3}{c}{Random} & \multicolumn{4}{c}{FAISS} \\ \cline{2-7} Layer & \begin{tabular}{c} Dot \\ product \\ \end{tabular} & Norm & ContraSim\_norm & 
\begin{tabular}{c} Dot \\ Product \\ \end{tabular} & Norm & ContraSim\_norm \\ \hline
1 & 48.93 \(\pm\) 7.07 & 68.67 \(\pm\) 10.86 & 89.39 \(\pm\) 14.46 & 15.00 \(\pm\) 6.41 & 20.42 \(\pm\) 9.32 & 15.76 \(\pm\) 6.56 \\
2 & 31.71 \(\pm\) 3.31 & 74.19 \(\pm\) 9.73 & 85.40 \(\pm\) 18.00 & 20.14 \(\pm\) 2.75 & 21.16 \(\pm\) 11.43 & 18.83 \(\pm\) 11.19 \\
3 & 49.32 \(\pm\) 6.53 & 83.92 \(\pm\) 13.12 & 85.68 \(\pm\) 18.88 & 13.00 \(\pm\) 4.50 & 29.42 \(\pm\) 22.99 & 26.36 \(\pm\) 16.01 \\
4 & 29.60 \(\pm\) 2.44 & 99.61 \(\pm\) 0.41 & 96.81 \(\pm\) 5.04 & 16.20 \(\pm\) 4.01 & 57.98 \(\pm\) 15.68 & 28.79 \(\pm\) 9.80 \\
5 & 99.75 \(\pm\) 0.29 & 99.86 \(\pm\) 0.33 & 99.86 \(\pm\) 0.23 & 82.17 \(\pm\) 7.36 & 82.39 \(\pm\) 7.73 & 74.04 \(\pm\) 7.11 \\
6 & 99.75 \(\pm\) 0.35 & 99.84 \(\pm\) 0.27 & 99.92 \(\pm\) 0.13 & 83.38 \(\pm\) 7.70 & 88.24 \(\pm\) 6.15 & 77.00 \(\pm\) 6.68 \\
7 & 99.52 \(\pm\) 0.77 & 99.85 \(\pm\) 0.29 & 99.92 \(\pm\) 0.13 & 89.72 \(\pm\) 5.57 & 89.23 \(\pm\) 6.98 & 78.21 \(\pm\) 6.77 \\
8 & 99.93 \(\pm\) 0.13 & 99.89 \(\pm\) 0.15 & 99.94 \(\pm\) 0.11 & 93.49 \(\pm\) 4.07 & 89.70 \(\pm\) 6.42 & 81.97 \(\pm\) 6.63 \\
9 & 99.61 \(\pm\) 0.38 & 99.76 \(\pm\) 0.40 & 99.91 \(\pm\) 0.17 & 82.48 \(\pm\) 9.32 & 84.85 \(\pm\) 8.57 & 81.37 \(\pm\) 6.53 \\
10 & 96.64 \(\pm\) 2.46 & 99.38 \(\pm\) 0.58 & 99.89 \(\pm\) 0.15 & 55.02 \(\pm\) 15.42 & 81.43 \(\pm\) 9.77 & 80.59 \(\pm\) 6.83 \\
11 & 87.04 \(\pm\) 8.13 & 98.40 \(\pm\) 1.25 & 99.83 \(\pm\) 0.31 & 29.62 \(\pm\) 13.82 & 82.20 \(\pm\) 9.46 & 80.74 \(\pm\) 7.08 \\
12 & 76.86 \(\pm\) 15.23 & 87.25 \(\pm\) 12.39 & 99.73 \(\pm\) 0.36 & 25.75 \(\pm\) 26.55 & 50.11 \(\pm\) 32.55 & 80.24 \(\pm\) 6.63 \\ \hline \hline \end{tabular}
\end{table}
Table 8: Multilingual benchmark with additional similarity measures. The left block is with random sampling, and the right block is FAISS sampling.

\begin{table}
\begin{tabular}{c c c c c} \hline \hline Vision Model & \multicolumn{2}{c}{ViT} & ConvNext \\ Language & BERT & GPT2 & BERT & GPT2 \\ Model & 6.32 & 6.32 & 11.39 & 8.86 \\ Random & Norm & 6.32 & 7.59 & 15.19 & 7.59 \\ ContraSim\_norm & 100 & 100 & 100 & 100 \\ \hline \multirow{4}{*}{FAISS} & Dot & \multirow{4}{*}{5.06} & \multirow{4}{*}{2.53} & \multirow{4}{*}{7.59} & \multirow{4}{*}{6.32} \\  & Product & & & \\ \cline{1-1} \cline{5-5}  & Norm & 5.06 & 5.06 & 6.32 & 10.12 \\ \cline{1-1} \cline{5-5}  & ContrraSim\_norm & 93.67 & 98.73 & 81.03 & 93.67 \\ \hline \hline \end{tabular}
\end{table}
Table 9: Image–caption benchmark results for additional similarity measures, on 4 different model pairs.