Skip to main content

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.

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 architecture
  • train(): Trains the model with built-in callbacks and logging
  • predict(): Generates class predictions
  • predict_and_explain(): Provides predictions with feature attributions

Subpackages

  • preprocess: To preprocess text input, using nltk and unidecode libraries.
  • explainability: Simple methods to visualize feature attributions at word and letter levels, using captumlibrary.

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_buckets=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
)
# 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

Documentation

For detailed usage and examples, please refer to the experiments notebook. 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

@InProceedings{joulin2017bag,
  title={Bag of Tricks for Efficient Text Classification},
  author={Joulin, Armand and Grave, Edouard and Bojanowski, Piotr and Mikolov, Tomas},
  booktitle={Proceedings of the 15th Conference of the European Chapter of the Association for Computational Linguistics: Volume 2, Short Papers},
  month={April},
  year={2017},
  publisher={Association for Computational Linguistics},
  pages={427--431},
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchfasttext-0.0.1.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torchfasttext-0.0.1-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

Details for the file torchfasttext-0.0.1.tar.gz.

File metadata

  • Download URL: torchfasttext-0.0.1.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for torchfasttext-0.0.1.tar.gz
Algorithm Hash digest
SHA256 29e5e9a17b9ddcd435b5c9544836f179a938eebf43c34a73b4063c994c710b0e
MD5 d528a897ec485d407d1911e33c1c3a21
BLAKE2b-256 0876fc1226c90eb13963fbbf433d7399bbc3a973b82639ca3b6d8688e7a410a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfasttext-0.0.1.tar.gz:

Publisher: python-publish.yml on InseeFrLab/torch-fastText

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torchfasttext-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: torchfasttext-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 23.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for torchfasttext-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ba5448ad518ed8bb377ecf6dbd3aa85cd18205e683e43b07fd473998019332b9
MD5 22d763920cab3439ef1f9265ef6d51cb
BLAKE2b-256 47b659df117d13f7bae292de5418f783a6aba4165951487f46595f114375db35

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchfasttext-0.0.1-py3-none-any.whl:

Publisher: python-publish.yml on InseeFrLab/torch-fastText

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page