What is fastText?
fastText is a library for efficient text classification and representation learning developed by Facebook's AI Research lab. Built on the foundations of Word2Vec, fastText extends the concept by incorporating subword information through character n-grams, making it particularly effective for handling out-of-vocabulary words and morphologically rich languages.
The library provides two main functionalities: learning word vector representations and text classification. fastText is designed for speed and simplicity, often achieving competitive results much faster than traditional deep learning approaches while requiring minimal computational resources and hyperparameter tuning.
fastText Performance Calculator
Training Memory: 100MB
Subword Features: 1,000,000
Throughput: 17MB/s
fastText Core Algorithms
Word Embeddings
Learn word representations using subword information and character n-grams.
• Character n-gram features
• Hierarchical softmax
• Negative sampling
• OOV word handling
Text Classification
Fast and accurate multi-class and multi-label text classification.
• Linear classifier (softmax)
• Multi-class/multi-label
• Hierarchical classification
• Real-time prediction
Subword Features
Character-level n-grams for handling morphology and rare words.
• Morphology awareness
• Rare word robustness
• Cross-lingual transfer
• Spelling variation handling
Optimization
Efficient training techniques for speed and scalability.
• Learning rate scheduling
• Multi-threading support
• Memory-efficient structures
• Quantization support
fastText Training Examples
Word Embeddings Training
Train word vectors with subword information for better handling of rare words.
import fasttext
# Train word embeddings on text file
model = fasttext.train_unsupervised(
input='text_corpus.txt',
model='skipgram', # or 'cbow'
dim=100, # vector dimensions
epoch=5, # training epochs
lr=0.05, # learning rate
minCount=5, # minimum word frequency
minn=3, # min character n-gram length
maxn=6, # max character n-gram length
thread=4 # number of threads
)
# Get word vector
vector = model.get_word_vector('hello')
print(f"Vector shape: {vector.shape}")
# Find similar words
similar_words = model.get_nearest_neighbors('king', k=10)
for score, word in similar_words:
print(f"{word}: {score:.4f}")
# Handle out-of-vocabulary words
oov_vector = model.get_word_vector('unknownword123')
print("OOV word vector computed from subwords")
Text Classification Training
Train fast and accurate text classifiers for multi-class problems.
# Prepare training data (format: __label__class_name text)
# Example file content:
# __label__positive This movie is great!
# __label__negative This movie is terrible.
# Train classification model
model = fasttext.train_supervised(
input='train.txt',
epoch=25,
lr=1.0,
wordNgrams=2, # use bigrams
dim=100, # vector dimensions
loss='softmax', # or 'hs' for hierarchical softmax
thread=4
)
# Test the model
test_results = model.test('test.txt')
print(f"Accuracy: {test_results[1]:.4f}")
print(f"Precision: {test_results[0]:.4f}")
# Make predictions
predictions = model.predict(['This is a great product!'], k=3)
for label, score in zip(predictions[0][0], predictions[1][0]):
print(f"{label}: {score:.4f}")
# Quantize model for smaller size
model.quantize(input='train.txt', retrain=True)
model.save_model('model_quantized.ftz')
Hyperparameter Tuning
Automatically find optimal hyperparameters for your classification task.
# Automatic hyperparameter tuning
model = fasttext.train_supervised(
input='train.txt',
autotuneValidationFile='validation.txt', # validation set for tuning
autotuneDuration=600, # tuning time in seconds
autotuneModelSize='2M' # target model size
)
# Manual hyperparameter search
import itertools
def grid_search():
learning_rates = [0.1, 0.5, 1.0]
epochs = [5, 10, 25]
word_ngrams = [1, 2, 3]
best_accuracy = 0
best_params = {}
for lr, epoch, ngram in itertools.product(learning_rates, epochs, word_ngrams):
model = fasttext.train_supervised(
input='train.txt',
lr=lr,
epoch=epoch,
wordNgrams=ngram,
dim=100,
verbose=0
)
results = model.test('validation.txt')
accuracy = results[1]
if accuracy > best_accuracy:
best_accuracy = accuracy
best_params = {'lr': lr, 'epoch': epoch, 'wordNgrams': ngram}
model = None # Free memory
return best_params, best_accuracy
# Run grid search
best_params, best_acc = grid_search()
print(f"Best params: {best_params}")
print(f"Best accuracy: {best_acc:.4f}")
Real-World fastText Implementations
Uses fastText for content classification and language detection across billions of posts.
- • Spam and abuse detection at scale
- • Multi-language content classification
- • Real-time content moderation
- • Processing 100+ languages daily
Airbnb
Employs fastText for search ranking and listing categorization across global markets.
- • Property description classification
- • Multi-language search understanding
- • Real-time query categorization
- • Supporting 60+ languages
Crisp
Leverages fastText for customer support automation and intent classification.
- • Customer query intent detection
- • Multi-language support ticket routing
- • Real-time sentiment analysis
- • Processing millions of messages
Zalando
Utilizes fastText for product categorization and recommendation systems.
- • Product description classification
- • Size and fit recommendations
- • Multi-language product matching
- • Fashion trend analysis
fastText Performance Optimization
Training Optimization
- • Use multiple threads for faster training
- • Adjust learning rate based on dataset size
- • Use hierarchical softmax for large vocabulary
- • Optimize n-gram range based on language
- • Use autotune for automatic hyperparameter optimization
- • Consider quantization for model compression
Inference Optimization
- • Use quantized models for faster prediction
- • Batch predictions when possible
- • Preprocess text consistently with training
- • Cache frequent predictions
- • Use appropriate k value for top-k predictions
- • Consider model ensemble for better accuracy
fastText Best Practices
✅ Do
- • Preprocess text consistently (lowercasing, tokenization)
- • Use validation set for hyperparameter tuning
- • Start with default parameters and tune incrementally
- • Use word n-grams for better context understanding
- • Consider subword features for morphologically rich languages
- • Use autotune feature for optimal hyperparameters
❌ Don't
- • Use fastText for tasks requiring deep context understanding
- • Ignore class imbalance in classification tasks
- • Over-tune hyperparameters without validation
- • Use too high dimensions without sufficient data
- • Forget to preprocess text before training/prediction
- • Mix different preprocessing between train/test