FastText Tutorial for Text Classification and Word Vectors

This FastText tutorial explains how to use fastText for natural language processing tasks such as text classification, sentiment analysis, and word representation learning. fastText is an open-source, lightweight library originally released by Facebook AI Research for learning text classifiers and word vectors on standard hardware.

fastText is useful when you need a fast baseline model, a small classifier, or word vectors that handle rare and misspelled words better than plain word-level embeddings. Transformer models are often stronger for context-heavy tasks, but fastText remains practical for CPU-friendly NLP workflows.

What fastText is used for in NLP projects

fastText can train supervised classifiers from labeled text and can learn word vectors from unlabeled text. In supervised mode, it predicts labels for a sentence or document. In unsupervised mode, it learns word representations using CBOW or skip-gram training.

A key feature of fastText is subword information. A word can be represented with character n-grams, not only as a complete token. This helps fastText build useful vectors for rare words, spelling variations, prefixes, suffixes, and words that were not seen exactly during training.

The current official GitHub repository describes fastText as MIT-licensed. If you distribute the software or pre-trained model files, review the exact license terms for the source code and model files you use.

FastText Tutorial - www.tutorialkart.com

Build and install FastText for command line or Python

fastText can be used from the command line or through the Python module. The command-line tool is convenient for training, testing, prediction, quantization, and vector export. The Python module is useful when fastText is part of a larger data-processing or machine-learning pipeline.

</>
Copy
pip install fasttext

Build FastText [icon name=”link” class=”” unprefixed_class=””]  – FastText Tutorial to make a build from source,  on Linux Distribution(like Ubuntu, CentOS, etc.) or MacOS

fastText input format with __label__ prefixes

For supervised text classification, fastText expects one training example per line. Each line starts with one or more labels, and every label uses the __label__ prefix. The remaining text on the line is the document or sentence used for training.

</>
Copy
__label__sports Brazil won the match with two late goals.
__label__business The company reported stronger quarterly revenue.
__label__technology The new phone includes a faster processor.
  • Use the same label format in training, validation, and test files.
  • Use UTF-8 text, especially for multilingual datasets.
  • Split data into training and validation sets before tuning parameters.
  • Remove duplicates that can leak from training into validation.

fastText commands and capabilities for NLP tasks

FastText library provides following capabilities [ FastText command_name is provide in the bracket] through its tools.

  • Training Supervised Classifier [supervised] – trains a model for text classification.
  • Testing a Classifier [test] – evaluates a model with precision and recall metrics.
  • Predictions [predict] – predicts labels for new text.
  • Predictions with Probabilities [predict-prob] – returns labels with probability scores.
  • Quantization [quantize] – reduces supervised model size for deployment.
  • Training SkipGram Model [skipgram] – learns word vectors using skip-gram training.
  • Training CBOW Model [cbow] – learns word vectors using Continuous Bag Of Words.
  • Print Word Vectors [print-word-vectors] – prints vectors for words.
  • Print Sentence Vectors [print-sentence-vectors] – prints vectors for sentences or paragraphs.
  • Query Nearest Neighbors [nn] – finds similar words in the vector space.
  • Query for Analogies [analogies] – explores word-vector relationships.

Text classification with fastText supervised models

Text Classification or Document Classification is an NLP (Natural Language Processing) task of predicting which category a given text belongs to. Sentiment analysis, email spam detection, support-ticket routing, and news-topic classification are common examples.

supervised – to train a supervised model

</>
Copy
./fasttext supervised -input train.txt -output model

Train and Test Supervised Text Classifier using fasttext [icon name=”link” class=”” unprefixed_class=””] – Fasttext Tutorial to train a supervised text classifier using labelled data and test the generated model for accuracy and performance numbers.

test – to test a model

</>
Copy
./fasttext test model.bin test.txt
N   1000
P@1 0.92
R@1 0.92

The test command reports the number of examples, precision at one, and recall at one. For prediction, use predict when you need only labels and predict-prob when you also need probability scores.

</>
Copy
echo "the team scored in the final minute" | ./fasttext predict-prob model.bin - 3

In Python, the same workflow can be handled with train_supervised(), test(), predict(), and save_model().

</>
Copy
import fasttext

model = fasttext.train_supervised(input="train.txt")
print(model.test("test.txt"))

labels, probabilities = model.predict("the team scored in the final minute", k=3)
print(labels)
print(probabilities)

model.save_model("model.bin")

quantize – to reduce the memory usage. Quantization can reduce supervised model size, but always compare the compressed model with the original model before deployment.

Word representations learning with fastText subword vectors

Vector representation of words, in a language, captures relationships such as similarity and analogy. fastText learns word vectors with unsupervised techniques such as CBOW and skip-gram, and its subword modeling helps with rare words and morphology.

Learn Word Representations in FastText [icon name=”link” class=”” unprefixed_class=””] using Unsupervised Learning techniques – CBOW (Continuous Bag Of Words) and SkipGram.

</>
Copy
./fasttext skipgram -input corpus.txt -output vectors
./fasttext cbow -input corpus.txt -output vectors_cbow

fastText vs Word2Vec and transformer models

Word2Vec generally learns a vector for each complete word token. fastText can use character n-grams, so it can build representations from subword pieces. This helps fastText handle misspellings, suffixes, prefixes, and unseen words better than a simple word-level model.

Transformer models learn contextual representations, so the vector for a word can change depending on the sentence. They are often stronger for advanced NLP tasks, but fastText is still useful when you need a small, fast, CPU-friendly classifier or static word vectors.

fastText official documentation and pre-trained vectors

FastText tutorial FAQs

What is the difference between fastText and Word2Vec?

Word2Vec usually learns one vector for each word token. fastText represents words with character n-grams, so it can create vectors for rare, misspelled, and unseen words by using subword pieces.

Is fastText pre-trained?

The fastText library can train your own models, and the official fastText site also provides pre-trained word vectors for many languages.

Can fastText be used for sentiment analysis?

Yes. Sentiment analysis is a supervised text classification task, so fastText can be trained with labeled examples such as __label__positive and __label__negative.

Are fastText embeddings obsolete because transformer models are common?

No. Transformer models are often better for context-sensitive tasks, but fastText remains useful for lightweight classifiers, CPU deployment, quick baselines, static word vectors, and projects where model size matters.

What does the __label__ prefix mean in fastText?

The __label__ prefix tells fastText that a token is a class label rather than a normal word in the document.

FastText tutorial editorial QA checklist before publishing

  • Check that every supervised example uses the __label__ prefix correctly.
  • Confirm that command-line examples use language-bash and output-only examples use the output class.
  • Verify that text classification sections explain training, testing, prediction, and probabilities.
  • Confirm that CBOW, skip-gram, and subword n-grams are not confused with contextual transformer embeddings.
  • Make sure the existing internal links to Build FastText, supervised classification, and word representation learning remain unchanged.