Skip to main content

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.

Metadata

Release files for torchtextclassifiers 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torchtextclassifiers 2.0.0
File Size Uploaded
torchtextclassifiers-2.0.0.tar.gz 37.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for torchtextclassifiers 2.0.0
File Interpreter ABI Platform
torchtextclassifiers-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 85.3 kB

Release files / torchtextclassifiers-2.0.0.tar.gz

Download URL torchtextclassifiers-2.0.0.tar.gz
Size 37.6 kB
Tags Source
SHA-256 checksum
How to use checksums
43a48c3cecd4cd75aa4e2700dce5a7a8338ea8c0b58b1f5059c680f750e9f314
BLAKE2b-256 checksum
How to use checksums
ee5374b1251f016f1cdb9f995e3b219d94a93a50b7db5c1d08968254f056068a
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release files / torchtextclassifiers-2.0.0-py3-none-any.whl

Download URL torchtextclassifiers-2.0.0-py3-none-any.whl
Size 47.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dd2f34db2f232ae3c0e14a08523612b156e298ab5f94922613851b8ada0560e6
BLAKE2b-256 checksum
How to use checksums
dcaa393555cb826afa01d91d8ac0329fda594caeea9744b8065f2aa93267292a
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page