Skip to main content

Text Classification with Transformers

Project description

NLPIPES

Text Classification with Transformers


Table of Contents
  1. Overview
  2. How to Use
  3. References

Overview

NLPipes provides an easy way to use Transformers-based models for training, evaluation and inference on a diversity of text classification tasks, including:

  • Single-label classification: Assign one label to each text. A typical use case is sentiment analysis where one want to detect the overall sentiment polarity (e.g., positive, neutral, negative) in a review.
  • Multi-label classification [Not yet implemented]: Assign one or more label to each text from a list of possible labels. A typical use case is tag detection where one want to detect the multiple aspects mentionned in a review (e.g., #product_quality, #delivery_time, #price, ...).
  • Aspect-based classification [Not yet implemented]: Assign one label from a list of possible labels for each of a list of aspects. A typical use case is aspect based sentiment analysis where one want to detect each aspect mentionned in a review along his assocated sentiment polarity (e.g., #product_quality: neutral, #delivery_time: negative, #price: positive, ...).
  • Zero-shot classification [Not yet implemented]: Assign one or more label to each text from a list of possible labels without the requirement of an annotated training dataset.

Built with

NLPipes is built with TensorFlow and HuggingFace Transformers:

  • TensorFlow: An end-to-end open source deep learning framework
  • Transformers: An general-purpose open-sources library for transformers-based architectures

Installation

  1. Create a virtual environment
python3 -m venv nlpipesenv
source nlpipesenv/bin/activate
  1. Install the package
pip install --upgrade pip
pip install nlpipes

We recommend installing NLPipes on a machine with at least 1 GPU accelerator.

How To Use

NLPipes expose a the Model API that offers a simplesd abstraction to run several text classification tasks. The Model encapsulate most of the complex code from the library and save having to deal with the complexity of transformers based algorithms to build a text classifier.

Train a model

Give NLPipes a single label for each text as a target to train a single-label classifier.

from nlpipes import Model

reviews = ["text", "text", "text", ...]
sentiments = ["positive", "negative", "neutral", ...]
all_labels = ["negative", "neutral", "positive"]

model = Model(task='single-label-classification',
              name_or_path='bert-base-uncased',
              all_labels=all_labels)

model.train(reviews, sentiments)
 
model.save('./sentiment_detection_model')

Evaluate a model

Give NLPipes an evaluation metric and the label(s) for each text to evaluate the model.

from nlpipes import Model, Confusion_matrix

eval_reviews   = ["text", "text", "text", ...]
eval_sentiments = ["positive", "negative", "neutral", ...]
eval_metric = Confusion_matrix(num_labels=3)

model = Model('./sentiment_detection_model')

evaluation = model.evaluate(eval_reviews, eval_sentiments,
                            metric=eval_metric)
 
print('Model performance: ', evaluation)

Predict with a model

Give NLPipes new texts to predict the most relevant label(s).

from nlpipes import Model

new_reviews = ["text", "text", "text", ...]

model = Model('./sentiment_detection_model')

predictions = model.predict(new_reviews)

print("Reviews: ", new_reviews)
print('Predicted labels: ', predictions.label)
print('Confidence Score: ', predictions.scores)

References

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

nlpipes-0.1.4.tar.gz (33.2 kB view hashes)

Uploaded Source

Built Distribution

nlpipes-0.1.4-py3-none-any.whl (41.5 kB view hashes)

Uploaded Python 3

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