Inferensys

Glossary

Logits

Logits are the raw, unnormalized output scores from a neural network's final linear layer, which are transformed into probability distributions via a softmax function.
AI evaluator reviewing output quality on laptop, comparison metrics visible, casual evaluation session.
MODEL DISTILLATION

What are Logits?

The raw, unnormalized scores output by a neural network's final linear layer before activation.

Logits are the unnormalized output vectors from the final linear layer of a neural network, representing the model's raw scores for each possible class or token before any probability transformation. In knowledge distillation, these logits are the primary vehicle for transferring 'dark knowledge'—the rich, inter-class relationships learned by a large teacher model—to a smaller student model. The teacher's logits are softened using temperature scaling to create informative soft targets for the student to mimic.

The student model is trained using a distillation loss, typically the Kullback-Leibler Divergence Loss, which minimizes the statistical difference between the softened logit distributions of the teacher and student. This process allows the compact student to achieve accuracy comparable to the larger teacher, making logits a fundamental concept for inference optimization and latency reduction through techniques like model distillation and the creation of efficient variants such as DistilBERT and TinyBERT.

MODEL DISTILLATION

Key Characteristics of Logits

In knowledge distillation, logits are the raw, unnormalized scores output by a neural network's final linear layer. Their properties are central to transferring 'dark knowledge' from a teacher model to a student.

01

Unnormalized Output Vectors

Logits are the raw, unnormalized scores produced by the final linear (fully connected) layer of a neural network before any activation function is applied. They represent the model's evidence for each possible class.

  • Key Property: They are not probabilities; their values can range from negative to positive infinity.
  • Example: In a 3-class problem, a logit vector might be [2.1, -1.7, 0.5], where the first class has the strongest evidence.
02

Source of Dark Knowledge

The relative magnitudes and ordering of logits contain rich information about the teacher model's learned inter-class relationships, known as dark knowledge. This is the core signal used in distillation.

  • A high logit for the correct class and a moderately high logit for a visually similar class (e.g., 'cat' and 'lynx') provides more learning signal than a simple one-hot label.
  • This relational information helps the student model generalize better and become more calibrated.
03

Transformation via Softmax & Temperature

To be used as training targets, logits are converted into a probability distribution using the softmax function. In distillation, a temperature parameter (T) is introduced to control the smoothness of this distribution.

  • Formula: softmax(logits_i) = exp(logits_i / T) / sum(exp(logits_j / T))
  • Effect of T:
    • T = 1: Standard softmax, producing the teacher's native probabilities.
    • T > 1: 'Softens' the distribution, amplifying the dark knowledge in non-top logits.
    • T → ∞: Approaches a uniform distribution.
04

Contrast with Hard Labels

Logits/Soft Targets differ fundamentally from hard labels (one-hot encoded ground truth). This distinction is why distillation often outperforms training on hard labels alone.

AspectHard LabelSoft Target (from Logits)
Format[0, 1, 0][0.05, 0.90, 0.05]
InformationOnly the correct class.Relative confidence across all classes.
Gradient SignalSparse.Dense, providing a richer learning signal per example.
05

Role in Distillation Loss

The student model is trained by minimizing a distillation loss that measures the difference between its softened outputs and the teacher's softened outputs (derived from logits). The standard loss is a weighted combination:

  • Distillation Term: Kullback-Leibler Divergence between the student and teacher softmax outputs (with temperature).
  • Hard Label Term: Standard cross-entropy with ground-truth labels.
  • Objective: L_total = α * L_CE(hard_labels) + (1-α) * T² * L_KL(teacher_soft || student_soft) The term scales the gradients from the KL divergence to match the magnitude of those from the hard labels.
06

Numerical Stability & Practical Use

Working directly with logits and the softmax function requires attention to numerical stability, especially when using temperature scaling.

  • Best Practice: Implement the LogSoftmax and KL Divergence functions together in a numerically stable way, often provided as a single operation in deep learning frameworks (e.g., F.kl_div(F.log_softmax(...), F.softmax(...))).
  • Initialization: The student model's final layer (which produces its logits) is often initialized to match the scale of the teacher's logits to facilitate stable optimization from the start of training.
CORE CONCEPT

The Role of Logits in Knowledge Distillation

Logits are the fundamental numerical signal enabling the transfer of 'dark knowledge' from a large teacher model to a compact student model.

