com.johnsnowlabs.nlp.annotators.classifier.dl
BertForMultipleChoice
Companion object BertForMultipleChoice
class BertForMultipleChoice extends AnnotatorModel[BertForMultipleChoice] with HasBatchedAnnotate[BertForMultipleChoice] with WriteOnnxModel with WriteOpenvinoModel with HasCaseSensitiveProperties with HasEngine
BertForMultipleChoice can load BERT Models with a multiple choice classification head on top (a linear layer on top of the pooled output and a softmax) e.g. for RocStories/SWAG tasks.
Pretrained models can be loaded with pretrained
of the companion object:
val spanClassifier = BertForMultipleChoice.pretrained() .setInputCols(Array("document_question", "document_context")) .setOutputCol("answer")
The default model is "bert_base_uncased_multiple_choice"
, if no name is provided.
For available pretrained models please see the Models Hub.
Models from the HuggingFace 🤗 Transformers library are also compatible with Spark NLP 🚀. To see which models are compatible and how to import them see https://github.com/JohnSnowLabs/spark-nlp/discussions/5669 and to see more extended examples, see BertForMultipleChoiceTestSpec.
Example
import spark.implicits._ import com.johnsnowlabs.nlp.base._ import com.johnsnowlabs.nlp.annotator._ import org.apache.spark.ml.Pipeline val document = new MultiDocumentAssembler() .setInputCols("question", "context") .setOutputCols("document_question", "document_context") val questionAnswering = BertForMultipleChoice.pretrained() .setInputCols(Array("document_question", "document_context")) .setOutputCol("answer") .setCaseSensitive(false) val pipeline = new Pipeline().setStages(Array( document, questionAnswering )) val data = Seq("The Eiffel Tower is located in which country?", "Germany, France, Italy").toDF("question", "context") val result = pipeline.fit(data).transform(data) result.select("answer.result").show(false) +---------------------+ |result | +---------------------+ |[France] | ++--------------------+
- See also
BertForQuestionAnswering for Question Answering tasks
Annotators Main Page for a list of transformer based classifiers
- Grouped
- Alphabetic
- By Inheritance
- BertForMultipleChoice
- HasEngine
- HasCaseSensitiveProperties
- WriteOpenvinoModel
- WriteOnnxModel
- HasBatchedAnnotate
- AnnotatorModel
- CanBeLazy
- RawAnnotator
- HasOutputAnnotationCol
- HasInputAnnotationCols
- HasOutputAnnotatorType
- ParamsAndFeaturesWritable
- HasFeatures
- DefaultParamsWritable
- MLWritable
- Model
- Transformer
- PipelineStage
- Logging
- Params
- Serializable
- Serializable
- Identifiable
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Parameters
A list of (hyper-)parameter keys this annotator can take. Users can set and get the parameter values through setters and getters, respectively.
-
val
batchSize: IntParam
Size of every batch (Default depends on model).
Size of every batch (Default depends on model).
- Definition Classes
- HasBatchedAnnotate
-
val
caseSensitive: BooleanParam
Whether to ignore case in index lookups (Default depends on model)
Whether to ignore case in index lookups (Default depends on model)
- Definition Classes
- HasCaseSensitiveProperties
-
val
engine: Param[String]
This param is set internally once via loadSavedModel.
This param is set internally once via loadSavedModel. That's why there is no setter
- Definition Classes
- HasEngine
-
val
maxSentenceLength: IntParam
Max sentence length to process (Default:
512
) -
val
vocabulary: MapFeature[String, Int]
Vocabulary used to encode the words to ids with WordPieceEncoder
Members
-
type
AnnotatorType = String
- Definition Classes
- HasOutputAnnotatorType
-
def
batchAnnotate(batchedAnnotations: Seq[Array[Annotation]]): Seq[Seq[Annotation]]
takes a document and annotations and produces new annotations of this annotator's annotation type
takes a document and annotations and produces new annotations of this annotator's annotation type
- batchedAnnotations
Annotations in batches that correspond to inputAnnotationCols generated by previous annotators if any
- returns
any number of annotations processed for every batch of input annotations. Not necessary one to one relationship IMPORTANT: !MUST! return sequences of equal lengths !! IMPORTANT: !MUST! return sentences that belong to the same original row !! (challenging)
- Definition Classes
- BertForMultipleChoice → HasBatchedAnnotate
-
def
batchProcess(rows: Iterator[_]): Iterator[Row]
- Definition Classes
- HasBatchedAnnotate
- val choicesDelimiter: Param[String]
-
final
def
clear(param: Param[_]): BertForMultipleChoice.this.type
- Definition Classes
- Params
-
def
copy(extra: ParamMap): BertForMultipleChoice
requirement for annotators copies
requirement for annotators copies
- Definition Classes
- RawAnnotator → Model → Transformer → PipelineStage → Params
-
def
explainParam(param: Param[_]): String
- Definition Classes
- Params
-
def
explainParams(): String
- Definition Classes
- Params
-
final
def
extractParamMap(): ParamMap
- Definition Classes
- Params
-
final
def
extractParamMap(extra: ParamMap): ParamMap
- Definition Classes
- Params
-
val
features: ArrayBuffer[Feature[_, _, _]]
- Definition Classes
- HasFeatures
-
final
def
get[T](param: Param[T]): Option[T]
- Definition Classes
- Params
-
final
def
getDefault[T](param: Param[T]): Option[T]
- Definition Classes
- Params
-
def
getInputCols: Array[String]
- returns
input annotations columns currently used
- Definition Classes
- HasInputAnnotationCols
-
def
getLazyAnnotator: Boolean
- Definition Classes
- CanBeLazy
-
final
def
getOrDefault[T](param: Param[T]): T
- Definition Classes
- Params
-
final
def
getOutputCol: String
Gets annotation column name going to generate
Gets annotation column name going to generate
- Definition Classes
- HasOutputAnnotationCol
-
def
getParam(paramName: String): Param[Any]
- Definition Classes
- Params
-
final
def
hasDefault[T](param: Param[T]): Boolean
- Definition Classes
- Params
-
def
hasParam(paramName: String): Boolean
- Definition Classes
- Params
-
def
hasParent: Boolean
- Definition Classes
- Model
-
val
inputAnnotatorTypes: Array[AnnotatorType]
Annotator reference id.
Annotator reference id. Used to identify elements in metadata or to refer to this annotator type
- Definition Classes
- BertForMultipleChoice → HasInputAnnotationCols
-
final
def
isDefined(param: Param[_]): Boolean
- Definition Classes
- Params
-
final
def
isSet(param: Param[_]): Boolean
- Definition Classes
- Params
-
val
lazyAnnotator: BooleanParam
- Definition Classes
- CanBeLazy
-
def
onWrite(path: String, spark: SparkSession): Unit
- Definition Classes
- BertForMultipleChoice → ParamsAndFeaturesWritable
-
val
optionalInputAnnotatorTypes: Array[String]
- Definition Classes
- HasInputAnnotationCols
-
val
outputAnnotatorType: AnnotatorType
- Definition Classes
- BertForMultipleChoice → HasOutputAnnotatorType
-
lazy val
params: Array[Param[_]]
- Definition Classes
- Params
-
var
parent: Estimator[BertForMultipleChoice]
- Definition Classes
- Model
-
def
save(path: String): Unit
- Definition Classes
- MLWritable
- Annotations
- @Since( "1.6.0" ) @throws( ... )
-
final
def
set[T](param: Param[T], value: T): BertForMultipleChoice.this.type
- Definition Classes
- Params
- def setChoicesDelimiter(value: String): BertForMultipleChoice.this.type
-
final
def
setInputCols(value: String*): BertForMultipleChoice.this.type
- Definition Classes
- HasInputAnnotationCols
-
def
setInputCols(value: Array[String]): BertForMultipleChoice.this.type
Overrides required annotators column if different than default
Overrides required annotators column if different than default
- Definition Classes
- HasInputAnnotationCols
-
def
setLazyAnnotator(value: Boolean): BertForMultipleChoice.this.type
- Definition Classes
- CanBeLazy
-
final
def
setOutputCol(value: String): BertForMultipleChoice.this.type
Overrides annotation column name when transforming
Overrides annotation column name when transforming
- Definition Classes
- HasOutputAnnotationCol
-
def
setParent(parent: Estimator[BertForMultipleChoice]): BertForMultipleChoice
- Definition Classes
- Model
-
def
toString(): String
- Definition Classes
- Identifiable → AnyRef → Any
-
final
def
transform(dataset: Dataset[_]): DataFrame
Given requirements are met, this applies ML transformation within a Pipeline or stand-alone Output annotation will be generated as a new column, previous annotations are still available separately metadata is built at schema level to record annotations structural information outside its content
Given requirements are met, this applies ML transformation within a Pipeline or stand-alone Output annotation will be generated as a new column, previous annotations are still available separately metadata is built at schema level to record annotations structural information outside its content
- dataset
Dataset[Row]
- Definition Classes
- AnnotatorModel → Transformer
-
def
transform(dataset: Dataset[_], paramMap: ParamMap): DataFrame
- Definition Classes
- Transformer
- Annotations
- @Since( "2.0.0" )
-
def
transform(dataset: Dataset[_], firstParamPair: ParamPair[_], otherParamPairs: ParamPair[_]*): DataFrame
- Definition Classes
- Transformer
- Annotations
- @Since( "2.0.0" ) @varargs()
-
final
def
transformSchema(schema: StructType): StructType
requirement for pipeline transformation validation.
requirement for pipeline transformation validation. It is called on fit()
- Definition Classes
- RawAnnotator → PipelineStage
-
val
uid: String
- Definition Classes
- BertForMultipleChoice → Identifiable
-
def
write: MLWriter
- Definition Classes
- ParamsAndFeaturesWritable → DefaultParamsWritable → MLWritable
-
def
writeOnnxModel(path: String, spark: SparkSession, onnxWrapper: OnnxWrapper, suffix: String, fileName: String): Unit
- Definition Classes
- WriteOnnxModel
-
def
writeOnnxModels(path: String, spark: SparkSession, onnxWrappersWithNames: Seq[(OnnxWrapper, String)], suffix: String): Unit
- Definition Classes
- WriteOnnxModel
-
def
writeOpenvinoModel(path: String, spark: SparkSession, openvinoWrapper: OpenvinoWrapper, suffix: String, fileName: String): Unit
- Definition Classes
- WriteOpenvinoModel
-
def
writeOpenvinoModels(path: String, spark: SparkSession, ovWrappersWithNames: Seq[(OpenvinoWrapper, String)], suffix: String): Unit
- Definition Classes
- WriteOpenvinoModel
Parameter setters
- def sentenceEndTokenId: Int
- def sentenceStartTokenId: Int
-
def
setBatchSize(size: Int): BertForMultipleChoice.this.type
Size of every batch.
Size of every batch.
- Definition Classes
- HasBatchedAnnotate
-
def
setCaseSensitive(value: Boolean): BertForMultipleChoice.this.type
- Definition Classes
- HasCaseSensitiveProperties
- def setMaxSentenceLength(value: Int): BertForMultipleChoice.this.type
- def setModelIfNotSet(spark: SparkSession, tensorflowWrapper: Option[TensorflowWrapper], onnxWrapper: Option[OnnxWrapper], openvinoWrapper: Option[OpenvinoWrapper]): BertForMultipleChoice
- def setVocabulary(value: Map[String, Int]): BertForMultipleChoice.this.type
Parameter getters
-
def
getBatchSize: Int
Size of every batch.
Size of every batch.
- Definition Classes
- HasBatchedAnnotate
-
def
getCaseSensitive: Boolean
- Definition Classes
- HasCaseSensitiveProperties
-
def
getEngine: String
- Definition Classes
- HasEngine
- def getModelIfNotSet: BertClassification