C ontra S im – Analyzing Neural Representations Based on Contrastive Learning (2024)

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.

## 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 @xcite @xcite . 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 @xcite . various similarity measures have been proposed for comparing representations, among them the most popular ones are based on centered kernel alignment (cka) @xcite and canonical correlation analysis (cca) @xcite . 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) @xcite . contrary to prior work (e.g., @xcite , 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 closedform measure. we experimentally evaluate contrasim on standard benchmark for similarity measures -the layer prediction benchmark @xcite , 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, con-trasim 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 @xcite . 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 @xcite is more similar to vision models than gpt-2 @xcite . our analysis indicates that both bert and gpt-2 create representations that are equally similar to the vision ones. ## related work many studies have used similarity measures for the interpretability of nns. for instance, @xcite 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, @xcite ) learn representations that are similar in the shallow layers. similar findings were noted for language models by @xcite . the latter also evaluated the effect of fine-tuning on language models and found that the top layers are most affected by fine-tuning. @xcite and @xcite found that increasing the model's layer width results in more similar representations between models. @xcite provided an interpretation of the learning process by examining how similar representations were dur-ing 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 @xcite and data retrieval @xcite . in contrast to that line of work, we borrow the contrastive learning formulation for similarity-based interpretations of deep networks. ## problem setup our goal is to obtain a scalar similarity score, which represents the similarity between the two sets of representations, a (i) 1 and a (i) 2 , and ranges from 0 (no similarity) to 1 (identical representations). that is, we define x 1 ∈ r n ×p 1 as a matrix of p 1 -dim activations of n data points, and x 2 ∈ r n ×p 2 as another matrix of p 2 -dim activations of n data points. we seek a similarity measure, s(x 1 , x 2 ). ## 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 θ denote an encoder with trainable parameters θ, and assume two representations a 1 and a 2 . in order to obtain a similarity score between 0 and 1, we first apply l2 normalization to the encoder outputs: z 1 = e θ (a 1 )/∥e θ (a 1 )∥ (and similarly for a 2 ). then their similarity is calculated as: for efficiency reasons, we calculate the similarity between batches of the normalized encoder representations, dividing by the batch size n: 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 θ1 and e θ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 section 6.3.3. training. none of the similarity measures commonly used in nns analysis uses negative examples to estimate the similarity of a given pair (section 2). 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) ∈ x with its encoded representation 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 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 @xcite , and minimize the following objective: with scalar temperature parameter τ > 0. here z p and 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, θ. 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. ## 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. ## experiments baselines. we compare contrasim with the following standard baselines. 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 θ to only maximize the similarity between positive pairs: ## 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 ( 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. ## 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.