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
- 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
)
- 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]
- 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)
-
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.
-
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:
- 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)
- 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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file databert-0.5.0.tar.gz.
File metadata
- Download URL: databert-0.5.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71bc90899334ca6be8a7e69f1baa4d2291257f90394af6215a84a5466393e567
|
|
| MD5 |
16151df1e53256c1f3e1901d390b54d9
|
|
| BLAKE2b-256 |
378ff8d3589f4379cca9d53593d7c5d3983c65fd8fa4ecb2848854d05d91a5a8
|
File details
Details for the file databert-0.5.0-py3-none-any.whl.
File metadata
- Download URL: databert-0.5.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c6e5df9f91b5eb8cba1bec5dc214f781492e96c090a98df5004f595e7db6160
|
|
| MD5 |
0d85d3ef0e82814d7fd5edfb42086f87
|
|
| BLAKE2b-256 |
922fcb134f50c83182f3826d6f3ce6bd5b3cd5a1e679f765995e5c6ba558a006
|