Feature hashing for large scale multitask learning

Kilian Q. Weinberger, Anirban Dasgupta, John Langford, Alexander J. Smola, Josh Attenberg

Introduction

In modern machine learning, particularly with text data, models often must process millions of unique features, such as words or n-grams. Traditional methods for handling this scale become prohibitively expensive in two primary ways:

  1. Memory: Storing a weight for every possible word for every single user is practically impossible. In a multitask setting like personalized spam filtering with 400,000 users and 40 million possible words, a naive model would require 16 trillion parameters.
  2. Computation: Standard dimensionality reduction techniques, like Random Projections, often require multiplying data by large, dense matrices. This destroys the inherent "sparsity" of text data (the fact that most emails only contain a few dozen unique words), which is crucial for fast processing.

This paper provides the theoretical foundation and a massive-scale demonstration of the "hashing trick"—a method for compressing high-dimensional data into a small, fixed-size memory footprint. The authors prove that Feature Hashing is not just a heuristic hack, but a mathematically sound way to preserve distances between data points while keeping memory usage exceptionally low.

The Hashing Trick and Multitask Learning

The core technique is the Hashing Trick. Instead of maintaining a massive dictionary of words, the system uses a hash function $h(word)$ to determine the index of that word in a fixed-size array.

The authors introduce two key refinements to make this work at scale:

  • Signed Hashing: They employ a second hash function $\xi(word)$ that randomly outputs $+1$ or $-1$. When multiple words inevitably collide at the same index, their values are multiplied by this sign before being summed. This ensures the resulting estimate remains unbiased, as errors from collisions tend to cancel each other out rather than accumulating.
  • Multitask Hashing: To handle multiple users simultaneously, the system concatenates the user ID with the word (e.g., hash("USER123_viagra")). This grants every user their own "virtual" feature space, all of which are folded into the same physical memory array.
Feature hashing pipeline showing global and personalized features mapped to a sparse vector

Theoretically, the authors provide exponential tail bounds proving that the "distortion" caused by hashing is very small with high probability, provided the input vectors aren't dominated by a single massive feature. Because random subspaces in high dimensions are "almost" orthogonal, thousands of different user models can be jammed into the same hash table with negligible interference.

Real-World Application: Personalized Spam Filtering

The authors evaluated their approach on a massive proprietary email spam dataset containing 3.2 million emails from 433,167 users. The raw feature space consisted of 40 million unique words.

By utilizing the hashing trick, they successfully modeled a problem that theoretically had 16 trillion dimensions using a hash table indexed by only 22 bits (roughly 4 million entries). Furthermore, hashing preserves data sparsity, unlike other projection methods, which is critical for high-speed online learning.

Graph showing a 30% reduction in spam miss-rate for the personalized hashed model at 22 bits

Key Results

  • 30% Spam Reduction: As shown in the results above, using personalized hashed features reduced the amount of uncaught spam by 30% compared to a state-of-the-art global classifier.
  • "Free" Personalization: Even users with no training data saw improvements. The personalized components of "active" users absorbed the idiosyncratic noise of those users, leaving the "global" part of the model cleaner and more representative of general spam trends.
  • Rapid Convergence: The "global-hashed" model converged to the performance of an unhashed baseline very quickly (around 22 bits), demonstrating that the distortion from hashing vanishes long before memory limits become an issue.

Limitations

While highly effective, the approach has limitations. The theoretical bounds depend on the ratio between the maximum value of a single feature and the total length of the vector. If one feature is disproportionately large, collisions become more damaging (though "multiple hashing" can mitigate this). Additionally, in extremely constrained memory environments, collisions could still degrade performance for specific, rare features.

Summary updated

BibTeX
@inproceedings{weinberger2009feature,
  author = {Kilian Q. Weinberger and Anirban Dasgupta and John Langford and Alexander J. Smola and Josh Attenberg},
  title = {{Feature hashing for large scale multitask learning}},
  booktitle = {International Conference on Machine Learning},
  year = {2009},
  doi = {10.1145/1553374.1553516},
}
Figure