Skip to main content

A text classification toolkit to easily build, train and evaluate deep learning text classifiers using PyTorch.

Project description

torchTextClassifiers

Documentation

A unified, extensible framework for text classification with categorical variables built on PyTorch and PyTorch Lightning.

🚀 Features

  • Complex input support: Handle text data alongside categorical variables seamlessly.
    • ValueEncoder: Pass raw string categorical values and labels directly — no manual integer encoding required. Build a ValueEncoder from DictEncoder or sklearn LabelEncoder instances once, and the wrapper handles encoding at train time and label decoding after prediction automatically.
  • Unified yet highly customizable:
    • Use any tokenizer from HuggingFace or the original fastText's ngram tokenizer.
    • Text embedding is split into two composable stages: TokenEmbedder (token → per-token vectors, with optional self-attention) and SentenceEmbedder (aggregation: mean / first / last / label attention). Combine them with CategoricalVariableNet and ClassificationHead — all are torch.nn.Module.
    • Two architecture paths: use ModelConfig + the torchTextClassifiers constructor for the standard TextClassificationModel (zero boilerplate), or build any nn.Module you like and pass it to torchTextClassifiers.from_model() for full control. The contrib sub-package ships ready-made custom architectures (e.g. MultiLevelTextClassificationModel for multi-task classification) as reference implementations.
  • Multiclass / multilabel classification support: Support for both multiclass (only one label is true) and multi-label (several labels can be true) classification tasks.
  • PyTorch Lightning: Automated training with callbacks, early stopping, and logging
  • Easy experimentation: Simple API for training, evaluating, and predicting with minimal code:
    • The torchTextClassifiers wrapper class orchestrates the tokenizer and the model for you
  • Explainability:
    • Captum integration: gradient-based token attribution via integrated gradients (explain_with_captum=True).
    • Label attention: class-specific cross-attention that produces one sentence embedding per class, enabling token-level explanations for each label (explain_with_label_attention=True). Enable it by setting n_heads_label_attention in ModelConfig.

📦 Installation

# Clone the repository
git clone https://github.com/InseeFrLab/torchTextClassifiers.git
cd torchTextClassifiers

# Install with uv (recommended)
uv sync

# Or install with pip
pip install -e .

📖 Documentation

Full documentation is available at: https://inseefrlab.github.io/torchTextClassifiers/ The documentation includes:

  • Getting Started: Installation and quick start guide
  • Architecture: Understanding the 3-layer design
  • Tutorials: Step-by-step guides for different use cases
  • API Reference: Complete API documentation

📝 Usage

Checkout the notebook for a quick start.

📚 Examples

See the examples/ directory for:

  • Basic text classification
  • Multi-class classification
  • Mixed features (text + categorical)
  • Advanced training configurations
  • Prediction and explainability
  • Multi-level classification — custom architecture via from_model and contrib

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

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

torchtextclassifiers-2.0.0.tar.gz (37.6 kB view details)

Uploaded Source

Built Distribution

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

torchtextclassifiers-2.0.0-py3-none-any.whl (47.7 kB view details)

Uploaded Python 3

File details

Details for the file torchtextclassifiers-2.0.0.tar.gz.

File metadata

  • Download URL: torchtextclassifiers-2.0.0.tar.gz
  • Upload date:
  • Size: 37.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for torchtextclassifiers-2.0.0.tar.gz
Algorithm Hash digest
SHA256 43a48c3cecd4cd75aa4e2700dce5a7a8338ea8c0b58b1f5059c680f750e9f314
MD5 d19b10c4e4bb4f4767ece46aac921461
BLAKE2b-256 ee5374b1251f016f1cdb9f995e3b219d94a93a50b7db5c1d08968254f056068a

See more details on using hashes here.

File details

Details for the file torchtextclassifiers-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: torchtextclassifiers-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 47.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for torchtextclassifiers-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dd2f34db2f232ae3c0e14a08523612b156e298ab5f94922613851b8ada0560e6
MD5 2fc38c01e86e000c11b60f28d15059a8
BLAKE2b-256 dcaa393555cb826afa01d91d8ac0329fda594caeea9744b8065f2aa93267292a

See more details on using hashes here.

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