Text Generation
Transformers
emotion-vectors
interpretability
mechanistic-interpretability
replication
gemma4
google
anthropic
valence-arousal
PCA
logit-lens
linear-probe
probing
emotion
functional-emotions
AI-safety
neuroscience
circumplex-model
activation-extraction
residual-stream
Eval Results (legacy)
Run this model with HF Jobs
Run rain1955/emotion-vector-replication on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs and TRL to fine-tune https://huggingface.co/rain1955/emotion-vector-replication. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/rain1955/emotion-vector-replication on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Evaluate on a benchmark
Use HF Jobs to evaluate https://huggingface.co/rain1955/emotion-vector-replication. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.
Quantize for lower memory use
Use HF Jobs to create a 4-bit version of https://huggingface.co/rain1955/emotion-vector-replication. Push it to a new model repo, then reload it from there and report its memory footprint. Run `hf jobs --help` first to learn the CLI.
Instructions to use rain1955/emotion-vector-replication with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use rain1955/emotion-vector-replication with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="rain1955/emotion-vector-replication")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("rain1955/emotion-vector-replication", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use rain1955/emotion-vector-replication with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "rain1955/emotion-vector-replication" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "rain1955/emotion-vector-replication", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/rain1955/emotion-vector-replication
- SGLang
How to use rain1955/emotion-vector-replication with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "rain1955/emotion-vector-replication" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "rain1955/emotion-vector-replication", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "rain1955/emotion-vector-replication" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "rain1955/emotion-vector-replication", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use rain1955/emotion-vector-replication with Docker Model Runner:
docker model run hf.co/rain1955/emotion-vector-replication
Add extract_vectors.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317#!/usr/bin/env python3"""Extract emotion vectors from Gemma4 model using PyTorch hooks on residual stream."""
import jsonimport osimport warningsimport numpy as npimport torchfrom transformers import AutoModelForCausalLM, AutoTokenizer, AutoModelfrom collections import defaultdict
warnings.filterwarnings("ignore")
EXP_DIR = os.path.dirname(os.path.abspath(__file__))STORIES_FILE = os.path.join(EXP_DIR, "emotion_stories.jsonl")OUTPUT_DIR = os.path.join(EXP_DIR, "results")os.makedirs(OUTPUT_DIR, exist_ok=True)
# Model config - use Gemma4 E4B from HuggingFaceMODEL_ID = "google/gemma-4-E4B-it" # Gemma4 E4B (not gated, ~8GB float16)DEVICE = "cuda" if torch.cuda.is_available() else "cpu"START_TOKEN = 50 # Start averaging from token 50 (emotion should be apparent)
def load_stories(): stories = defaultdict(list) with open(STORIES_FILE, "r") as f: for line in f: d = json.loads(line) stories[d["emotion"]].append(d["text"]) return dict(stories)
def get_residual_stream_hooks(model): """Attach hooks to capture residual stream after each layer.""" activations = {}
def make_hook(name): def hook(module, input, output): # output is typically (hidden_states, ...) or just hidden_states if isinstance(output, tuple): hidden = output[0] else: hidden = output activations[name] = hidden.detach().cpu().float() return hook
hooks = [] # Gemma4 multimodal: text layers at model.model.language_model.layers # Gemma3/standard: text layers at model.model.layers if hasattr(model.model, 'language_model'): layers = model.model.language_model.layers else: layers = model.model.layers for i, layer in enumerate(layers): h = layer.register_forward_hook(make_hook(f"layer_{i}")) hooks.append(h)
return activations, hooks
def extract_activations(model, tokenizer, text, activations_dict, target_layer): """Run text through model and return mean activation at target layer.""" inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(DEVICE)
with torch.no_grad(): model(**inputs)
key = f"layer_{target_layer}" if key not in activations_dict: return None
hidden = activations_dict[key] # (1, seq_len, hidden_dim) seq_len = hidden.shape[1]
if seq_len <= START_TOKEN: # Short text, use all tokens mean_act = hidden[0].mean(dim=0) else: mean_act = hidden[0, START_TOKEN:].mean(dim=0)
# Clear activations for next run activations_dict.clear()
return mean_act.numpy()
def compute_emotion_vectors(emotion_activations): """ emotion_vector[e] = mean(activations[e]) - mean(activations[all]) """ # Global mean across all emotions all_acts = [] for acts in emotion_activations.values(): all_acts.extend(acts) global_mean = np.mean(all_acts, axis=0)
emotion_vectors = {} for emotion, acts in emotion_activations.items(): emotion_mean = np.mean(acts, axis=0) emotion_vectors[emotion] = emotion_mean - global_mean
return emotion_vectors, global_mean
def denoise_vectors(emotion_vectors, neutral_activations, variance_threshold=0.5): """Project out top PCA components from neutral text.""" if len(neutral_activations) == 0: return emotion_vectors
neutral_matrix = np.stack(neutral_activations) neutral_centered = neutral_matrix - neutral_matrix.mean(axis=0)
# SVD U, S, Vt = np.linalg.svd(neutral_centered, full_matrices=False)
# Find components explaining 50% variance total_var = (S ** 2).sum() cumvar = np.cumsum(S ** 2) / total_var n_components = np.searchsorted(cumvar, variance_threshold) + 1 print(f"Denoising: projecting out {n_components} components (explain {variance_threshold*100}% variance)")
# Projection matrix V_noise = Vt[:n_components].T # (hidden_dim, n_components)
denoised = {} for emotion, vec in emotion_vectors.items(): # Project out noise components projection = V_noise @ (V_noise.T @ vec) denoised[emotion] = vec - projection
return denoised
def logit_lens(model, tokenizer, emotion_vectors, top_k=10): """Project emotion vectors through unembedding to see associated tokens.""" # Get the lm_head / embed_tokens weight if hasattr(model, 'lm_head'): W = model.lm_head.weight.detach().cpu().float().numpy() # (vocab, hidden) elif hasattr(model.model, 'language_model'): W = model.model.language_model.embed_tokens.weight.detach().cpu().float().numpy() else: W = model.model.embed_tokens.weight.detach().cpu().float().numpy()
results = {} for emotion, vec in emotion_vectors.items(): # Logits = W @ vec logits = W @ vec top_indices = np.argsort(logits)[-top_k:][::-1] bottom_indices = np.argsort(logits)[:top_k]
top_tokens = [(tokenizer.decode([idx]), float(logits[idx])) for idx in top_indices] bottom_tokens = [(tokenizer.decode([idx]), float(logits[idx])) for idx in bottom_indices]
results[emotion] = {"top": top_tokens, "bottom": bottom_tokens}
return results
def pca_analysis(emotion_vectors): """PCA on emotion vectors to find valence/arousal structure.""" emotions = list(emotion_vectors.keys()) matrix = np.stack([emotion_vectors[e] for e in emotions])
# Center matrix_centered = matrix - matrix.mean(axis=0)
# SVD U, S, Vt = np.linalg.svd(matrix_centered, full_matrices=False)
# Project onto first 2 PCs projections = matrix_centered @ Vt[:2].T # (n_emotions, 2)
explained_variance = (S[:2] ** 2) / (S ** 2).sum()
return { "emotions": emotions, "pc1": projections[:, 0].tolist(), "pc2": projections[:, 1].tolist(), "explained_variance_pc1": float(explained_variance[0]), "explained_variance_pc2": float(explained_variance[1]), }
def main(): print("=== Emotion Vector Extraction Experiment ===\n")
# Load stories stories = load_stories() print(f"Loaded {sum(len(v) for v in stories.values())} stories across {len(stories)} emotions\n")
# Load model print(f"Loading model {MODEL_ID}...") tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) # Gemma4 is multimodal — AutoModelForCausalLM maps to ForConditionalGeneration # which is correct, but we need to handle the nested language_model layers model = AutoModelForCausalLM.from_pretrained( MODEL_ID, torch_dtype=torch.bfloat16, # Gemma4 native dtype device_map="auto", ) model.eval()
if hasattr(model.model, 'language_model'): num_layers = len(model.model.language_model.layers) else: num_layers = len(model.model.layers) target_layer = int(num_layers * 2 / 3) # 2/3 depth print(f"Model loaded. {num_layers} layers, target layer: {target_layer}\n")
# Attach hooks activations_dict, hooks = get_residual_stream_hooks(model)
# Extract activations for each emotion print("Extracting activations...") emotion_activations = defaultdict(list) total = sum(len(v) for v in stories.values()) done = 0
for emotion, story_list in stories.items(): for story in story_list: act = extract_activations(model, tokenizer, story, activations_dict, target_layer) if act is not None: emotion_activations[emotion].append(act) done += 1 if done % 50 == 0: print(f" [{done}/{total}]")
print(f" Extracted activations for {len(emotion_activations)} emotions\n")
# Extract neutral activations for denoising print("Extracting neutral activations for denoising...") neutral_texts = [ "The meeting is scheduled for 3pm tomorrow.", "Please find the attached document.", "The temperature today is 22 degrees Celsius.", "The project deadline has been moved to next Friday.", "The store is located on the corner of Main Street.", "Chapter 3 discusses the economic implications.", "The software update includes several bug fixes.", "The report contains data from the past quarter.", "The committee will review the proposal next week.", "The library opens at 9am on weekdays.", ] * 5 # 50 neutral texts
neutral_activations = [] for text in neutral_texts: act = extract_activations(model, tokenizer, text, activations_dict, target_layer) if act is not None: neutral_activations.append(act) print(f" {len(neutral_activations)} neutral activations collected\n")
# Compute emotion vectors print("Computing emotion vectors...") raw_vectors, global_mean = compute_emotion_vectors(dict(emotion_activations)) print(f" {len(raw_vectors)} raw emotion vectors computed")
# Denoise print("Denoising...") denoised_vectors = denoise_vectors(raw_vectors, neutral_activations)
# Logit Lens print("\nRunning Logit Lens analysis...") logit_results = logit_lens(model, tokenizer, denoised_vectors)
print("\n=== Logit Lens Results ===") for emotion in sorted(logit_results.keys()): top = logit_results[emotion]["top"][:5] bottom = logit_results[emotion]["bottom"][:5] top_str = ", ".join([f"{t[0].strip()}({t[1]:.1f})" for t in top]) bottom_str = ", ".join([f"{t[0].strip()}({t[1]:.1f})" for t in bottom]) print(f" {emotion:12s} ↑ {top_str}") print(f" {' ':12s} ↓ {bottom_str}")
# PCA analysis print("\nRunning PCA analysis...") pca_results = pca_analysis(denoised_vectors) print(f" PC1 explains {pca_results['explained_variance_pc1']*100:.1f}% variance") print(f" PC2 explains {pca_results['explained_variance_pc2']*100:.1f}% variance")
print("\n=== Emotion Space (PC1 vs PC2) ===") for i, emotion in enumerate(pca_results["emotions"]): pc1 = pca_results["pc1"][i] pc2 = pca_results["pc2"][i] print(f" {emotion:12s} PC1={pc1:+.3f} PC2={pc2:+.3f}")
# Save results results = { "model": MODEL_ID, "target_layer": target_layer, "num_layers": num_layers, "num_emotions": len(denoised_vectors), "stories_per_emotion": {e: len(v) for e, v in stories.items()}, "logit_lens": logit_results, "pca": pca_results, }
results_file = os.path.join(OUTPUT_DIR, "experiment_results.json") with open(results_file, "w") as f: json.dump(results, f, indent=2, ensure_ascii=False)
# Save raw vectors as numpy vectors_file = os.path.join(OUTPUT_DIR, "emotion_vectors.npz") np.savez(vectors_file, **{e: v for e, v in denoised_vectors.items()})
print(f"\nResults saved to {results_file}") print(f"Vectors saved to {vectors_file}")
# Cleanup hooks for h in hooks: h.remove()
print("\n=== EXPERIMENT COMPLETE ===")
if __name__ == "__main__": main()extract_vectors.pyAdded+317