BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Fine-tuning13 of 19 in topic

512. Turn an LM head into a classifier with label words

MediumFine-tuning Pro

Why it matters Initialising a classifier from label-word embeddings gives a strong zero-shot starting point and speeds up few-shot fine-tuning.

A pretrained LM already scores every vocabulary word. For a classification task with label words like "yes" and "no", you can reuse those rows of the output embedding as the classifier, before or instead of training a new head.

Write verbalizer_probs(h, E, label_ids). h is (n, d) final hidden states, E is the (V, d) output embedding, label_ids is a list of C token ids. Build W = E[label_ids] (C, d), logits = h @ W.T (n, C), and return (W, probs) where probs is a stable softmax over the C label logits only (renormalised among the labels).

Functiondef verbalizer_probs(h, E, label_ids)

Example 1

Input: h = [[1, 0]], E = [[1, 0], [0, 1], [2, 0]], label_ids = [2, 1]

Output: W = [[2, 0], [0, 1]] and probs = [[0.8808, 0.1192]]

Example 2

Input: h = Hv, E = Ev, label_ids = [4, 0, 2]

Output: ([[0.9, -0.99, -0.1], [-2.56, -1.54, 0.61], [1.12, 0.02, -1.34]], [[0.203658, 0.004597, 0.791746], [0.37217, 0.032361, 0.595469]])

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Gather the label rows, compute logits, softmax over the label axis only.

Hint 2

Fancy-index the rows of E.

Hint 3

Normalise over C, not V.

Common mistakes
  • Normalising over the whole vocabulary.
  • Using columns of E instead of rows.
Understand the idea

A language model's last layer already knows what "yes" and "no" mean: each word has a row in the output embedding, and the score for a word is the dot product of the hidden state with that row. For a yes/no task you can simply keep those two rows as a tiny classifier, like hiring someone who already knows the job.

Worked example

hidden h = [1, 0]. The row for label word A is [2, 0] and for B is [0, 1]. Scores are 2 and 0. Softmax over just these two gives e^2 / (e^2 + 1) = 0.881 and 0.119.

W = E[label_ids]; logits = h W^T; probs = softmax(logits) over the C labels

Renormalising over the labels removes probability mass the LM gives to all other words. This verbaliser idea comes from prompt-based few-shot learning, and the same rows make a good initial weight when you add and train a real classification head.

Shapes and data flow
h (n, d), E (V, d) -> W = E[label_ids] (C, d) -> logits (n, C) -> probs (n, C)
Topics
Fine-tuning
Similar questions
  • LoRA forward pass and weight mergeMedium
  • Attention with trainable prefix keys and values (prefix tuning)Medium
  • Pack examples into fixed-length training rowsMedium
  • Compute a DoRA adapted weightMedium
All Fine-tuning problems
Report
512 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def verbalizer_probs(h, E, label_ids):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS