Registry / ai-ml / fasttext

fasttext

JSON →
library0.9.3pypypiunverified

fastText is a library for efficient learning of word representations and sentence classification. Developed by Facebook AI Research, it's particularly good for large-scale text processing tasks. The current version is 0.9.3, with releases focusing on new features, performance, and API stability rather than a fixed cadence.

pip install fasttext
INSTALL
IMPORT
SIG · FASTTEXT
F
fasttext
ai-mlpythonv0.9.3
Install
Import
Disk
Pass rate
0/ 10
Env Coverage0 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v? · pip install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
build_error
glibc
py 3.103.95 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fasttext
import fasttext
import fastText
Prior to v0.9.1, the official GitHub module was 'fastText', while PyPI had an unofficial 'fasttext'. They merged, and the official import is now 'fasttext'.

This quickstart demonstrates how to train a basic supervised text classification model and make predictions using fastText. The training data must be in the specific fastText format with `__label__` prefixes.

import fasttext import os # Create a dummy training file for demonstration training_data_path = 'train.txt' with open(training_data_path, 'w') as f: f.write('__label__positive This is a good movie.\n') f.write('__label__negative This movie was terrible.\n') f.write('__label__positive I love this film.\n') # Train a supervised model model = fasttext.train_supervised(input=training_data_path) # Predict a label text_to_predict = 'This is an excellent film.' predictions = model.predict(text_to_predict) print(f"Text: '{text_to_predict}'") print(f"Prediction: {predictions[0][0]}, Probability: {predictions[1][0]:.4f}") # Clean up dummy file os.remove(training_data_path)
fasttext --version
Debug
Known issues
breakingVersion 0.9.1 merged the previously separate official 'fastText' (from GitHub) and unofficial 'fasttext' (from PyPI) Python modules. This involved significant API changes, especially for users who were previously installing directly from GitHub.
fix
Ensure you are importing `fasttext` (lowercase) and refer to the v0.9.1+ documentation for updated API calls, particularly `fasttext.load_model()` and training parameters. Uninstall any old `fastText` installations before reinstalling `fasttext`.
affects: <0.9.1
gotchaInstallation on some operating systems (e.g., Windows, or macOS without specific tools) can fail due to C++ compilation requirements. fastText is a C++ library with Python bindings.
fix
Ensure you have a C++ compiler (e.g., GCC, Clang for Linux/macOS, MSVC for Windows) installed and correctly configured in your PATH. On Windows, this often means installing 'Build Tools for Visual Studio'.
affects: All versions
gotchaThe `fasttext.load_model()` function expects a `.bin` model file, which contains both word vectors and classification information. It cannot directly load standalone `.vec` (vector) files.
fix
Always save your trained fastText models using `model.save_model('model.bin')` and load them with `fasttext.load_model('model.bin')`. If you only have `.vec` files, you'll need to retrain or find the original `.bin` model.
affects: All versions
gotchaTraining data for supervised classification (e.g., `train_supervised`) must adhere to a specific format: each line should contain the label prefixed with `__label__`, followed by the text, for example: `__label__positive This is a great product.`
fix
Pre-process your training data to match the fastText input format. Each line is a document, and each document starts with its label(s) prefixed by `__label__`.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'fasttext'
This error typically occurs when the `fasttext` Python package is not installed correctly in the active Python environment, or a local file named `fasttext.py` is shadowing the installed library.
fix
Ensure fastText is installed using `pip install fasttext`. If you have a local file named `fasttext.py`, rename it to avoid conflicts. If using Anaconda, try `conda install -c conda-forge fasttext`.
AttributeError: module 'fasttext' has no attribute 'load_model'
This usually happens when attempting to call a method like `load_model` (or `train_supervised`, `predict`, etc.) directly on the `fasttext` module, but the method is either not directly exposed this way in your installed version or a local `fasttext.py` file is preventing the correct module from being loaded. It can also occur if there's confusion between the official `fasttext` library and Gensim's `FastText` wrapper.
fix
Ensure you are using the correct FastText API. For the official fastText Python binding, methods like `load_model` are directly available after `import fasttext`. If you are using Gensim's implementation, you would typically import `from gensim.models import FastText` and the model object would have methods like `wv.most_similar()`. Also, check for any local file named `fasttext.py` that might be overriding the package import. A correct usage example would be `import fasttext; model = fasttext.load_model('model.bin')`.
ValueError: Invalid model file. Please download the updated model from www.fasttext.cc.
This error indicates that the model file you are trying to load is either corrupted, an older version that is incompatible with your current `fasttext` library, or not a valid fastText model file (.bin or .ftz format).
fix
Download the latest pre-trained models from the official fastText website (www.fasttext.cc) or ensure that your custom-trained model was saved correctly and is not corrupted. If you trained the model yourself, verify the training and saving process.
ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected X from C header, got Y from PyObject.
This error typically arises due to an incompatibility between the installed `fasttext` library (which has C++ extensions) and a recently updated NumPy version, especially with the release of NumPy 2.0. The binary extensions of `fasttext` were compiled against an older NumPy API.
fix
Downgrade your NumPy version to a compatible one (e.g., `pip install numpy<2`) or try installing a `fasttext` wheel specifically built for newer NumPy versions if available. If building from source, ensure you have a compatible compiler and NumPy version when compiling fastText. Some users have found success with alternative `fasttext` wheels for Windows that address NumPy compatibility.
ValueError: <filename> cannot be opened for training!
This error occurs when the `fasttext.train_supervised()` or `fasttext.train_unsupervised()` function cannot locate or access the specified input training file. This is often due to an incorrect file path, the file not existing, or insufficient permissions.
fix
Verify that the input file path passed to the training function is correct and that the file exists at that location. Ensure that the Python process has the necessary read permissions for the file. For example, `model = fasttext.train_supervised(input='path/to/your/data.txt')`.
Upgrade
Version history
0.9.3latest on PyPI · released Jun 12, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
40 hits · last 30 days
node
36
OpenAI (training)
1
Resources