sparknlp.annotator.classifier_dl.distil_bert_for_zero_shot_classification#

Contains classes for DistilBertForZeroShotClassification.

Module Contents#

Classes#

DistilBertForZeroShotClassification

DistilBertForZeroShotClassification using a ModelForSequenceClassification trained on NLI (natural language

class DistilBertForZeroShotClassification(classname='com.johnsnowlabs.nlp.annotators.classifier.dl.DistilBertForZeroShotClassification', java_model=None)[source]#

DistilBertForZeroShotClassification using a ModelForSequenceClassification trained on NLI (natural language inference) tasks. Equivalent of DistilBertForSequenceClassification models, but these models don’t require a hardcoded number of potential classes, they can be chosen at runtime. It usually means it’s slower but it is much more flexible.

Note that the model will loop through all provided labels. So the more labels you have, the longer this process will take.

Any combination of sequences and labels can be passed and each combination will be posed as a premise/hypothesis pair and passed to the pretrained model.

Pretrained models can be loaded with pretrained() of the companion object:

>>> sequenceClassifier = DistilBertForZeroShotClassification.pretrained() \
...     .setInputCols(["token", "document"]) \
...     .setOutputCol("label")

The default model is "distilbert_base_zero_shot_classifier_uncased_mnli", if no name is provided.

For available pretrained models please see the Models Hub.

To see which models are compatible and how to import them see Import Transformers into Spark NLP 🚀.

Input Annotation types

Output Annotation type

DOCUMENT, TOKEN

CATEGORY

Parameters:
batchSize

Batch size. Large values allows faster processing but requires more memory, by default 8

caseSensitive

Whether to ignore case in tokens for embeddings matching, by default True

configProtoBytes

ConfigProto from tensorflow, serialized into byte array.

maxSentenceLength

Max sentence length to process, by default 128

coalesceSentences

Instead of 1 class per sentence (if inputCols is sentence) output 1 class per document by averaging probabilities in all sentences, by default False

activation

Whether to calculate logits via Softmax or Sigmoid, by default “softmax”.

Examples

>>> import sparknlp
>>> from sparknlp.base import *
>>> from sparknlp.annotator import *
>>> from pyspark.ml import Pipeline
>>> documentAssembler = DocumentAssembler() \
...     .setInputCol("text") \
...     .setOutputCol("document")
>>> tokenizer = Tokenizer() \
...     .setInputCols(["document"]) \
...     .setOutputCol("token")
>>> sequenceClassifier = DistilBertForZeroShotClassification.pretrained() \
...     .setInputCols(["token", "document"]) \
...     .setOutputCol("label") \
...     .setCaseSensitive(True)
>>> pipeline = Pipeline().setStages([
...     documentAssembler,
...     tokenizer,
...     sequenceClassifier
... ])
>>> data = spark.createDataFrame([["I loved this movie when I was a child.", "It was pretty boring."]]).toDF("text")
>>> result = pipeline.fit(data).transform(data)
>>> result.select("label.result").show(truncate=False)
+------+
|result|
+------+
|[pos] |
|[neg] |
+------+
getClasses()[source]#

Returns labels used to train this model

setConfigProtoBytes(b)[source]#

Sets configProto from tensorflow, serialized into byte array.

Parameters:
bList[int]

ConfigProto from tensorflow, serialized into byte array

setCoalesceSentences(value)[source]#

Instead of 1 class per sentence (if inputCols is ‘’’sentence’’’) output 1 class per document by averaging probabilities in all sentences. Due to max sequence length limit in almost all transformer models such as DistilBERT (512 tokens), this parameter helps to feed all the sentences into the model and averaging all the probabilities for the entire document instead of probabilities per sentence. (Default: true)

Parameters:
valuebool

If the output of all sentences will be averaged to one output

static loadSavedModel(folder, spark_session)[source]#

Loads a locally saved model.

Parameters:
folderstr

Folder of the saved model spark_session : pyspark.sql.SparkSession The current SparkSession

Returns:
DistilBertForZeroShotClassification

The restored model

static pretrained(name='distilbert_base_zero_shot_classifier_uncased_mnli', lang='en', remote_loc=None)[source]#

Downloads and loads a pretrained model.

Parameters:
namestr, optional

Name of the pretrained model, by default “distilbert_base_zero_shot_classifier_uncased_mnli” lang : str, optional Language of the pretrained model, by default “en” remote_loc : str, optional Optional remote address of the resource, by default None. Will use Spark NLPs repositories otherwise.

Returns:
DistilBertForZeroShotClassification

The restored model