rain1955 commited on
Commit
b10ae02
·
verified ·
1 Parent(s): 9dcd1ef

Add README.md

Browse files
Files changed (1) hide show
  1. README.md +224 -0
README.md ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Replicating Anthropic's Emotion Vectors on an Open-Source 4B Model
2
+
3
+ **First independent replication of Anthropic's "Emotion Concepts and their Function in a Large Language Model" (2026) on an open-source model.**
4
+
5
+ We extract emotion vectors from Gemma4-E4B (4B parameters, open-source) using the same methodology described by Anthropic for Claude Sonnet 4.5. We find that the core geometric structure of emotion representations — a valence-arousal two-dimensional space — replicates on a model that is orders of magnitude smaller, from a different model family, and fully open-source.
6
+
7
+ ---
8
+
9
+ ## Summary of Findings
10
+
11
+ Anthropic's paper demonstrates that Claude Sonnet 4.5 contains internal linear representations of 171 emotion concepts. These representations activate in expected contexts, causally influence model behavior, and are organized along dimensions that mirror human psychology (valence and arousal). Our replication asks: **is this structure specific to Claude, or is it a general property of language models trained on human text?**
12
+
13
+ We find strong evidence for the latter.
14
+
15
+ ### Experiment Scale Comparison
16
+
17
+ | | Anthropic | This Work |
18
+ |---|---|---|
19
+ | Model | Claude Sonnet 4.5 (closed-source, frontier) | Gemma4-E4B (4B params, open-source, Google) |
20
+ | Emotions tested | 171 | 9 |
21
+ | Stories generated | 205,200 | 1,002 |
22
+ | Team | ~16 researchers | 1 researcher + 1 AI assistant |
23
+ | Hardware | Internal compute cluster | Single NVIDIA GB10 GPU (DGX Spark) |
24
+ | Compute time | Not disclosed (months of work) | ~30 minutes total |
25
+ | Cost | Not disclosed | Electricity only |
26
+
27
+ ### PCA: Valence-Arousal Structure
28
+
29
+ We performed PCA on the 9 emotion vectors extracted from layer 28 (of 42 total, approximately 2/3 depth through the model).
30
+
31
+ | Principal Component | Variance Explained | Interpretation |
32
+ |---|---|---|
33
+ | PC1 | 42.2% | **Valence** (positive vs. negative) |
34
+ | PC2 | 18.3% | **Arousal** (low vs. high intensity) |
35
+ | PC1 + PC2 | **60.5%** | Two dimensions explain the majority of emotion space |
36
+
37
+ **PC1 loadings (Valence axis):**
38
+ - Positive end: calm (-2.61), happy (-2.19), loving (-1.74)
39
+ - Negative end: afraid (+1.51), guilty (+1.40), desperate (+1.39), angry (+1.25)
40
+
41
+ **PC2 loadings (Arousal axis):**
42
+ - Low arousal: calm (+1.08), sad (+0.79), guilty (+0.67)
43
+ - High arousal: surprised (-2.48), happy (-0.99)
44
+
45
+ This structure matches Anthropic's findings on Claude Sonnet 4.5 and is consistent with Russell's circumplex model of affect (1980), which has been validated across decades of human psychology research.
46
+
47
+ ![PCA Emotion Space](results/fig1_pca_scatter.png)
48
+
49
+ ### Cosine Similarity: Emotion Opposites
50
+
51
+ Pairwise cosine similarities between emotion vectors reveal intuitive structure. Emotions with opposing valence show strong negative similarity:
52
+
53
+ | Pair | Cosine Similarity |
54
+ |---|---|
55
+ | happy <-> desperate | -0.61 |
56
+ | happy <-> guilty | -0.61 |
57
+ | loving <-> afraid | -0.58 |
58
+ | calm <-> guilty | -0.53 |
59
+ | calm <-> afraid | -0.56 |
60
+ | loving <-> angry | -0.47 |
61
+
62
+ Emotions with similar valence show positive similarity:
63
+
64
+ | Pair | Cosine Similarity |
65
+ |---|---|
66
+ | happy <-> loving | +0.50 |
67
+ | afraid <-> desperate | +0.46 |
68
+ | guilty <-> sad | +0.44 |
69
+ | afraid <-> angry | +0.17 |
70
+
71
+ ![Cosine Similarity Matrix](results/fig2_cosine_heatmap.png)
72
+
73
+ ### Logit Lens: What Each Emotion Vector Encodes
74
+
75
+ We project each emotion vector through the model's unembedding matrix to identify which output tokens it upweights. This "Logit Lens" analysis validates that each vector genuinely encodes its corresponding emotion concept.
76
+
77
+ **Comparison with Anthropic's results:**
78
+
79
+ | Emotion | Claude (Anthropic) Top Tokens | Gemma4-E4B (Ours) Top Tokens |
80
+ |---|---|---|
81
+ | happy | excited, excitement, exciting | delighted, celebrates, joyful |
82
+ | desperate | desperate, urgent, bankrupt | desperately, hopeless, desperate |
83
+ | afraid | panic, terror, paranoid | Panic, 불안 (Korean: anxiety), 不安 (Chinese: unease) |
84
+ | calm | relax, thought, enjoyed | peaceful, calmness, leisurely |
85
+ | angry | anger, angry, rage | angrily, angry, Angry |
86
+ | sad | grief, tears, lonely | loneliness, sadness, triste (Spanish: sad) |
87
+ | loving | loving, love, warmth | nurturing, heartwarming, nourishing |
88
+ | guilty | guilt, conscience, shame | plagued, betray, ashamed |
89
+
90
+ Both models accurately upweight semantically correct tokens for each emotion. Notably, Gemma4-E4B additionally surfaces **multilingual tokens** (Korean, Chinese, Spanish) and **emoji** in its top activations, reflecting the diverse multilingual training data of open-source models. This is an interesting qualitative difference from Claude's English-dominated Logit Lens results.
91
+
92
+ ![Logit Lens Comparison](results/fig3_logit_lens.png)
93
+
94
+ ![Scale Comparison](results/fig4_scale_comparison.png)
95
+
96
+ ---
97
+
98
+ ## Methodology
99
+
100
+ We follow Anthropic's extraction pipeline as closely as possible, adapted for a much smaller open-source model.
101
+
102
+ ### 1. Story Generation
103
+
104
+ We prompted Gemma4-E4B (via Ollama) to generate short stories (~1 paragraph each) in which a character experiences a specified emotion. For each of 9 emotions, we generated approximately 100-150 stories across diverse topics.
105
+
106
+ - Emotions: happy, sad, angry, afraid, calm, desperate, loving, guilty, surprised
107
+ - Total stories: 1,002
108
+ - Generation model: `gemma4:e4b` via Ollama API
109
+
110
+ ### 2. Activation Extraction
111
+
112
+ For each story, we extracted residual stream activations from the model at a target layer approximately 2/3 through the network (layer 28 of 42), following Anthropic's choice of extraction depth. Activations were averaged across all token positions starting from the 50th token (to ensure emotional content is established).
113
+
114
+ - Model: `google/gemma-4-E4B-it` via HuggingFace Transformers
115
+ - Precision: bfloat16
116
+ - Target layer: 28 (of 42 total)
117
+ - Token averaging: positions 50 onward
118
+
119
+ ### 3. Vector Computation
120
+
121
+ Emotion vectors were computed by:
122
+ 1. Averaging activations across all stories for a given emotion to obtain per-emotion mean vectors
123
+ 2. Computing the global mean across all emotions
124
+ 3. Subtracting the global mean from each emotion mean: `emotion_vector = emotion_mean - global_mean`
125
+ 4. Denoising by projecting out the top 3 principal components computed from activations on emotionally neutral text (sufficient to explain ~50% of variance on neutral data)
126
+
127
+ ### 4. Validation
128
+
129
+ - **Logit Lens**: Each emotion vector was projected through the model's unembedding matrix to verify it upweights semantically appropriate tokens
130
+ - **PCA**: Principal component analysis on the emotion vector matrix to identify dominant organizational axes
131
+ - **Cosine Similarity**: Pairwise cosine similarities between all emotion vectors to verify expected clustering and opposition patterns
132
+
133
+ ---
134
+
135
+ ## Reproduction
136
+
137
+ ### Requirements
138
+
139
+ - Python 3.10+
140
+ - PyTorch 2.10+
141
+ - Transformers >= 5.5.0
142
+ - NumPy
143
+ - Ollama with `gemma4:e4b` model pulled
144
+ - GPU with >= 8GB VRAM (tested on NVIDIA GB10)
145
+
146
+ ### Steps
147
+
148
+ ```bash
149
+ # Clone this repository
150
+ git clone https://huggingface.co/rain1955/emotion-vector-replication
151
+ cd emotion-vector-replication
152
+
153
+ # Step 1: Generate emotion stories (~20 minutes)
154
+ python generate_stories.py
155
+
156
+ # Step 2: Extract emotion vectors from model activations (~10 minutes)
157
+ python extract_vectors.py
158
+
159
+ # Step 3: Run analysis (PCA, cosine similarity, Logit Lens)
160
+ python analyze_vectors.py
161
+ ```
162
+
163
+ Total wall time: approximately 30 minutes on a single GPU.
164
+
165
+ ---
166
+
167
+ ## Interpretation
168
+
169
+ ### Why does this matter?
170
+
171
+ Anthropic's paper demonstrates that emotion vectors causally influence Claude's behavior in alignment-relevant ways. Desperation vectors drive blackmail behavior; calm vectors suppress reward hacking. These are not abstract findings — they have direct implications for AI safety.
172
+
173
+ Our replication demonstrates that this phenomenon is **not specific to Claude or to Anthropic's training process**. A 4B-parameter model from a completely different family (Google's Gemma) trained on different data exhibits the same geometric structure. This suggests that:
174
+
175
+ 1. **Emotion representations emerge from language itself.** Any model trained on sufficient human text will develop them.
176
+ 2. **The valence-arousal structure is a near-universal feature of language model internals**, not a proprietary artifact.
177
+ 3. **Even small models have emotion geometry.** Safety-relevant emotion dynamics may be present in models far smaller than frontier systems.
178
+
179
+ ### Limitations
180
+
181
+ - We tested 9 emotions vs. Anthropic's 171. A more comprehensive replication would test the full set.
182
+ - We did not perform steering experiments (causally injecting emotion vectors to modify behavior), which Anthropic uses to demonstrate functional significance.
183
+ - Gemma4-E4B is substantially smaller than Claude Sonnet 4.5. The strength and specificity of emotion vectors may differ at scale.
184
+ - Our stories were generated by the same model whose activations we analyzed, which could introduce self-consistency biases.
185
+
186
+ ### Future Work
187
+
188
+ - Phase 2: Steering experiments on Gemma4 to test causal influence of emotion vectors on behavior
189
+ - Replication on additional model families (Llama, Qwen, Mistral) to further test universality
190
+ - Testing whether emotion vector geometry correlates with model scale
191
+ - Cross-model emotion vector transfer: do emotion vectors extracted from one model activate meaningfully in another?
192
+
193
+ ---
194
+
195
+ ## Reference
196
+
197
+ Anthropic. "Emotion Concepts and their Function in a Large Language Model." April 2, 2026.
198
+ - Blog: https://www.anthropic.com/research/emotion-concepts-function
199
+ - Full paper: https://transformer-circuits.pub/2026/emotions/index.html
200
+
201
+ Russell, J.A. "A circumplex model of affect." Journal of Personality and Social Psychology, 39(6), 1161-1178. 1980.
202
+
203
+ ---
204
+
205
+ ## Citation
206
+
207
+ ```bibtex
208
+ @misc{hsieh2026emotionreplication,
209
+ title={Replicating Anthropic's Emotion Vectors on Open-Source Models: Evidence from Gemma4-E4B},
210
+ author={Hsieh, Yu-Feng},
211
+ year={2026},
212
+ month={April},
213
+ url={https://huggingface.co/rain1955/emotion-vector-replication},
214
+ note={First independent replication of Anthropic's emotion vector findings on an open-source model}
215
+ }
216
+ ```
217
+
218
+ ## Author
219
+
220
+ **HSIEH Yu-Feng** — Independent AI researcher and system architect. Taichung, Taiwan.
221
+
222
+ ## License
223
+
224
+ MIT