Abstract
Two things seem to be indisputable in the contemporary deep learning discourse:
- The categorical cross-entropy loss after softmax activation is the method of choice for classification.
- Training a CNN classifier from scratch on small datasets does not work well. In contrast to this, we show that the cosine loss function provides substantially better performance than cross-entropy on datasets with only a handful of samples per class. For example, the accuracy achieved on the CUB-200-2011 dataset without pre-training is by 30% higher than with the cross-entropy loss. Further experiments on other popular datasets confirm our findings. Moreover, we demonstrate that integrating prior knowledge in the form of class hierarchies is straightforward with the cosine loss and improves classification performance further.
Introduction
The "data hunger" of deep neural networks is a significant barrier to their application in niche domains. When training data is scarce—often defined as fewer than 100 samples per class—the standard practice is transfer learning: pre-training a model on a massive dataset like ImageNet and fine-tuning it on the target task. However, transfer learning introduces domain biases and can be restricted by licensing or technical mismatches.
While the community has largely accepted Categorical Cross-Entropy (CE) as the default loss function, this paper argues that CE is fundamentally ill-suited for small-data regimes. CE encourages models to push logit values toward infinity to minimize loss, leading to rapid overfitting when data is limited. We propose that simply replacing CE with Cosine Loss allows models to learn effectively from scratch on tiny datasets.
The Cosine Loss Approach
The standard classification pipeline uses a Softmax function followed by Cross-Entropy loss. In contrast, Cosine Loss focuses on the angular similarity between network outputs and target labels. The process involves:
- $L^2$-Normalization of the network's output features, mapping them onto a unit hypersphere.
- $L^2$-Normalization of target class labels (e.g., one-hot vectors).
- Maximizing Cosine Similarity between the normalized output and the target.
Mathematically, the loss is defined as $L_{cos}(x, y) = 1 - \cos(\theta)$, where $\theta$ is the angle between the prediction and the ground truth.
As shown in the loss landscapes above, Cross-Entropy (a) only reaches its optimum as logits approach infinity. On small datasets, models exploit this by memorizing noise. Cosine Loss (c) is bounded between 0 and 2, acting as a hyperparameter-free regularizer that forces the network to prioritize the direction of feature vectors over their magnitude, which often contains more semantic information.
Experimental Evaluation
The effectiveness of Cosine Loss was tested across various small datasets, including fine-grained visual tasks (CUB-200-2011, NAB, Stanford Cars), general scene recognition (MIT Indoor Scenes), and text classification (AG News).
The results demonstrate a significant performance gap in low-data regimes. On the CUB-200-2011 dataset, accuracy improved from 51.9% with CE to 67.6% with Cosine Loss—a 30% relative gain without pre-training. As the number of samples per class increases, the performance of CE eventually catches up, as seen in the CIFAR-100 results. Notably, the integration of semantic embeddings (dashed lines in the figure above) further boosts performance by leveraging class hierarchies to inform the model about inter-class relationships.
Analysis and Key Insights
- The "Infinity" Trap: CE's requirement for infinite logits leads to overfitting on small datasets. Cosine Loss avoids this by operating on a bounded scale.
- Magnitude as Noise: In high-dimensional spaces, vector magnitude often represents noise. By normalizing it away, Cosine Loss focuses the model on semantic direction.
- Semantic Integration: Because Cosine Loss measures vector similarity, it is trivial to replace one-hot targets with semantic embeddings derived from class hierarchies (like WordNet). This allows the model to know that a "Sparrow" is more similar to a "Finch" than to a "Car."
- Domain Agnostic: Gains were observed in both image and text classification, suggesting the benefit is a fundamental property of the loss function rather than a domain-specific trick.
Conclusion
While Cosine Loss does not yet outperform large-scale pre-training, it significantly narrows the gap for researchers working with limited data. For niche domains where transfer learning is impractical or prohibited, switching to Cosine Loss represents a simple, effective method to improve model robustness and accuracy when training from scratch.
Summary updated