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.3.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.3.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: databert-0.3.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.3.0.tar.gz
Algorithm Hash digest
SHA256 fea09648d73b1d8220d4649feb0cafaf4e36d0efe078456115baca0acd6daedd
MD5 54aaf74463f7d8cf8af8d19fd0fae20c
BLAKE2b-256 b99cf2cf2711477b96ca50d54ddbbe20668e058bb4aa8509e7a6cdc399d8d643

See more details on using hashes here.

File details

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

File metadata

  • Download URL: databert-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e0ce3cf7c6efbd3617518dbad82c27eb83755e8e34150d5905351c3ae9905553
MD5 09633f8efa2cda66a1687c5780584358
BLAKE2b-256 91b9c135560505449d01f4cc62eb53fa313d34255c711278662edf428e2f8b4b

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