Skip to main content

NWF for NLP: Transformer encoders (BERT, DistilBERT) with (z, sigma) output for incremental learning, semantic search, text classification.

Project description

nwf-nlp

PyPI version Python 3.9+ License: MIT

NWF for Natural Language Processing

nwf-nlp provides Transformer-based text encoders that produce semantic charges (z, sigma) for incremental learning, semantic search, and classification. Built on HuggingFace models (BERT, RoBERTa, DistilBERT) with a custom head for uncertainty estimation.

Features

  • TransformerEncoder — wrapper over HuggingFace transformers (DistilBERT, BERT, RoBERTa)
  • Output (z, sigma) — compatible with nwf-core Field and Mahalanobis search
  • Incremental learning — add new categories without retraining the full model
  • Semantic search — find documents by query using charge similarity
  • Batch encoding — GPU support, configurable batch size
  • Pooling — [CLS] or mean pooling

Installation

pip install nwf-core nwf-nlp

Requires: nwf-core>=0.2.3, torch, transformers, scikit-learn.


Quick Start

from nwf import Charge, Field
from nwf.nlp import TransformerEncoder

enc = TransformerEncoder("distilbert-base-uncased", latent_dim=64)
enc.fit(train_texts, epochs=3)
z, sigma = enc.encode("Some text")
charge = Charge(z=z, sigma=sigma)

field = Field()
for i, text in enumerate(texts):
    z, s = enc.encode(text)
    field.add(Charge(z=z, sigma=s), labels=[labels[i]], ids=[i])

API

TransformerEncoder

Parameter Description
model_name HuggingFace model: "distilbert-base-uncased", "bert-base-uncased", "roberta-base"
latent_dim Output dimension of z
pooling "cls" or "mean"
freeze_backbone If True, only train the head (faster)
max_length Max tokens (default 512)
Method Description
fit(texts, epochs, batch_size, lr) Train head on texts (unsupervised: Gaussian prior)
encode(texts, batch_size) Returns (z, sigma) as numpy arrays

Examples

Install with examples: pip install nwf-nlp[examples]

Script Description
20newsgroups.py Incremental text classification: 3 categories, add sci.med without retraining

Run:

python examples/20newsgroups.py --epochs 2 --k 5
python examples/20newsgroups.py --save results/nlp.png

Notebook: notebooks/20newsgroups.ipynb


Application areas (сферы применения)

Area Use case Components
Incremental text classification Add new categories without retraining TransformerEncoder, Field, k-NN
Semantic search Find documents by query in charge space encode(query), Field.search
Topic modeling Cluster documents by latent charges z, sigma from TransformerEncoder

License

MIT


nwf-nlp (Русский)

NWF для обработки естественного языка

nwf-nlp предоставляет Transformer-энкодеры для текста с выходом семантических зарядов (z, sigma) для инкрементального обучения, семантического поиска и классификации.

Компоненты

  • TransformerEncoder — обёртка над HuggingFace (DistilBERT, BERT, RoBERTa)
  • Выход (z, sigma) — совместим с Field и поиском по Махаланобису
  • Инкрементальность — добавление новых тем без переобучения
  • Семантический поиск — поиск документов по запросу

Установка

pip install nwf-core nwf-nlp

Пример

from nwf.nlp import TransformerEncoder
from nwf import Charge, Field

enc = TransformerEncoder("distilbert-base-uncased", latent_dim=64)
enc.fit(тексты, epochs=3)
z, sigma = enc.encode("Текст для кодирования")
field.add(Charge(z=z, sigma=sigma), labels=[метка])

Лицензия

MIT

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

nwf_nlp-0.1.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file nwf_nlp-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: nwf_nlp-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for nwf_nlp-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4f4ec2b9807d48480194f05c2a96fe43c3a31b06e3002de72ae07a4b475a820f
MD5 485d2282512faeab0ffa2e382306d1e8
BLAKE2b-256 1bc1c2a041b050ccd22680e922461dae1ce159ccce0d2e4800623ce79f5b2247

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