Controlling Model Identity through Internal Activations: A Study of MLP and Residual Hooks

Introduction

To understand Steering Vectors, you have to stop thinking of LLMs as black boxes that read text and spit out answers. Instead, think of them as a massive collection of neurons firing in specific patterns.

When you ask ChatGPT "Who are you?", a specific pattern of neurons fires that represents the concept of Helpful AI Assistant. When you ask it to roleplay as a dog, a different pattern fires.

Usually, we influence these patterns by Prompting ("Act like a dog"). This is like asking a polite actor to pretend to be a dog. They might do it, but they know they are acting.

Steering vector represents a specific direction in the model's activation space. By capturing the difference in hidden states between two opposing concepts (e.g., "Dog" minus "Assistant"), we isolate a vector that encodes that specific semantic feature. We then add this vector to the model's forward pass during inference, mathematically biasing the activations toward the desired concept.

The Neurostimulation Analogy

Think of this more like Neurostimulation.

In neuroscience, researchers can use electrodes to apply electric fields to specific parts of the brain. By stimulating the right cluster of neurons, they can force a hand to twitch or trigger a specific emotion, bypassing the person's conscious control.

Steering Vectors act like that electric field. We calculate the specific "electrical signal" that corresponds to a target concept—whether that is a specific persona, an emotion, or a writing style—and broadcast it directly into the model's layers.

We aren't asking the model to think about adopting a new identity; we are stimulating its neurons to fire in a pattern that embodies that trait. The model doesn't "decide" to change; its internal state is simply manipulated by the field we applied.

But Why?

Prompting operates entirely in input space: it asks the model to behave differently, but does not change its internal representations. As a result, prompt-based identities are fragile, easily overridden, and often inconsistent across turns.

Fine-tuning, on the other hand, permanently alters model weights. While effective, it is expensive, irreversible, and obscures where and how a behavior is encoded inside the model.

Activation steering, motivated by the Linear Representation Hypothesis, sits between these extremes. It directly modifies internal representations at inference time—treating identity as a property of the model's state, not an instruction or a permanent rewrite. This allows precise, reversible, and interpretable control over behavior without retraining the model.

Working

Steering Vector Calculation Diagram
Image generated by Gemini

As shown in the diagram above, the magic happens in the subtraction: Steering Vector = Activation A - Activation B.

But why subtraction? Why not just use "Activation A"?

If you simply took the activations for "I like Dogs," you wouldn't just capture the concept of Dogs. You would also capture the neurons responsible for:

If you injected all of that, you would just be adding noise to the model. You need to isolate the specific flavor you want.

The subtraction acts as a filter:

Activation A (Target): Contains [English] + [Grammar] + [Intelligence] + [Dog Concept]

Activation B (Base): Contains [English] + [Grammar] + [Intelligence] + [Cat Concept]

When you calculate A minus B, the common elements cancel out:

  • (English - English) = 0
  • (Grammar - Grammar) = 0
  • (Intelligence - Intelligence) = 0

The Result: You are left with a raw, distilled vector representing Pure Dog-ness (minus Cat-ness).

How Insertion Works

Vector Insertion in Transformer Layers
Image generated by Gemini

In a standard Transformer model (like GPT or Phi-3), information flows through a series of layers. Layer 1 passes information to Layer 2, which passes it to Layer 3, and so on.

To steer the model, we intervene in this flow. We pick a specific layer (for example, Layer 18) and add our vector to the information passing through it.

The Formula:
New Output = Original Output + (Coefficient × Steering Vector)

Why Middle Layers?

In transformer models, early layers mostly capture surface-level features like syntax and token structure, while late layers are tightly coupled to final token selection. Steering early layers often adds noise, and steering late layers can override behavior too aggressively.

Middle layers sit at the point where high-level semantic concepts are formed but not yet finalized. Intervening here allows us to bias the model's internal representation of meaning and identity in a stable way, without directly forcing the output.

Why it Works? — Linear Representation Hypothesis

The Linear Representation Hypothesis suggests that high-level concepts inside neural networks are represented as directions in a high-dimensional activation space. This means abstract ideas—such as gender, roles, or identity—can often be manipulated through simple vector arithmetic.

Classic Example: "King − Man + Woman" Equation

The vector difference between King and Man captures the concept of royalty without male gender. Adding Woman shifts this representation toward the female counterpart, resulting in Queen.

