# Analysing Adversarial Attacks with Linear Probing

*This work was produced as part of the* [*Apart Fellowship*](https://www.apartresearch.com/lab)*.* [*@Yoann Poupart*](https://www.alignmentforum.org/users/yoann-poupart?mention=user) *and* [*@Imene Kerboua*](https://www.lesswrong.com/users/imene-kerboua?mention=user) *led the project;* [*@Clement Neo*](https://www.alignmentforum.org/users/clement-neo?mention=user) *and* [*@Jason  Hoelscher-Obermaier*](https://www.alignmentforum.org/users/jas-ho?mention=user) *provided mentorship, feedback and project guidance.*

*Here, we present a qualitative analysis of our preliminary results. We are at the very beginning of our experiments, so any setup change, experiment advice, or idea is welcomed. We also welcome any relevant paper that we could have missed. For a better introduction to interpreting adversarial attacks, we recommend reading* [*scasper*](https://www.lesswrong.com/users/scasper)*’s post: *[*EIS IX: Interpretability and Adversaries*](https://www.alignmentforum.org/posts/kYNMXjg8Tmcq3vjM6/eis-ix-interpretability-and-adversaries)*.*

*Code available on* [*GitHub*](https://github.com/Xmaster6y/mulsi)* (still drafty).*

TL;DR
=====

*   Basic adversarial attacks produce humanly indistinguishable examples for the human eye.
*   In order to explore the [features](https://www.semanticscholar.org/paper/Adversarial-Examples-Are-Not-Bugs%2C-They-Are-Ilyas-Santurkar/1f4294d8e0b0c8559479fac569fc0ea91b4dc0bd) vs [bugs](https://www.semanticscholar.org/paper/A-Discussion-of-'Adversarial-Examples-Are-Not-Bugs%2C-Nakkiran/8a66cb8c20f4c41e603d28f4e8cc9b3c270e4345) view of adversarial attacks, we trained linear probes on a classifier’s activations (we used CLIP-based models for further multimodal work). The goal is that training linear probes to detect basic concepts can help analyse and detect adversarial attacks.
*   As the concepts studied are simple (e.g. color), they can be probed from every layer of the model.
*   We show that concept probes are indeed modified by the adversarial attack, but only on the later layers.
*   Hence, we can naively detect adversarial attacks by observing the disagreement between early layers’ probes and later layers’ probes.

Introduction
============

Motivation
----------

Adversarial samples are a concerning failure mode of deep learning models. It has been shown that by optimising an input, e.g. an image, the output of the targeted model can be shifted at the attacker’s will. This failure mode appears repeatedly in different domains like [image classification](https://www.semanticscholar.org/paper/Explaining-and-Harnessing-Adversarial-Examples-Goodfellow-Shlens/bee044c8e8903fb67523c1f8c105ab4718600cdb), [text encoders](https://www.semanticscholar.org/paper/BERT-ATTACK%3A-Adversarial-Attack-against-BERT-Using-Li-Ma/dc0ce66f5ab4c5173cdef951649044e4c4c05076), and more recently on [multimodal models](https://www.semanticscholar.org/paper/Jailbreak-in-pieces%3A-Compositional-Adversarial-on-Shayegani-Dong/92b9d8b8c81c4c53ea62000c0924500b2dd11bce) enabling [arbitrary outputs](https://www.semanticscholar.org/paper/On-the-Adversarial-Robustness-of-Multi-Modal-Models-Schlarmann-Hein/5690e35b8beab92a80055fe2530c29c24e495379) or [jailbreaking](https://www.semanticscholar.org/paper/Visual-Adversarial-Examples-Jailbreak-Aligned-Large-Qi-Huang/142e934dd5d6c53f877c30243d436255e3a0dde7).

![](https://res.cloudinary.com/lesswrong-2-0/image/upload/f_auto,q_auto/v1/mirroredImages/Jv8cQY27kRJZ6FrKN/pbdaozjysp6jl1f5botk)

*Figure 1: Experimental setup to analyse an adversarial attack using linear probes. Given a classifier trained on image samples, we train linear probes to detect the concepts activated by each input image. Each linear probe is specialised on one concept. In this example, an image of a lemon should activate the concept “ovaloid”. When the input is attacked, the probe can detect other concepts related to the targeted adversarial class.*

Hypotheses to Test
------------------

This work was partly inspired by the feature vs bug world hypothesis. In our particular context this would imply that adversarial attacks might be caused by meaningful feature directions being activated (feature world) or by high-frequency directions overfitted by the model (bug world). The hypotheses we would like to test are thus the following:

*   The adversariality doesn't change the features. [Nakkiran et al.](https://www.semanticscholar.org/paper/A-Discussion-of-'Adversarial-Examples-Are-Not-Bugs%2C-Nakkiran/8a66cb8c20f4c41e603d28f4e8cc9b3c270e4345) found truly adversarial samples that are "bugs", while [Ilyas et al.](https://www.semanticscholar.org/paper/Adversarial-Examples-Are-Not-Bugs%2C-They-Are-Ilyas-Santurkar/1f4294d8e0b0c8559479fac569fc0ea91b4dc0bd) seem to indicate that adversarial attacks find features.
*   The different optimisation schemes don't produce the same categories of adversarial samples (features vs bugs). Some might be more robust, e.g. w.r.t. the representation induced.

Post Navigation
---------------

First, we briefly present the background we used for [adversarial attacks](#Adversarial_Attacks) and [linear probing](#Linear_Probing). Then we showcase experiments, presenting our [setup](#Models_Setup), to understand the impact of [adversarial attacks on linear probes](#Analysing_Adversarial_Attacks_with_Linear_Probing1) and see if we can [detect it naively](#Concept_Probability_Through_Layers__Naive_Detection). Finally, we present the [limitations](#Limitations) and [perspectives](#Perspectives) of our work before [concluding](#Conclusion).

Background
==========

Adversarial Attacks
-------------------

For our experiment, we'll begin with the most basic and well-known adversarial attack, the [Fast Gradient Sign Method (FGSM)](https://www.semanticscholar.org/paper/Explaining-and-Harnessing-Adversarial-Examples-Goodfellow-Shlens/bee044c8e8903fb67523c1f8c105ab4718600cdb). This intuitive method takes advantage of the classifier differentiability to optimise the adversary goal (misclassification) using a gradient descent. It can be described as:

$$
\hat{x} = x + \epsilon \cdot {\rm sign} \left[\nabla_x \, \mathcal{L}(\theta, x, y)        \right].
$$

With $x,y$ the original image and label, $\hat{x}$ the adversarial example and $\epsilon$ the perturbation amplitude. This simple method can be derived in an [iterative form](https://www.semanticscholar.org/paper/Adversarial-examples-in-the-physical-world-Kurakin-Goodfellow/b544ca32b66b4c9c69bcfa00d63ee4b799d8ab6b):

$$
x_{n+1} = \prod \left\{ x_n + \epsilon \cdot {\rm sign} \left[\nabla_x \, \mathcal{L}(\theta, x_n, y),\right] \right\},
$$

and seen as projected gradient descent, with the projection $\prod$ ensuring that $||x-x_{n+1}||_\infty\leq \epsilon$. In our experiments $\epsilon=3$.

Linear Probing
--------------

[Linear probing](https://www.semanticscholar.org/paper/Understanding-intermediate-layers-using-linear-Alain-Bengio/5e23a28063b395bdaf784dc548a046885cb90cf2) is a simple idea where you train a linear model (probe) to predict a concept from the internals of the interpreted target model. The prediction performances are then attributed to the knowledge contained in the target model's latent representation rather than to the simple linear probe. Here, we are mostly interested in binary concepts, e.g. features presence/absence or adversarial/non-adversarial, and the optimisation can be formulated as minimising the following loss:

$$
\mathcal{L}={\rm BCE}(\mathcal{P}(h), c) + \lambda ||\mathcal{P}||.
$$

where in our experiments $\mathcal{P}$ is a logistic regression using the $\ell_2$ penalty.

Experiments
===========

Models Setup
------------

### Dataset Creation

In order to explore image concepts we first focused on a small category of images, fruits and vegetables, before choosing easy concepts like shapes and colours. We proceeded as follow:

*   We curated a dataset originating from Kaggle, making sure to have proper train and test splits. We also have spotted some misclassified images.
*   We then [labelled](https://huggingface.co/spaces/mulsi/explore-label-concepts) (not totally finished) the presence of concepts on these images to create our [concepts dataset](https://huggingface.co/datasets/mulsi/fruit-vegetable-concepts).
*   See more details in [appendix](#Dataset_Creation).
*   We trained different classifiers on this dataset, all from the [CLIP](https://www.semanticscholar.org/paper/Learning-Transferable-Visual-Models-From-Natural-Radford-Kim/6f870f7f02a8c59c3e23f407f3ef00dd1dcf8fc4) vision encoder.
*   We didn’t use augmentation tricks as all [our classifiers](https://huggingface.co/collections/Xmaster6y/fruits-and-vegetables-6612a9e7a0af69dc7d0bcf94) already reached pretty good accuracies.
*   We first created an [activation dataset](https://huggingface.co/datasets/mulsi/fruit-vegetable-activations) using the CLIP’s vision encoder. We used the residual stream activations as we are interested in full representation rather than head behaviour.
*   We subsequently trained [our probes](https://huggingface.co/mulsi/fruit-vegetable-probes) on these activations.

### Classifier Training

### Probe Training

### Sanity Checks

The goal is to validate that probes are detecting the concepts and not something else that is related to the class (we are not yet totally confident about our probes, more in the limitations).

*   Each layer and each concept has a dedicated linear probe.
*   We plot precision, recall and f1 scores for some of the trained probes, such as the one for the colour *“red”*, *“yellow”*, *“stem”* and *“sphere”*.
*   Noticeably the dynamics of concepts seem different. Certain concepts like “green” are present in every layer while concepts like “sphere” seem to appear in later layers.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/2925b13276c6bc198a183a70730eff3c8be60ea9ee50a185.png)

*Figure 2: Metrics for a probe trained to detect the “red” and “yellow” concepts given a layer’s activations. The probes seem to detect the concept over all layers with almost an equivalent precision and recall.*

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/9b3a89691c825db7c0df642c869f8a440d4bb8ac2465ae31.png)

*Figure 3: Metrics for a probe trained to detect the “stem” and “sphere” concepts given a layer’s activations. The probes seem to detect the concepts better in later layers.*

Analysing Adversarial Attacks with Linear Probing
-------------------------------------------------

### Goal

See what kind of features (if any) adversarial attacks find. Increase of the probe's accuracy on non-related features w.r.t. the input sample but related to the target sample.

### Setup

*   Model: ViT (CLIP)
*   Niche image dataset ( fruits and vegetables)
*   Get or train a classifier based on CLIP (frozen)
*   Label concepts in this dataset, e.g. shape/colour/category/background
*   Probe before and during the iterative FGSM and see if the probe activation changes

### Lemon -> Tomato

For this experiment, we show that given an input image of a lemon, it requires a few steps of adversarial perturbation for the model to output the desired target, tomato in our case (see figures 4-6).

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/b6cc3bc21aacb0f776a6e8bdcc4de6aaa9348ad7fbcf3903.png)

*Figure 4: Logit curve (left) and the final adversarial image (right) misclassified as a tomato.*

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/1fbf844ffa073bc8429643a4a6def961cb2ec62f81d34fcf.png)

*Figure 5: Probability of the ovaloid probe for layer 11 at different steps of the adversarial attack. The ovaloid concept disappears through the attack.*

We compare the appearance of two concepts *“ovaloid”* and *“sphere”* in the last layer of the vision encoder, which are the shapes of the lemon and the tomato respectively. It appears in Figure 7 that the concept *“ovaloid”* characterising the lemon disappears gradually with the increase of the adversarial steps, while the *“sphere”* concept marking the tomato starts to appear (see Figure 8).

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/9bf29fa9359e7faa79ac5833629129c237d42c52f3a6a6c9.png)

*Figure 6: Probability of the sphere probe for layer 11 at different steps of the adversarial attack. The sphere concept appears through the attack.*

### Lemon -> Tomato (Layer 6 & Layer 0)

We noted that the concept *“sphere”* which characterises a tomato, does not appear and remains unchanged in the first layers of the encoder (see Figure 9 and 10 for layers 0 and 6 respectively) despite the adversarial steps.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/eee143cb8326d76f0c2adc87ed6e4d227f14becd72709d9e.png)

*Figure 7: Probability of the sphere probe for layer 0 at different steps of the adversarial attack. The sphere concept is unchanged through the attack.*

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/24ca6ea053393fcaaae3531a6c3be45604b0242bb0df8132.png)

*Figure 8: Probability of the sphere probe for layer 6 at different steps of the adversarial attack. The sphere concept is unchanged through the attack.*

### Lemon -> Banana

We focus our interest in a concept that characterises both a lemon and a banana, the colour *“yellow”*. The adversarial attack does not change the representation of this concept through the layers and the adversarial steps (see Figure 11, Figure 12 and Figure 13).

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/d43302e705c16945422af498c2476a430d9b911eac8ec3cb.png)

*Figure 9: Probability of the yellow probe for layer 11 at different steps of the adversarial attack. The yellow concept is unchanged through the attack.*

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/ef47894d6793d1a72d7953508da8c8dce2556f24d4fdb238.png)

*Figure 10: Probability of the yellow probe for layer 6 at different steps of the adversarial attack. The yellow concept is unchanged through the attack.*

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/8b6d15397983e016dc611c35fd9c0b291a4dc194a1a1be45.png)

*Figure 11: Probability of the yellow probe for layer 0 at different steps of the adversarial attack. The yellow concept is unchanged through the attack.*

### Tomato -> Lemon

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/fb31e359308cdf2c4c5f4fcffb489d47ddbb81969cb5a48d.png)

*Figure 12: Probability of the sphere probe for layer 11 at different steps of the adversarial attack. The sphere concept disappears through the attack.*

Concept Probability Through Layers: Naive Detection
---------------------------------------------------

*This section contains early-stage claims not fully backed, thus we would love hearing feedback on this methodology.*

We spotted interesting concept activation patterns through layers, and we believe they could help detect adversarial attacks once formalised. We spotted two patterns:

*   “genesis out of thin air” a concept going from 0 in L0 to 1 in L11. Indicating a concept being artificially activated.
*   “Appear and disappear” a concept going from low in L0 to high in middle layers and the low again in L11. Indicating a concept being artificially suppressed.

![](https://res.cloudinary.com/lesswrong-2-0/image/upload/f_auto,q_auto/v1/mirroredImages/Jv8cQY27kRJZ6FrKN/ndq6ko9y2ke02o0t5s4x)

*Figure 13: Evolution of the probes’ probability during layers for the original image and its adversarial attack. The orange line shows the probability of the concept’s appearance (sphere) before the attack, and the blue line shows it at the 10th step of the attack.*

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/f379c31ccfe97298edd89e5172170ef114d172e64b3e09d9.png)

*Figure 14: Evolution of the probes’ probability during the adversarial attacks for different layers. The mean probability over the tokens is represented on the left and the probability on the CLS token (used for classification) is represented on the right.*

Looking at the concept curve (e.g. on the CLS in Figure 14) we see that there is a transition mostly on the last layer. This could indicate that our adversarial attacks mostly target later layers and thus could be detected spotting incoherent patterns like the probability in Figure 14 (L0: 02 -> L4: 0.35 -> L8: 0.8 -> L10: 0.0).

Intuitively this would only be fair for simple concepts that are in the early layers.

We present what we are excited to (see) tackle next in the [perspectives section](#Perspectives).

Limitations
===========

### Interpretability Illusion

Interpretability is known to have illusion issues and linear probing doesn’t make an exception. We are not totally confident that our probes do measure their associated concept. In this respect we aim to further improve and refine our dataset.

*   The appearance of concepts like “sphere” in later layers might be a labelling artifact.
*   We tried more robust probes but it didn’t improve the sanity checks.
    *   We tried only training on early layers and evaluating on all
    *   We tried simultaneously training on all layers

### You Need Concept in Advance

*   Here, without the labelled concepts, analysing the adversarial attack is impossible.
*   While image classification is easy to conceptualise, multimodal settings might be more challenging. Adversarial attacks based on a text decoder might be much harder to detect.

### Introduced Biases

We trained one probe per concept and per layer (on all visual tokens). Our intuition is that information is mixed by the cross-attention mechanism and similarly represented across tokens for such simple concepts.

### Truly Bugs Attacks

We are not tackling some adversarial attacks that have been labelled as truly bugs. We are interested in testing our method on it as this could remain a potential threat.

Perspectives
============

We now describe what we are excited to test next and present possible framing of this study for future work.

### What’s next?

First, we want to make sure that we are training the correct probes. Some proxies are mixed with concepts, e.g. pulp is mostly equivalent to orange + lemon. For that, we are thinking of ways to augment our labelled dataset using basic CV tricks.

We are moving to automated labelling using GPT4V, while keeping a human in the loop overseeing the labels produced. And we are also thinking about creating concept segmentation maps as the concepts localisation might be relevant (c.f. the heatmaps above).

In the longer term view, we are also willing to explore the following tweaks:

*   Exploring the impact of changing the adversarial loss, e.g. without using a classifier.
*   Using different classifiers, e.g. more robust classifiers.
*   Explore universal adversarial attacks and non-transferable adversarial attacks.
*   The same detection method could be used but for image adversarial attacks based on a text decoder.
*   Compare the impact of adversarial attacks on the concepts in the text decoder and image encoder.
*   Using the probe directions to create adversarial attacks.
*   Using adversarial attacks to create steering vectors

### Multimodal Settings

*   The same detection method could be used but for image adversarial attacks based on a text decoder.
*   Compare the impact of adversarial attacks on the concepts in the text decoder and image encoder.

### Representation Engineering

*   Using the probe directions to create adversarial attacks.
*   Using adversarial attacks to create steering vectors

Related Work
============

### Analysing Adversarial Attacks

*   [Dection based on explainable reinforcement learning](https://www.semanticscholar.org/paper/Robust-Adversarial-Attacks-Detection-Based-on-Deep-Hickling-Aouf/5740c57ef3a1a1d398be96d599153ffb6e3e54c9)
*   [Using mutual information between the origin class and the target class](https://www.semanticscholar.org/paper/Understanding-Adversarial-Examples-From-the-Mutual-Zhang-Benz/e582444763f8b1e3e2c725aab1f37c5d64b69123)
*   [Defence via concept extraction by segmentation](https://www.semanticscholar.org/paper/Extracting-Knowledge-For-Adversarial-Detection-and-Freitas-Georgia/39ef4f5c21227fd59ebc7858d3de299e695c0faa)
*   [Feature robustness analysis of adversarial attacks](https://www.semanticscholar.org/paper/Adversarial-Perturbations-Are-Not-So-Weird%3A-of-and-Springer-Mitchell/4cdca901e6324198501ebda9100a1877d19ebb88)

### Adversarial Attacks on Latent Representation

*   [Steer the last feature layer using a target decoder](https://www.semanticscholar.org/paper/Towards-Feature-Space-Adversarial-Attack-Xu-Tao/4d3e1c4b597a44e7f020e911636f7ccc739df3d3)
*   [Attack based on inner layers for increasing transferability](https://www.semanticscholar.org/paper/Feature-Space-Perturbations-Yield-More-Transferable-Inkawhich-Wen/14a865278845d5a51e3b181f322e993397650b48)

### Universal Adversarial Attacks (UAPs)

*   [Analysis of generaling properties of UAPs](https://www.semanticscholar.org/paper/On-Distinctive-Properties-of-Universal-Park-Wei/707bdc248ca5d982f7be62eefc2d5ccfe7864c99) in the continuity of [Ilyas et al.](https://www.semanticscholar.org/paper/Adversarial-Examples-Are-Not-Bugs%2C-They-Are-Ilyas-Santurkar/1f4294d8e0b0c8559479fac569fc0ea91b4dc0bd)
*   [Universal bug features](https://www.semanticscholar.org/paper/Dominant-Patterns%3A-Critical-Features-Hidden-in-Deep-Ye-Qin/c19a1737ead4e2e06a4142bb1d27efca7cb39f7f)
*   [Using distributional properties to create UAPs](https://www.semanticscholar.org/paper/Crafting-Targeted-Universal-Adversarial-Considering-Wang-Cai/fe5f0bc19634726af7cd11ca102342bf9baaf0b5)
*   [A survey on adversarial attacks](https://www.semanticscholar.org/paper/A-Survey-On-Universal-Adversarial-Attack-Zhang-Benz/c514fe110102ae7243b9443b089cee3edaf007bf)

### Visualising Adversarial Attacks

*   [Feature visualisation of adversarial attacks](https://www.semanticscholar.org/paper/Massif%3A-Interactive-Interpretation-of-Adversarial-Das-Park/a6ddfbad5067496b941cfcf857637cdb2c2b18ac) (features based on neuron activation maximisation)
*   [Visualising neuron pathways of adversarial attacks](https://www.semanticscholar.org/paper/Bluff%3A-Interactively-Deciphering-Adversarial-on-Das-Park/d57b96e4e643e3adffef3c431cbf907cdbe4b037)
*   [Visualisation of neuron vulnerability](https://www.semanticscholar.org/paper/Visual-Analytics-of-Neuron-Vulnerability-to-Attacks-Li-Wang/76337642892a8181e91e424050c4679e92ff3fb6)

Conclusion
==========

Our method doesn’t guarantee that we’ll be able to detect any adversarial attacks but showcases that some mechanistic measures can be sanity safeguards. These linear probes are not costly to train nor to use during inference and can help detect adversarial attacks and possibly other kinds of anomalies.

Appendix
========

Dataset description
-------------------

We collected a dataset of fruits and vegetables image classification from Kaggle (insert link). The dataset was cleaned and manually labelled by experts. The preprocessing included a deduplication step to remove duplicate images, and a filtering of images that were present in both the train and test sets, which encouraged data leakage.

For the annotation process, there were 2 annotators who labelled each image with the concepts that appear on it, each image is associated with at least 1 concept. An additional step was required to resolve annotation conflicts and correct the concepts.

After preprocessing, the train set contains over 2579 samples and the test set over 336 samples with 36 classes each; the class distributions over sets are shown in Figure 17.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/6e311cd39e3dd737ceaf6320d60b669e5d5a1ba96335490a.png)

*Figure 17: Class distributions over both the train and test sets.*

The chosen concepts concern the colour, shape and background of the images. Figure 18 shows the distribution of concepts over the whole dataset.