In knowledge distillation, logits are the unnormalized, real-valued output vectors from a neural network's final linear layer before the softmax activation. These raw scores contain the model's relative confidence across all possible classes, encoding richer relational information—termed dark knowledge—than simple one-hot labels. During distillation, the teacher model's logits are softened using temperature scaling (a hyperparameter T > 1) to produce a smoother probability distribution. This softened distribution becomes the primary training target for the student model, which learns to mimic these probabilities rather than just the hard ground-truth labels.

The student model is trained using a distillation loss function, typically a weighted combination of the standard cross-entropy loss with true labels and a mimicry loss like Kullback-Leibler (KL) Divergence that measures the statistical distance between the teacher's and student's softened outputs. By aligning its logits with the teacher's, the student internalizes the teacher's nuanced understanding of class similarities and decision boundaries. This process allows the smaller student to achieve accuracy comparable to the larger teacher while being significantly more efficient for inference, directly supporting the goals of model compression and latency reduction in production systems.

CORE CONCEPTS

Logits vs. Probabilities: A Technical Comparison

A fundamental comparison of the raw, unnormalized scores (logits) output by a neural network's final linear layer and the normalized probability distributions produced by the softmax function, with a focus on their roles in knowledge distillation and inference optimization.

Feature / PropertyLogitsProbabilities (Softmax Output)

Definition

The unnormalized, real-valued scores (often called log-odds) output by the final linear layer of a neural network prior to activation.

The normalized probability distribution over possible output classes, generated by applying the softmax function to the logits.

Mathematical Form

Vector z ∈ ℝ^C, where C is the number of classes. No inherent constraints on value range or sum.

Vector p ∈ (0,1)^C, where ∑_{i=1}^C p_i = 1 and p_i > 0. p = softmax(z) = exp(z_i) / ∑_j exp(z_j).

Value Range

(-∞, +∞). Can be any real number.

(0, 1). Bounded between 0 and 1, exclusive.

Sum of Vector

Unconstrained. Sum has no meaningful interpretation.

Constrained to sum to 1 (100%). Represents a valid probability distribution.

Interpretation in Distillation

The primary carrier of the teacher model's 'dark knowledge'. Relative magnitudes encode rich inter-class relationships.

The softened target distribution used for the student's mimicry loss (e.g., KL Divergence). Smoothness is controlled by temperature scaling.

Role in Training Loss

Used internally by frameworks; standard cross-entropy loss compares logits to ground truth via a softmax internally.

Explicitly used in distillation loss functions (e.g., KL Divergence) to compare teacher and student distributions.

Numerical Stability

Raw values can be very large or small, leading to overflow/underflow in exp() during softmax. Requires stable implementation (e.g., log-softmax).

Inherently stable after normalization, but direct computation from large logits can be unstable without careful coding.

Gradient Flow

Gradients can flow directly and are typically well-behaved. The softmax gradient involves the probabilities themselves: ∂L/∂z = p - y.

Gradients with respect to probabilities are simpler but are usually computed via the chain rule through the logits for efficiency.

Information Content

Contains maximal information before normalization. The full 'pre-softmax' signal.

Contains compressed information; extreme probabilities (near 0 or 1) lose fine-grained comparative detail.

Temperature Scaling (T)

Operation: z_scaled = z / T. Linearly scales the logit vector.

Operation: p = softmax(z / T). Exponentially affects the output distribution. As T → ∞, p → uniform distribution.

Use in Inference (Argmax)

argmax(z) is identical to argmax(p). The class with the highest logit is the predicted class.

argmax(p) is the standard final prediction. The probability value indicates model confidence.

Storage & Memory Footprint

Identical to probabilities (C floating-point values).

Identical to logits (C floating-point values).

LOGITS

Frequently Asked Questions

Logits are the unnormalized output vectors from a neural network's final layer. In knowledge distillation, they are the primary vehicle for transferring 'dark knowledge' from a teacher model to a student. This FAQ clarifies their role, mechanics, and importance in model compression.

Logits are the raw, unnormalized output scores produced by the final linear (fully connected) layer of a neural network, prior to any activation function like softmax. They represent the model's evidence for each possible class or token before being converted into a probability distribution.

In a classification model, if there are C classes, the logits are a vector of length C. A higher logit value for a given class indicates stronger evidence from the model that the input belongs to that class. The transformation from logits to probabilities is typically done via the softmax function, which exponentiates and normalizes the logits, ensuring they sum to 1.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.