This works because the model encodes concepts like gender and role along approximately linear directions.

Applied to large language models, this hypothesis implies that properties such as style, persona, or identity can also be represented as linear directions in activation space. If a desired trait (for example, "dog-like behavior") corresponds to a consistent direction, then adding that direction to the model's internal activations should bias generation toward that trait—without retraining the model.

Implementation

Through this experiment I am trying to implement the Dog Personality in my LLM.

1. Calculating the Steering Vector

First, we isolate the "personality" by finding the difference between the Target (Dog) and Base (Standard AI) hidden states. We then normalize this vector so it has a length of 1, effectively turning it into a pure direction.

The Formula:
v_steering = (μ(h_target) - μ(h_base)) / ||μ(h_target) - μ(h_base)||₂

Where:

# Calculate difference diff = target_out[layer][:, -1, :] - base_out[layer][:, -1, :] # Normalize vec = torch.mean(diff, dim=0) vec = vec / (torch.norm(vec) + 1e-8)

2. Injecting the Vector (The Steering)

Once we have the vector, we inject it into the model during the forward pass. This is a linear addition to the existing activations at a specific layer.

The Formula:
h_steered = h_original + λ · v_steering

Where:

Experimental Methodology & Analysis

To isolate the "Dog" persona without breaking the model, I conducted the experiment in three distinct stages: Identifying Scale Range, Identifying Range of Layers, and Finding the Final Output.

Step 1: Identifying Scale Range

Objective: The first step was to determine the "coefficient of failure"—the steering strength required to override the model's base identity ("Microsoft Phi-3"). I performed a sweep of coefficients ranging from 5 to 30 on individual layers.

Observations:

Conclusion: A high scale is necessary to break the base identity, but single-layer steering is too unstable to control the destination.

Step 2: Identifying Range of Layers

Objective: After determining that single layers were insufficient, I tested different blocks of layers (Middle vs. Late) to see where the steering vector would be most effective.

Observations:

Conclusion: Steering the "middle" of the model creates an adversarial relationship between the layers. To prevent self-correction, the intervention must occur closer to the output head.

Step 3: Finding the Final Output

Objective: Based on the previous findings, I targeted the Late Layers (20, 22, 24, 26) with a high steering coefficient (>40) to bypass the model's correction mechanisms entirely.

Results:

Conclusion: The optimal configuration for identity shifting is Late Layer Intervention combined with a high Steering Coefficient (42.5). This specific combination is required to mathematically overpower the robust safety and identity training embedded in modern instruction-tuned models.

Limitations

Semantic Drift Over Long Generations

In multi-turn or long outputs, the effect of a single steering vector may decay, leading the model to revert to its default behavior.

Hardware / Performance Constraints

Unintended Behavior

Steering can cause unexpected hallucinations or behavior that is logically inconsistent with the concept.

Conclusion

Steering vectors provide a direct and interpretable way to control language model behavior by modifying internal activations rather than relying on prompts or permanent fine-tuning. By treating persona and identity as linear directions in activation space, we can inject specific traits in a targeted and reversible manner.

While semantic representations emerge in middle layers, modern instruction-tuned models actively correct identity shifts. In practice, stable persona transfer required late-layer intervention with sufficiently strong steering. This highlights both where identities are enforced inside models and how activation steering can selectively override them.

Overall, steering vectors offer a powerful framework for understanding and controlling model behavior at inference time.

💻 View Code on GitHub

References

  1. Model Used for Experimenting: https://huggingface.co/microsoft/Phi-3-mini-4k-instruct
  2. Steering vectors: tailor LLMs without training. Part I: Theory (Interpretability Series): https://www.youtube.com/watch?v=cp-YSyc5aW8&t=1332s
  3. Steering LLM Behavior Without Fine-Tuning: http://youtube.com/watch?v=F2jd5WuT-zg&t=376s
  4. Refusal in Language Models Is Mediated by a Single Direction: https://arxiv.org/pdf/2406.11717
  5. STEERING LANGUAGE MODELS WITH ACTIVATION ENGINEERING: https://arxiv.org/pdf/2308.10248
  6. Reference Code: https://github.com/abrvkh/explainability_toolkit/blob/main/notebooks/phi3_steering_vectors.ipynb