Harmonic loss trains interpretable AI modelsHarmonic Loss Trains Interpretable AI Models
The authors train GPT-2 with harmonic loss, validation loss 3.146 on OpenWebText vs. 3.159 for the standard model.
- The authors partly attribute insufficient interpretability, low data efficiency, and grokking to SoftMax in cross-entropy, hoping for a scale-invariant supervision signal with finite convergence points.
- The final logits are changed to Euclidean distances between representations and class vectors, then normalized by HarMax using negative powers of distances. Class weights are treated as class centers; algorithm experiments use n=1, GPT-2 uses about 28.
- The authors state that algorithmic models are more compact, generalize with less data, and have less grokking. MNIST test accuracy 92.49% vs. 92.50%; GPT-2 validation loss on OpenWebText 3.146 vs. 3.159. CoLA layer 6 probe F1 is not higher.
- Section 8 states that scalability on larger models still needs study. Appendix C states whether heavy tails are a feature or a defect remains to be understood. Experiments go up to GPT-2 small and ResNet-50; the paper does not report the number of samples in algorithmic data.
- Evaluated models
- Harmonic MLPHarmonic TransformerHarmonic GPTOne-layer neural network (MNIST, harmonic loss)ResNet-50 (harmonic loss)
- Benchmarks
- In-Context LearningModular AdditionEquivalence ClassesGenealogy LearningPermutation CompositionMNISTImageNetOpenWebTextfunction-vector tasksSST2CoLA
- Metrics
- explained variancetest accuracyvalidation lossparallelogram lossTop-1 accuracyTop-5 accuracyF1
The paper proposes harmonic loss as an alternative supervision signal for training neural networks and large language models. Its difference from standard cross-entropy lies in replacing SoftMax normalization with the scale-invariant HarMax function and computing logits with Euclidean distance rather than dot product. The authors state that scale invariance and the designed finite set of convergence points (interpretable as class centers) bring better interpretability and faster convergence. The authors validate on algorithmic, vision, and language datasets, claiming that models trained with harmonic loss are more interpretable, require less data for generalization, and exhibit reduced grokking compared to standard models. Compared to standard GPT-2, GPT-2 trained with harmonic loss learns more interpretable representations. The authors believe this loss could be valuable in data-limited or high-risk scenarios.
Paper analysis
What problem does this paper attempt to solve?
The authors attribute three difficulties in generalization partly to SoftMax and replace cross-entropy with harmonic loss.
SoftMax in cross-entropy is seen by the authors as a partial cause of insufficient interpretability, low data efficiency, and grokking; this paper replaces this supervision signal with harmonic loss.
- Scenario: The authors believe networks can generalize on unseen data, but medical, financial, and autonomous systems require interpretable outputs, and scarce-data domains require generalization from fewer samples.
- Existing practices: The authors state that efficient sampling and accelerated training only optimize existing pipelines, not touching model design; grokking makes stopping time difficult to determine and lengthens computation.
- Authors' attribution: The above three points are partially attributed to SoftMax in categorical cross-entropy. They call it "The devil is in the SoftMax."
- This paper proposes: logits are computed by Euclidean distance, and SoftMax is replaced by scale-invariant HarMax. Convergence points are finite and interpretable as class centers. Comparisons are against standard cross-entropy models on algorithmic tasks, MNIST, and GPT-2.
What related research exists?
Related work covers representation geometry, loss functions, and grokking; the authors compare harmonic loss against cross-entropy.
Representation geometry
- Concept structure: The paper discusses concept representations in space, time, and color, as well as one-dimensional directions, lattices, and circles.
- Unexplained variance: The authors state that there is often still a large amount of unexplained variance beyond these geometries, thus requiring more interpretable representations.
- Mechanistic interpretability: The introduction cites Bereska and Gavves (2024), stating that despite progress in understanding LLM internals, it is still far from fully explaining outputs.
Loss functions
- Loss and representation: The paper states that the loss function changes how the model represents data, and points to surveys by Wang et al. (2022) and Alshammari et al. (2025).
- Contrastive learning: It also uses Euclidean distance to separate classes, but the paper states it is not itself supervised learning, and typically is followed by a cross-entropy layer, thus reintroducing SoftMax.
- Euclidean distance supervision: The paper states that except for simple tasks like sentence sentiment, direct use of Euclidean distance for supervision in language modeling is relatively unexplored; Section 6 states that using only Euclidean distance is insufficient to reproduce harmonic loss.
Data efficiency and grokking
- Pipeline modifications: The introduction mentions efficient sampling and accelerated training. The authors state they optimize existing training pipelines, not model design.
- Grokking: Power et al. (2022) describe generalization occurring after training convergence on small algorithmic datasets. The authors view this delay as a phenomenon to be avoided, citing Liu et al. (2022).
Baseline methods
- Comparison: Standard cross-entropy layer, i.e., dot-product logits plus SoftMax. Appendix D also compares contrastive loss and spherical/cosine loss. Benchmarks are five synthetic algorithmic tasks, MNIST, ImageNet, OpenWebText, and SST2, CoLA, and 12 function-vector tasks.
The authors position the paper as an alternative supervision signal: replacing SoftMax with scale-invariant HarMax, replacing dot product with Euclidean distance, and making weights converge to class centers.
How does the paper address this problem?
Harmonic loss uses Euclidean distance as logits and replaces SoftMax with scale-invariant HarMax.
Replace the final cross-entropy layer with a harmonic layer: the logit is changed to represent the Euclidean distance to the class vector, and the probability is changed to HarMax. Both losses ultimately are −log p, and the authors believe the values are comparable.
Loss definition
- Cross-entropy layer: the unembedding matrix is W, and the penultimate layer is denoted x. The logit is the dot product of the column vector with x, ignoring the bias; the probability is SoftMax, and the loss is −log pc.
- Harmonic layer: di is the l2 distance between the class vector wi and x. wi is treated as the key, x as the query, and the smaller the distance, the larger the probability.
- HarMax: pi=(1/din)/(∑j 1/djn). This formula is the probability of the i-th class, n controls the heavy-tailedness of the distribution, and the loss is still −log pc.
- Exponent: the authors give n approximately equal to the square root of the intrinsic dimension; in LLMs, the embedding dimension approximates this dimension. In the algorithmic experiments, the unembedding takes n=1; for GPT-2, n is the square root of 768, about 28.
Design of relative cross-entropy
- Finite convergence: the authors state that as the probability approaches 1, cross-entropy pushes the logit to infinity, while harmonic only needs to push the correct class distance to 0, for example by learning wi equal to x.
- Scale invariance: multiplying all distances by the same constant does not change the probability; scaling the cross-entropy logit changes the loss.
- Two-dimensional toy: Figure 1 is trained with Adam. In two-point classification, the harmonic loss decreases faster, and the l2 norm enters a plateau; the cross-entropy l2 keeps rising. In five-point classification, the origin cannot linearly separate the points, the cross-entropy loss stops at a high plateau, and the authors state that harmonic can decrease to machine precision, with the weights corresponding to the data points.
- Formal properties: Appendix G gives three theorems. When linearly separable, cross-entropy has no finite minimum; the harmonic loss is scale-invariant and reaches a global minimum at finite weights. Under a distance-interval condition, the authors give a PAC-Bayes-type error bound with finite weight norm. At critical points, the class weights are approximately a convex combination of the samples of that class.
How interpretability is measured
- Compression: use the cumulative explained variance of PCA to see whether semantics are concentrated in a low dimension. The authors state that when the ground truth is lacking, interpretability is difficult to measure directly.
- Geometry: the authors hypothesize that parallelogram units composed of multiple one-dimensional semantic directions support compositional reasoning, and measure this with the parallelogram loss in Section 5.
- Placement: the algorithmic model only replaces the unembedding. GPT-2 has tied embedding and unembedding weights. MNIST is a 784×10 linear layer on flattened images; ImageNet changes the loss on ResNet-50.
What experiments did the paper conduct?
The authors claim harmonic models are more compact and exhibit less grokking; GPT-2 validation loss is 3.146 vs. 3.159.
Experimental setup
- Algorithm models: Standard/Harmonic MLP has 16-dimensional embeddings, hidden layer widths 100 and 16, and SiLU activation. The Transformer has 16-dimensional embeddings, learnable positional embeddings, 2 decoder layers, 2 attention heads, and MLP hidden layer 64. The Harmonic version only changes the unembedding, n=1.
- Optimization: MLP trained for 7000 epochs, Transformer for 10000 epochs. AdamW, learning rate 2×10^{-3}, weight decay 10^{-2}, embedding L2 strength 0.01. The permutation Transformer uses L2 strength 0.005, which the authors state makes training more complete.
- Five tasks: predicting the fourth point from three points on a 5×5 grid to form a parallelogram; addition mod 31; whether an integer from 0 to 39 is congruent mod 5; parent, grandparent, sibling on a 127-node complete binary tree; composition of two permutations in S4.
- Vision and language: MNIST is a single layer 784×10, batch 64, learning rate 0.001, 10 epochs. GPT-2 small (128M, based on NanoGPT) trained on OpenWebText, 8 V100s, block size 1024, batch 480 blocks, Adam with β1=0.9, β2=0.95, 10k steps total, embeddings and unembeddings tied. ResNet-50 trained on ImageNet with AutoAugment for 90 epochs, learning rate 0.1, divided by 10 at epochs 10, 30, 60, 80.
- GPT-2 schedule: standard model warms up over 2k steps to 6×10^{-4}, harmonic over 1k steps to 6×10^{-3}; cosine decay is written as from 2k to 10k steps, with final learning rates of 3×10^{-5} and 3×10^{-4} respectively.
- Metrics: test accuracy, PCA explained variance, validation loss, parallelogram loss, and probe F1. No LLM judge. Grokking plots use 20 seeds; the criterion is reaching accuracy 0.9 for 20 consecutive epochs; runs that do not reach it are omitted. Figure 3(b) is the mean of 3 seeds; the caption of Appendix Figure 14(b) says fixed seed.
Main results
Swipe horizontally to read the table
Setting Cross-entropy Harmonic Source GPT-2 small, OpenWebText, 10k-step validation loss 3.159 3.146 Section 5 MNIST, single layer, 10 epochs, test accuracy 92.50% 92.49% Section 4 ImageNet, ResNet-50, Top-1 74.17% 75.08% Table 1 ImageNet, ResNet-50, Top-5 91.88% 92.12% Table 1 - GPT-2 curves: the authors state harmonic GPT drops faster initially, partly because of the larger learning rate; performance is similar at the end, and training loss fluctuations appear smaller.
- MNIST weights: the authors state harmonic weights are closer to images of each digit class, and color weights below 0.01 yellow (Figure 4 right). Test accuracy is close for the two losses.
- ImageNet comparison: the authors state that in their implementation harmonic is slightly better than cross-entropy in the same implementation. Table 1 also lists cross-entropy Top-1 77.6%, Top-5 95.3% from [35], and supervised contrastive loss Top-1 78.7%, Top-5 94.3%; these are not this paper's training implementation.
Algorithmic tasks
- Representations: the authors state harmonic embeddings are more organized. In in-context learning, the harmonic model almost always explains 100% of variance with the first two principal components and recovers the two-dimensional grid; on the family-tree task only the Harmonic MLP recovers the tree. On equivalence classes both can cluster, and the authors state the standard representation is more elongated because cross-entropy has no incentive to compress irrelevant variation to 0.
- Modular addition case: in Figure 4, the standard MLP at weight decay 0 and 0.5 has first-two-PC EV of 26% and 58%; harmonic at weight decay 0 is 100%. The authors state the standard MLP without weight decay often fails to generalize, and with stronger decay exhibits grokking, whereas harmonic generalizes quickly and forms a circle. The main text also states the harmonic transformer's success rate at forming a circle is similar to the standard model, but the first-two-PC explained variance is usually higher.
- Data and grokking: in Figure 3(b) the authors state harmonic requires comparable or less data, and in-context learning generalizes almost immediately. In Figure 3(c) the authors state grokking is generally reduced, more clearly on modular addition and permutation composition. The text does not give epoch counts for each point. On the permutation task, the authors state the harmonic MLP repeatedly shows 6 clusters each containing 4 permutations, corresponding to a coset of a Klein four-subgroup or its conjugate; the harmonic transformer often forms 4 clusters, corresponding to cosets of a subgroup isomorphic to S3.
Geometry and probes on language models
- Parallelograms: 12 function-vector tasks; multi-token words use the last token, projected onto the first two principal components, sampling 10000 quadruples. The loss is the norm of the difference of the sum of the two end embeddings, divided by a scale factor. The authors state harmonic GPT has lower loss on every task (Figure 5). In the present-past plot, the authors state harmonic quadrilaterals are closer to rectangles, and the standard model's are flatter.
- Middle layers: Appendix C compares layer 6 outputs. The authors state harmonic has more spikes where parallelogram loss is near 0, along with heavier tails, and attribute the heavy tails to a power law. On capitalize data, the authors state harmonic makes upper- and lower-case words overlap on the first two principal components, whereas cross-entropy separates them (Figure 9).
- Probes: a 1-layer MLP with hidden width 16 reads the residual stream; Table 2 reports validation F1. SST2 layer 0 is 76.2±1.5% and 77.9±1.1%, CoLA layer 0 is 73.9±1.0% and 74.3±1.0%; SST2 layer 6 point estimates are both 79.9% (±1.3% and ±1.7%). CoLA layer 6 harmonic is 77.1±4.2%, lower than cross-entropy's 78.2±1.7%. The paper does not explain this difference.
Other ablations and analyses
- Component ablation (Figure 6, MLP): in in-context learning, HarMax alone or l2 logit alone reproduces full harmonic; modular addition requires both. The authors state only l2 distance with HarMax recovers both the grid and the circle.
- Exponent n swept from 1 to 10 (Appendix E): on the grid task n=1 has the highest explained variance. The authors consider the modular-addition MLP stable at n=1, and that the Transformer's explained variance and circular structure may improve with a slightly larger exponent.
- Appendix D compares contrastive loss and spherical loss; the caption of Figure 10 states harmonic loss reconstruction on MLP is better than the other rows. Figure 11 gives no numerical values.
- Appendix B enumerates the 28 left cosets and 28 right cosets of S4, selects the partition by silhouette score, then colors.
- Figure 3(a) is the mean of 20 seeds. The authors state harmonic representations are overall more compact, with fewer unexplained components.
What are some points that can be further explored?
The authors state that larger models still need to be tested; whether heavy tails are a feature or a defect remains inconclusive in the appendix.
Limitations and Future Directions Noted by the Authors
- Scale: Section 8 states that the scalability and applicability of these findings to larger models still needs study.
- Heavy tails: Appendix C states whether the heavy tails of harmonic loss are a feature or a defect remains to be understood.
Experimental Coverage
- Algorithmic experiments compare Standard/Harmonic MLP and Transformer, 4 models total, 5 synthetic tasks; appendix Figure 7 representation plots use 20 seeds.
- Grokking plots use 20 seeds and omit runs that do not reach 90% accuracy. The paper does not report the number of omitted runs, nor the number of samples in each algorithmic dataset.
- Vision experiments are MNIST single-layer network for 10 epochs, and ResNet-50 on ImageNet trained for 90 epochs. The paper does not report repetitions for GPT-2 and ImageNet.
- Language model is GPT-2 small (128M), trained on OpenWebText for 10k steps. Probes use residual stream layers 0 and 6 of SST2 and CoLA. The paper does not specify the number of repetitions for ± in Table 2.
- Ablations include loss components, n from 1 to 10, and contrastive and spherical losses in Appendix D. The paper does not report names of the 12 function-vector tasks other than present-past and capitalize, nor human agreement.
Summarize the main content of the paper
Harmonic loss replaces the cross-entropy final layer; the authors claim it mitigates grokking and makes class weights correspond to class centers.
Replacing the final-layer cross-entropy with harmonic loss, training networks the authors believe have more interpretable representations.
- Problem: The authors believe SoftMax cross-entropy pushes logits to infinity to make probabilities approach 1, and is sensitive to scale; this is related to poor interpretability, high data requirements, and grokking.
- Approach: Logits are replaced by the l2 distance between class vectors and representations, with probabilities normalized by the negative n-th power of distance. The algorithmic models use n=1, GPT-2 small uses about 28. The authors interpret weights as class centers.
- Algorithmic results: Four small models, five synthetic tasks. The authors claim harmonic models generalize with less data and exhibit less grokking. In the modular addition case, the harmonic MLP without weight decay has 100% EV for the first two principal components, while the standard MLP has 26% and 58% at weight decay 0 and 0.5 (Figure 4).
- Larger settings: MNIST test accuracy 92.49% vs. 92.50%. GPT-2 small trained on OpenWebText for 10k steps, validation loss 3.146 vs. 3.159; the authors claim lower parallelogram loss on 12 function-vector tasks, and note that the initial faster part is partly due to a larger learning rate. Their ImageNet implementation achieves 75.08% vs. 74.17% Top-1.
- Not everything is higher: CoLA layer 6 probe F1, harmonic 77.1±4.2%, cross-entropy 78.2±1.7%. The authors suggest the loss may be useful in low-data and interpretability-critical scenarios, and state that it needs testing on larger models.