Skip to main content

BERT Classifier for text classification

Project description

DataBERT Classifier for Dataset Identification

This package provides a comprehensive toolset for training and deploying BERT-based classifiers for text classification tasks, ranging from binary to multiclass problems. Leveraging the power of the transformers library, it simplifies the process of fine-tuning pre-trained BERT models and offers functionalities to evaluate, save, and deploy these models effectively.

Features

  • Easy-to-use classes for training and using BERT models for text classification.
  • Preprocessing and tokenization tailored for BERT's requirements.
  • Calculation of various evaluation metrics including accuracy, precision, recall, specificity, and ROC-AUC.
  • Functionality to save and load trained models and tokenizers.
  • Capability to push trained models to the Hugging Face Hub.

Installation

To use this package, you need to install the required libraries. It's recommended to use a virtual environment:

pip install transformers torch sklearn numpy

Usage

Training DataBERT

  1. Initialize the Classifier

You can initialize the TrainBERTClassifier with desired training parameters. Here's an example:

from bert_classifier import TrainBERTClassifier

classifier = TrainBERTClassifier(
    model_name='bert-base-uncased',
    num_labels=2,
    max_length=128,
    batch_size=32,
    learning_rate=2e-5,
    epochs=3
)
  1. Prepare your data

Organize your text data and labels for training and validation. For example:

train_texts = ['This is the first text', 'Here is another one']
train_labels = [0, 1]

val_texts = ['This text is for validation', 'Another validation text']
val_labels = [0, 1]
  1. Train the model

Use the train method to fine-tune the BERT model on your data:

classifier.train(train_texts, train_labels, val_texts, val_labels)
  1. Evaluate the Model : After training, the model's performance metrics for the validation set will be printed automatically, including accuracy, precision, recall, specificity, and ROC-AUC.

  2. Save the Model:

Save your trained model and tokenizer for later use:

classifier.save_model_and_tokenizer('path/to/save/directory')

Using the Trained BERT Classifier

After training and saving your model, you can use it for classifying new texts:

  1. Initialize the Classifier with the Trained Model:

Load your trained model and tokenizer:

from bert_classifier import BERTClassifier

model_path = 'path/to/save/directory/model'
tokenizer_name = 'bert-base-uncased'  # Or path to tokenizer if you saved it

bert_classifier = BERTClassifier(model_path, tokenizer_name)
  1. Classify new texts

You can now use the classifier to predict the class of new texts:

text = "Example text to classify"
prediction, confidence = bert_classifier.predict(text)
print(f"Predicted class: {prediction} with confidence {confidence}")

Contributions

Contributions to this package are welcome. Please follow conventional commit messages and ensure code quality for any pull requests.

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

databert-0.4.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

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

databert-0.4.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file databert-0.4.0.tar.gz.

File metadata

  • Download URL: databert-0.4.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.15

File hashes

Hashes for databert-0.4.0.tar.gz
Algorithm Hash digest
SHA256 734a362b533a6cac81098a14fdb2a77bcbb067abe10511e46fba92588516d04d
MD5 73fd6286d2ab8208fcdfb10327abf3b3
BLAKE2b-256 c4efdc7b003288333e8887e6e98a7e96d6381afbe87e12fb9951afa5c0587c04

See more details on using hashes here.

File details

Details for the file databert-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: databert-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.15

File hashes

Hashes for databert-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b1df15365c25946f0d62b1068752edacc14d782c5ab7463468703f337dfd3f38
MD5 14dc44ac14c1909bf0ec98b57b0fec60
BLAKE2b-256 efdc289e66cb5d8790cac9ea5b911f57bf6535ae7d2eb51fc010a420f21d2465

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