A text classification toolkit to easily build, train and evaluate deep learning text classifiers using PyTorch.
Project description
torchTextClassifiers
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
ValueEncoderfromDictEncoderor sklearnLabelEncoderinstances once, and the wrapper handles encoding at train time and label decoding after prediction automatically.
- ValueEncoder: Pass raw string categorical values and labels directly — no manual integer encoding required. Build a
- 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) andSentenceEmbedder(aggregation: mean / first / last / label attention). Combine them withCategoricalVariableNetandClassificationHead— all aretorch.nn.Module. - Two architecture paths: use
ModelConfig+ thetorchTextClassifiersconstructor for the standardTextClassificationModel(zero boilerplate), or build anynn.Moduleyou like and pass it totorchTextClassifiers.from_model()for full control. Thecontribsub-package ships ready-made custom architectures (e.g.MultiLevelTextClassificationModelfor 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
torchTextClassifierswrapper class orchestrates the tokenizer and the model for you
- The
- 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 settingn_heads_label_attentioninModelConfig.
- Captum integration: gradient-based token attribution via integrated gradients (
📦 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_modelandcontrib
📄 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
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43a48c3cecd4cd75aa4e2700dce5a7a8338ea8c0b58b1f5059c680f750e9f314
|
|
| MD5 |
d19b10c4e4bb4f4767ece46aac921461
|
|
| BLAKE2b-256 |
ee5374b1251f016f1cdb9f995e3b219d94a93a50b7db5c1d08968254f056068a
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd2f34db2f232ae3c0e14a08523612b156e298ab5f94922613851b8ada0560e6
|
|
| MD5 |
2fc38c01e86e000c11b60f28d15059a8
|
|
| BLAKE2b-256 |
dcaa393555cb826afa01d91d8ac0329fda594caeea9744b8065f2aa93267292a
|