sparknlp.annotator.seq2seq.t5_transformer#

Contains classes for the T5Transformer.

Module Contents#

Classes#

T5Transformer

T5: the Text-To-Text Transfer Transformer

class T5Transformer(classname='com.johnsnowlabs.nlp.annotators.seq2seq.T5Transformer', java_model=None)[source]#

T5: the Text-To-Text Transfer Transformer

T5 reconsiders all NLP tasks into a unified text-to-text-format where the input and output are always text strings, in contrast to BERT-style models that can only output either a class label or a span of the input. The text-to-text framework is able to use the same model, loss function, and hyper-parameters on any NLP task, including machine translation, document summarization, question answering, and classification tasks (e.g., sentiment analysis). T5 can even apply to regression tasks by training it to predict the string representation of a number instead of the number itself.

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

>>> t5 = T5Transformer.pretrained() \
...     .setTask("summarize:") \
...     .setInputCols(["document"]) \
...     .setOutputCol("summaries")

The default model is "t5_small", if no name is provided. For available pretrained models please see the Models Hub.

For extended examples of usage, see the Spark NLP Workshop.

Input Annotation types

Output Annotation type

DOCUMENT

DOCUMENT

Parameters:
configProtoBytes

ConfigProto from tensorflow, serialized into byte array.

task

Transformer’s task, e.g. summarize:

minOutputLength

Minimum length of the sequence to be generated

maxOutputLength

Maximum length of output text

doSample

Whether or not to use sampling; use greedy decoding otherwise

temperature

The value used to module the next token probabilities

topK

The number of highest probability vocabulary tokens to keep for top-k-filtering

topP

Top cumulative probability for vocabulary tokens

If set to float < 1, only the most probable tokens with probabilities that add up to topP or higher are kept for generation.

repetitionPenalty

The parameter for repetition penalty. 1.0 means no penalty.

noRepeatNgramSize

If set to int > 0, all ngrams of that size can only occur once

ignoreTokenIds

A list of token ids which are ignored in the decoder’s output

Notes

This is a very computationally expensive module especially on larger sequence. The use of an accelerator such as GPU is recommended.

References

Paper Abstract:

Transfer learning, where a model is first pre-trained on a data-rich task before being fine-tuned on a downstream task, has emerged as a powerful technique in natural language processing (NLP). The effectiveness of transfer learning has given rise to a diversity of approaches, methodology, and practice. In this paper, we explore the landscape of transfer learning techniques for NLP by introducing a unified framework that converts all text-based language problems into a text-to-text format. Our systematic study compares pre-training objectives, architectures, unlabeled data sets, transfer approaches, and other factors on dozens of language understanding tasks. By combining the insights from our exploration with scale and our new Colossal Clean Crawled Corpus, we achieve state-of-the-art results on many benchmarks covering summarization, question answering, text classification, and more. To facilitate future work on transfer learning for NLP, we release our data set, pre-trained models, and code.

Examples

>>> import sparknlp
>>> from sparknlp.base import *
>>> from sparknlp.annotator import *
>>> from pyspark.ml import Pipeline
>>> documentAssembler = DocumentAssembler() \
...     .setInputCol("text") \
...     .setOutputCol("documents")
>>> t5 = T5Transformer.pretrained("t5_small") \
...     .setTask("summarize:") \
...     .setInputCols(["documents"]) \
...     .setMaxOutputLength(200) \
...     .setOutputCol("summaries")
>>> pipeline = Pipeline().setStages([documentAssembler, t5])
>>> data = spark.createDataFrame([[
...     "Transfer learning, where a model is first pre-trained on a data-rich task before being fine-tuned on a " +
...     "downstream task, has emerged as a powerful technique in natural language processing (NLP). The effectiveness" +
...     " of transfer learning has given rise to a diversity of approaches, methodology, and practice. In this " +
...     "paper, we explore the landscape of transfer learning techniques for NLP by introducing a unified framework " +
...     "that converts all text-based language problems into a text-to-text format. Our systematic study compares " +
...     "pre-training objectives, architectures, unlabeled data sets, transfer approaches, and other factors on dozens " +
...     "of language understanding tasks. By combining the insights from our exploration with scale and our new " +
...     "Colossal Clean Crawled Corpus, we achieve state-of-the-art results on many benchmarks covering " +
...     "summarization, question answering, text classification, and more. To facilitate future work on transfer " +
...     "learning for NLP, we release our data set, pre-trained models, and code."
... ]]).toDF("text")
>>> result = pipeline.fit(data).transform(data)
>>> result.select("summaries.result").show(truncate=False)
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|result                                                                                                                                                                                                        |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|[transfer learning has emerged as a powerful technique in natural language processing (NLP) the effectiveness of transfer learning has given rise to a diversity of approaches, methodologies, and practice .]|
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
name = T5Transformer[source]#
configProtoBytes[source]#
task[source]#
minOutputLength[source]#
maxOutputLength[source]#
doSample[source]#
temperature[source]#
topK[source]#
topP[source]#
repetitionPenalty[source]#
noRepeatNgramSize[source]#
ignoreTokenIds[source]#
setIgnoreTokenIds(self, value)[source]#

A list of token ids which are ignored in the decoder’s output.

Parameters:
valueList[int]

The words to be filtered out

setConfigProtoBytes(self, b)[source]#

Sets configProto from tensorflow, serialized into byte array.

Parameters:
bList[int]

ConfigProto from tensorflow, serialized into byte array

setTask(self, value)[source]#

Sets the transformer’s task, e.g. summarize:.

Parameters:
valuestr

The transformer’s task

setMinOutputLength(self, value)[source]#

Sets minimum length of the sequence to be generated.

Parameters:
valueint

Minimum length of the sequence to be generated

setMaxOutputLength(self, value)[source]#

Sets maximum length of output text.

Parameters:
valueint

Maximum length of output text

setDoSample(self, value)[source]#

Sets whether or not to use sampling, use greedy decoding otherwise.

Parameters:
valuebool

Whether or not to use sampling; use greedy decoding otherwise

setTemperature(self, value)[source]#

Sets the value used to module the next token probabilities.

Parameters:
valuefloat

The value used to module the next token probabilities

setTopK(self, value)[source]#

Sets the number of highest probability vocabulary tokens to keep for top-k-filtering.

Parameters:
valueint

Number of highest probability vocabulary tokens to keep

setTopP(self, value)[source]#

Sets the top cumulative probability for vocabulary tokens.

If set to float < 1, only the most probable tokens with probabilities that add up to topP or higher are kept for generation.

Parameters:
valuefloat

Cumulative probability for vocabulary tokens

setRepetitionPenalty(self, value)[source]#

Sets the parameter for repetition penalty. 1.0 means no penalty.

Parameters:
valuefloat

The repetition penalty

References

See Ctrl: A Conditional Transformer Language Model For Controllable Generation for more details.

setNoRepeatNgramSize(self, value)[source]#

Sets size of n-grams that can only occur once.

If set to int > 0, all ngrams of that size can only occur once.

Parameters:
valueint

N-gram size can only occur once

static loadSavedModel(folder, spark_session)[source]#

Loads a locally saved model.

Parameters:
folderstr

Folder of the saved model

spark_sessionpyspark.sql.SparkSession

The current SparkSession

Returns:
T5Transformer

The restored model

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

Downloads and loads a pretrained model.

Parameters:
namestr, optional

Name of the pretrained model, by default “t5_small”

langstr, optional

Language of the pretrained model, by default “en”

remote_locstr, optional

Optional remote address of the resource, by default None. Will use Spark NLPs repositories otherwise.

Returns:
T5Transformer

The restored model