Abstract
Fine-tuning Large Language Models (LLMs) for recommendation systems using standard Language Modeling Loss (LML) is often suboptimal because it penalizes "hallucinated" items that may actually reflect true user preferences. We introduce Masked Softmax Loss (MSL), a novel objective that restricts loss computation to valid item tokens using a Trie tree implementation. To address the resulting gradient vanishing issue, we propose an Adaptive Temperature Strategy (ATS). Our approach achieves a 42% average improvement in NDCG@10 while reducing training time by up to 6.7%.
Introduction: The Problem with Standard Language Modeling
A common paradigm for building LLM-based recommenders involves fine-tuning the model on user interaction histories using the standard next-token prediction objective, Language Modeling Loss (LML). However, LML is fundamentally misaligned with the goals of a recommendation system.
First, LML treats all tokens in the vocabulary as negatives. If an LLM predicts a semantically relevant but non-existent item (e.g., "Iron Man 4" for a Marvel fan), LML heavily penalizes this "hallucination." In reality, such a prediction indicates the model has captured the user's semantic preferences; the item simply isn't in the catalog. Second, LML's gradient energy is primarily consumed by teaching the model to distinguish valid item names from arbitrary text, rather than ranking positive items over negative ones.
Methodology: Masked Softmax Loss (MSL)
To resolve these issues, we propose Masked Softmax Loss (MSL). MSL modifies the softmax operation to only consider tokens that constitute valid items within the system's catalog.
Trie Tree Implementation
To implement this efficiently, we utilize a prefix tree (Trie) containing all valid item descriptions. During training, the model traverses the Trie to identify and mask invalid tokens at each step of the sequence generation. This ensures the LLM focuses exclusively on differentiating between available items.
Adaptive Temperature Strategy (ATS)
Restricting the softmax denominator to a small subset of valid tokens can lead to gradient vanishing. As the probability of the target token quickly approaches unity, the gradient drops toward zero. We introduce an Adaptive Temperature Strategy (ATS) that dynamically calculates an optimal temperature scaling parameter ($\tau$) based on the average number of valid tokens in the dataset, ensuring stable training without manual tuning.
Evaluation
We evaluated MSL on four Amazon review datasets: Toys and Games, Books, Clothing, and Office Products. Data was preprocessed using a 5-core setting and a sliding window approach.
Key Results
- Ranking Accuracy: MSL achieved an average improvement of 42.24% in NDCG@10 over baselines, including standard LML and S-DPO.
- High Efficiency: By ignoring tens of thousands of invalid tokens during the forward and backward passes, MSL is 4.4% to 6.7% faster than standard LML.
- Stability: Unlike Direct Preference Optimization (DPO) methods, which require noisy negative sampling, MSL is stable and significantly faster to train.
Discussion and Insights
The failure of LML in recommendation tasks stems from its gradient distribution. By decomposing LML, we find that the gradient is dominated by the task of lifting valid items over invalid ones, which effectively "drowns out" the ranking signal that differentiates positive items from negative ones.
Furthermore, MSL provides a tighter upper bound for optimizing the NDCG metric than LML. While hallucinations are typically viewed as a flaw in generative AI, in recommendation, "semantic hallucinations" are valuable indicators of user intent that should not be penalized.
Limitations and Future Work
The primary engineering challenge for MSL is maintaining the Trie tree in highly dynamic environments where the item catalog changes frequently. Future work should also explore LLM architectures specifically designed for recommendation rather than adapting general-purpose generative models.
Summary updated