Skip to main content

A tool set for NLP.

Project description

Usage Sample ''''''''''''

.. code:: python

    import torch
    from sklearn.model_selection import train_test_split
    from nlpx.text_token import Tokenizer
    from nlpx.model.classifier import TextCNNClassifier
    from nlpx.model.wrapper import ClassModelWrapper
    from nlpx.dataset import TokenDataset, PaddingTokenCollator

    if __name__ == '__main__':
        classes = ['class1', 'class2', 'class3'...]
        texts = [[str],]
        labels = [0, 0, 1, 2, 1...]
        tokenizer = Tokenizer.from_texts(texts, min_freq=5)
        sent = 'I love you'
        tokens = tokenizer.encode(sent, max_length=6)
        # [101, 66, 88, 99, 102, 0]
        sent = tokenizer.decode(tokens)
        # ['<BOS>', 'I', 'love', 'you', '<EOS>', '<PAD>']

        tokens = tokenizer.batch_encode(texts, padding=False)
        X_train, X_test, y_train, y_test = train_test_split(tokens, labels, test_size=0.2)
        train_set = TokenDataset(X_train, y_train)
        val_set = TokenDataset(X_test, y_test)

        model = TextCNNClassifier(embed_dim=128, vocab_size=tokenizer.vocab_size, num_classes=len(classes))
        model_wrapper = ClassModelWrapper(model, classes=classes)
        model_wrapper.train(train_set, val_set, show_progress=True, collate_fn=PaddingTokenCollator(tokenizer.pad))

        result = model_wrapper.evaluate(val_set, collate_fn=PaddingTokenCollator(tokenizer.pad))
        # 0.953125

        test_inputs = torch.tensor(test_tokens, dtype=torch.long)
        result = model_wrapper.predict(test_inputs)
        # [0, 1]

        result = model_wrapper.predict_classes(test_inputs)
        # ['class1', 'class2']

        result = model_wrapper.predict_proba(test_inputs)
        # ([0, 1], array([0.99439645, 0.99190724], dtype=float32))

        result = model_wrapper.predict_classes_proba(test_inputs)
        # (['class1', 'class2'], array([0.99439645, 0.99190724], dtype=float32))

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

NLPX-1.6.2.tar.gz (34.6 kB view details)

Uploaded Source

File details

Details for the file NLPX-1.6.2.tar.gz.

File metadata

  • Download URL: NLPX-1.6.2.tar.gz
  • Upload date:
  • Size: 34.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for NLPX-1.6.2.tar.gz
Algorithm Hash digest
SHA256 508a450b7c985b0af7210178ede77f59a89c8612590bbf005aaf05298b9bc624
MD5 dfe1a6cd7dc87f0820148f1a87dd9c1f
BLAKE2b-256 e73b035bd006af407b9f6a69cb1d8a59c6e263aeb80c9e6d669baa5c94e5da73

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page