Absolutely—if you know that your 2‐way classifier is just “Yes” vs “No,” you can seed your new head with the same weight vectors the LM uses to predict those words. In HuggingFace transformers, the LM head (in a GPT‐style model it’s model.lm_head.weight, in a BERT‐style masked‐LM it’s model.cls.predictions.decoder.weight) is typically tied to the input embeddings. You can grab the rows for “Yes” and “No” and stick them right into your AutoModelForSequenceClassification head.
Here’s a minimal example using a causal‐LM (e.g. GPT‑2 → 2‑label classifier):
import torch
from transformers import AutoModelForCausalLM, AutoModelForSequenceClassification, AutoTokenizer
# 1) Load tokenizer & models
model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
# load a plain LM so we have lm_head
lm_model = AutoModelForCausalLM.from_pretrained(model_name)
# load a seq‐clf (same base, but num_labels=2)
clf_model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)
# 2) Identify the token‐ids for "Yes" and "No"
yes_id = tokenizer.convert_tokens_to_ids("Yes")
no_id = tokenizer.convert_tokens_to_ids("No")
# 3) Extract the LM‐head weight & bias for those tokens
lm_w = lm_model.lm_head.weight # [vocab_size × hidden_size]
lm_b = getattr(lm_model.lm_head, "bias", None)
yes_w = lm_w[yes_id] # [hidden_size]
no_w = lm_w[no_id]
# 4) Initialize your classifier head
# classifier.weight has shape [num_labels × hidden_size]
new_w = torch.stack([yes_w, no_w], dim=0)
clf_model.classifier.weight.data.copy_(new_w)
# 5) (Optionally) initialize bias too, if you want to inherit the LM prior
if lm_b is not None:
yes_b = lm_b[yes_id]
no_b = lm_b[no_id]
clf_model.classifier.bias.data.copy_(torch.tensor([yes_b, no_b]))
# Now clf_model.classifier starts off “speaking the same language” as the LM head for
# exactly Yes/No. You can still fine‐tune normally on your classification data.
A few caveats:
- If “Yes”/“No” are multi‑token in your tokenizer, you might average (or take the first) token’s vector.
- In BERT/MaskedLM‐style models the LM head lives under
model.cls.predictions.decoder.weight instead, and bias under model.cls.predictions.bias.
- You still want to fine‑tune the classifier (and probably the rest of the model) on your labeled data—this is just a smarter warm start, not a replacement for task supervision.