An implementation of the https://github.com/facebookresearch/fastText supervised learning algorithm for text classification using Pytorch.
This project has been archived.
The maintainers of this project have marked this project as archived. No new releases are expected.
Project description
torchFastText : Efficient text classification with PyTorch
A flexible PyTorch implementation of FastText for text classification with support for categorical features.
⚠️ This repository is no longer maintained.
It has evolved into a newer, actively maintained project: torchTextClassifiers - that aims at being a more general, unified framework and a toolkit for text classification in PyTorch.
👉 Please use the updated version here: https://github.com/InseeFrLab/torchTextClassifiers
Features
- Supports text classification with FastText architecture
- Handles both text and categorical features
- N-gram tokenization
- Flexible optimizer and scheduler options
- GPU and CPU support
- Model checkpointing and early stopping
- Prediction and model explanation capabilities
Installation
pip install torchFastText
Key Components
build(): Constructs the FastText model architecturetrain(): Trains the model with built-in callbacks and loggingpredict(): Generates class predictionspredict_and_explain(): Provides predictions with feature attributions
Subpackages
preprocess: To preprocess text input, usingnltkandunidecodelibraries.explainability: Simple methods to visualize feature attributions at word and letter levels, usingcaptumlibrary.
Run pip install torchFastText[preprocess] or pip install torchFastText[explainability] to download these optional dependencies.
Quick Start
from torchFastText import torchFastText
# Initialize the model
model = torchFastText(
num_tokens=1000000,
embedding_dim=100,
min_count=5,
min_n=3,
max_n=6,
len_word_ngrams=True,
sparse=True
)
# Train the model
model.train(
X_train=train_data,
y_train=train_labels,
X_val=val_data,
y_val=val_labels,
num_epochs=10,
batch_size=64,
lr=4e-3
)
# Make predictions
predictions = model.predict(test_data)
where train_data is an array of size $(N,d)$, having the text in string format in the first column, the other columns containing tokenized categorical variables in int format.
Please make sure y_train contains at least one time each possible label.
Dependencies
- PyTorch Lightning
- NumPy
Categorical features
If any, each categorical feature $i$ is associated to an embedding matrix of size (number of unique values, embedding dimension) where the latter is a hyperparameter (categorical_embedding_dims) – chosen by the user – that can take three types of values:
None: same embedding dimension as the token embedding matrix. The categorical embeddings are then summed to the sentence-level embedding (which itself is an averaging of the token embeddings). See Figure 1.int: the categorical embeddings have all the same embedding dimensions, they are averaged and the resulting vector is concatenated to the sentence-level embedding (the last linear layer has an adapted input size). See Figure 2.list: the categorical embeddings have different embedding dimensions, all of them are concatenated without aggregation to the sentence-level embedding (the last linear layer has an adapted input size). See Figure 3.
Default is None.
Figure 1: The 'sum' architecture
Figure 2: The 'average and concatenate' architecture
Figure 3: The 'concatenate all' architecture
Documentation
For detailed usage and examples, please refer to the example notebook (notebooks/example.ipynb). Use pip install -r requirements.txt after cloning the repository to install the necessary dependencies (some are specific to the notebook).
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT
References
Inspired by the original FastText paper [1] and implementation.
[1] A. Joulin, E. Grave, P. Bojanowski, T. Mikolov, Bag of Tricks for Efficient Text Classification.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torchfasttext-0.1.0.tar.gz.
File metadata
- Download URL: torchfasttext-0.1.0.tar.gz
- Upload date:
- Size: 26.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
21d923d87746f4f9c74a8418e003faee677c34643ed4c556598f836d234c2357
|
|
| MD5 |
dc198bb9e78654d6fd07a2efac9edc80
|
|
| BLAKE2b-256 |
19a8d8d1515280f899ed7f73836c9348911d99c54889db6a9bf69163f45a3c36
|
Provenance
The following attestation bundles were made for torchfasttext-0.1.0.tar.gz:
Publisher:
python-publish.yml on InseeFrLab/torch-fastText
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchfasttext-0.1.0.tar.gz -
Subject digest:
21d923d87746f4f9c74a8418e003faee677c34643ed4c556598f836d234c2357 - Sigstore transparency entry: 726612398
- Sigstore integration time:
-
Permalink:
InseeFrLab/torch-fastText@7c4c827eaa0aacf2c43c951e5d35a52fd3ed2c0d -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/InseeFrLab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@7c4c827eaa0aacf2c43c951e5d35a52fd3ed2c0d -
Trigger Event:
release
-
Statement type:
File details
Details for the file torchfasttext-0.1.0-py3-none-any.whl.
File metadata
- Download URL: torchfasttext-0.1.0-py3-none-any.whl
- Upload date:
- Size: 30.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b398dcff29337495ee1ccb1cd9ec6c611d32321a016f211fdaffdffabe98c8f7
|
|
| MD5 |
d782c69f532d0517cdaee7a61a0a7916
|
|
| BLAKE2b-256 |
d06d5555f85a433f02ac2879440e1a5306a307437ecf2e6f9d6068533c681386
|
Provenance
The following attestation bundles were made for torchfasttext-0.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on InseeFrLab/torch-fastText
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchfasttext-0.1.0-py3-none-any.whl -
Subject digest:
b398dcff29337495ee1ccb1cd9ec6c611d32321a016f211fdaffdffabe98c8f7 - Sigstore transparency entry: 726612408
- Sigstore integration time:
-
Permalink:
InseeFrLab/torch-fastText@7c4c827eaa0aacf2c43c951e5d35a52fd3ed2c0d -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/InseeFrLab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@7c4c827eaa0aacf2c43c951e5d35a52fd3ed2c0d -
Trigger Event:
release
-
Statement type: