Description
Longformer Model with sequence classification/regression head on top (a linear layer on top of the pooled output) e.g. for multi-class document classification tasks.
longformer_base_sequence_classifier_imdb
is a fine-tuned Longformer model that is ready to be used for Sequence Classification tasks such as sentiment analysis or multi-class text classification and it achieves state-of-the-art performance.
We used TFLongformerForSequenceClassification to train this model and used LongformerForSequenceClassification annotator in Spark NLP 🚀 for prediction at scale!
Predicted Entities
neg
, pos
How to use
document_assembler = DocumentAssembler() \
.setInputCol('text') \
.setOutputCol('document')
tokenizer = Tokenizer() \
.setInputCols(['document']) \
.setOutputCol('token')
sequenceClassifier = LongformerForSequenceClassification \
.pretrained('longformer_base_sequence_classifier_imdb', 'en') \
.setInputCols(['token', 'document']) \
.setOutputCol('class') \
.setCaseSensitive(True) \
.setMaxSentenceLength(1024)
pipeline = Pipeline(stages=[
document_assembler,
tokenizer,
sequenceClassifier
])
example = spark.createDataFrame([['I really liked that movie!']]).toDF("text")
result = pipeline.fit(example).transform(example)
val document_assembler = DocumentAssembler()
.setInputCol("text")
.setOutputCol("document")
val tokenizer = Tokenizer()
.setInputCols("document")
.setOutputCol("token")
val tokenClassifier = LongformerForSequenceClassification.pretrained("longformer_base_sequence_classifier_imdb", "en")
.setInputCols("document", "token")
.setOutputCol("class")
.setCaseSensitive(true)
.setMaxSentenceLength(1024)
val pipeline = new Pipeline().setStages(Array(document_assembler, tokenizer, sequenceClassifier))
val example = Seq("I really liked that movie!").toDS.toDF("text")
val result = pipeline.fit(example).transform(example)
import nlu
nlu.load("en.classify.imdb.longformer").predict("""I really liked that movie!""")
Results
* +--------------------+
* |result |
* +--------------------+
* |[neg, neg] |
* |[pos, pos, pos, pos]|
* +--------------------+
Model Information
Model Name: | longformer_base_sequence_classifier_imdb |
Compatibility: | Spark NLP 3.4.0+ |
License: | Open Source |
Edition: | Official |
Input Labels: | [token, document] |
Output Labels: | [ner] |
Language: | en |
Size: | 555.4 MB |
Case sensitive: | true |
Max sentense length: | 4096 |
Data Source
https://huggingface.co/datasets/imdb
Benchmarking
{
"eval_loss": 0.17011842131614685,
"eval_accuracy": 0.95484,
"eval_f1": 0.95484,
"eval_precision": 0.95484,
"eval_recall": 0.95484,
"eval_runtime": 337.8184,
"eval_samples_per_second": 74.004,
"eval_steps_per_second": 2.315,
"epoch": 3.0
}