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

Example: 20 Newsgroups

Classification and incremental category addition.

cd examples && python 20newsgroups.py
  • Loads 20 Newsgroups (3 categories)
  • Trains TransformerEncoder head (2 epochs)
  • k-NN classification in charge space
  • Adds new category (sci.med) without retraining

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.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nwf_nlp-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 53dacff9223ab5a6125d71e577419e37b101801296d9a5082d05da0aed5aa639
MD5 d695fe22596b95e9c9a0d4b5a94595a0
BLAKE2b-256 ebf8bfa699b8bf6f866c0bc8d2d7776d765ce04724ca1723bcb7080902e976a6

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