ClassifierBase
def ClassifierBase(
*args, **kwargs
):Helper class that provides a standard way to create an ABC using inheritance.
Helper class that provides a standard way to create an ABC using inheritance.
Convert train or test examples to HF dataset
Convert a Hugging Face dataset to X, y arrays
Evaluates labeled data using the trained model. If print_report is True, prints classification report and returns nothing. Otherwise, returns and prints a dictionary of the results. Extra kwargs fed to self.predict.
Explain the predictions on given examples in X. (Requires shap and matplotlib to be installed.)
Sample a dataset with num_samples per class
Helper class that provides a standard way to create an ABC using inheritance.
Trains the classifier on a list of texts (X) and a list of labels (y). Additional keyword arguments are passed directly to self.model.fit.
Args:
Returns:
Save model to specified filename (e.g., /tmp/mymodel.gz). Model saved as pickle file. To reload the model, supply model_path when instantiatingSKClassifier.
categories = [
"alt.atheism",
"soc.religion.christian",
"comp.graphics",
"sci.med" ]
train_b = fetch_20newsgroups(
subset="train", categories=categories, shuffle=True, random_state=42
)
test_b = fetch_20newsgroups(
subset="test", categories=categories, shuffle=True, random_state=42
)
x_train = train_b.data
y_train = train_b.target
x_test = test_b.data
y_test = test_b.target
classes = train_b.target_names
# y_test = [classes[y] for y in y_test]
# y_train = [classes[y] for y in y_train]
clf = SKClassifier(labels=classes)
clf.train(x_train, y_train)
test_doc1 = "Jesus Christ was a first century Jewish teacher and religious leader."
test_doc2 = "The graphics on my monitor are terrible."
print(clf.predict(test_doc1))
print(clf.predict([test_doc2]))
print(clf.predict([test_doc1, test_doc2]))
clf.evaluate(x_test, y_test)soc.religion.christian
comp.graphics
['soc.religion.christian', 'comp.graphics']
precision recall f1-score support
alt.atheism 0.93 0.87 0.90 319
comp.graphics 0.88 0.96 0.92 389
sci.med 0.94 0.84 0.89 396
soc.religion.christian 0.91 0.96 0.94 398
accuracy 0.91 1502
macro avg 0.91 0.91 0.91 1502
weighted avg 0.91 0.91 0.91 1502
precision recall f1-score support
alt.atheism 0.93 0.87 0.90 319
comp.graphics 0.88 0.96 0.92 389
sci.med 0.94 0.84 0.89 396
soc.religion.christian 0.91 0.96 0.94 398
accuracy 0.91 1502
macro avg 0.91 0.91 0.91 1502
weighted avg 0.91 0.91 0.91 1502
Helper class that provides a standard way to create an ABC using inheritance.
Trains the classifier on a list of texts (X) and a list of labels (y). Extra kwargs are treated as arguments to transformers.TrainingArguments.
Args:
Returns:
Predict labels. Extra kwargs fed to Hugging Face transformers text-classification pipeline.
Predict labels. Extra kwargs fed to Hugging Face transformers text-classification pipeline.
The default model is a tiny BERT model (i.e., `google/bert_uncased_L-2_H-128_A-2), but we will use a larger model here to improve accuracy (e.g., distilbert).
categories = [
"alt.atheism",
"soc.religion.christian",
"comp.graphics",
"sci.med" ]
train_b = fetch_20newsgroups(
subset="train", categories=categories, shuffle=True, random_state=42
)
test_b = fetch_20newsgroups(
subset="test", categories=categories, shuffle=True, random_state=42
)
x_train = train_b.data
y_train = train_b.target
x_test = test_b.data
y_test = test_b.target
classes = train_b.target_names
clf = HFClassifier(model_id_or_path='distilbert/distilbert-base-uncased',
device='cuda', labels=classes)
clf.train(x_train, y_train, num_train_epochs=1, per_device_train_batch_size=8)
test_doc1 = "Jesus Christ was a first century Jewish teacher and religious leader."
test_doc2 = "The graphics on my monitor are terrible."
print(clf.predict(test_doc1))
print(clf.predict([test_doc2]))
print(clf.predict([test_doc1, test_doc2]))
clf.evaluate(x_test, y_test)Some weights of DistilBertForSequenceClassification were not initialized from the model checkpoint at distilbert/distilbert-base-uncased and are newly initialized: ['classifier.bias', 'classifier.weight', 'pre_classifier.bias', 'pre_classifier.weight']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
| Step | Training Loss |
|---|
soc.religion.christian
comp.graphics
['soc.religion.christian', 'comp.graphics']
precision recall f1-score support
alt.atheism 0.89 0.88 0.89 319
comp.graphics 0.97 0.98 0.97 389
sci.med 0.97 0.95 0.96 396
soc.religion.christian 0.94 0.96 0.95 398
accuracy 0.95 1502
macro avg 0.94 0.94 0.94 1502
weighted avg 0.95 0.95 0.95 1502
You seem to be using the pipelines sequentially on GPU. In order to maximize efficiency please use a dataset
0.9454061251664447
Helper class that provides a standard way to create an ABC using inheritance.
Trains the classifier on a list of texts (X) and a list of labels (y). Additional keyword arguments are passed directly to SetFit.TrainingArguments
Args:
Returns:
predict label probabilities
Save model to specified folder path, save_path. To reload the model, supply path in model_id_or_path argument when instantiatingFewShotClassifier.
model_head.pkl not found on HuggingFace Hub, initialising classification head with random weights. You should TRAIN this model on a downstream task to use it for predictions and inference.
Sample a tiny dataset with only 8 examples per class (or 16 total examples):
dataset = load_dataset("SetFit/sst2")
X_train, y_train = clf.dataset2arrays(dataset["train"], text_key="text", label_key="label")
X_test, y_test = clf.dataset2arrays(dataset["test"], text_key="text", label_key="label")
X_sample, y_sample = clf.sample_examples(X_train, y_train, label_key="label", num_samples=8)Repo card metadata block was not found. Setting CardData to empty.
Applying column mapping to the training dataset
***** Running training *****
Num unique pairs = 144
Batch size = 32
Num epochs = 10
| Step | Training Loss |
|---|---|
| 1 | 0.242700 |
| 50 | 0.047300 |
precision recall f1-score support
negative 0.88 0.94 0.91 912
positive 0.93 0.87 0.90 909
accuracy 0.91 1821
macro avg 0.91 0.91 0.91 1821
weighted avg 0.91 0.91 0.91 1821
tensor([[0.1657, 0.8343],
[0.8551, 0.1449]], dtype=torch.float64)
['positive', 'negative']