-
Notifications
You must be signed in to change notification settings - Fork 707
/
DistilBertForSequenceClassificationTestSpec.scala
174 lines (134 loc) · 6.27 KB
/
DistilBertForSequenceClassificationTestSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright 2017-2022 John Snow Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.johnsnowlabs.nlp.annotators.classifier.dl
import com.johnsnowlabs.nlp.annotators.Tokenizer
import com.johnsnowlabs.nlp.base.DocumentAssembler
import com.johnsnowlabs.nlp.training.CoNLL
import com.johnsnowlabs.nlp.util.io.ResourceHelper
import com.johnsnowlabs.tags.SlowTest
import com.johnsnowlabs.util.Benchmark
import org.apache.spark.ml.{Pipeline, PipelineModel}
import org.apache.spark.sql.functions.{col, explode, size}
import org.scalatest.flatspec.AnyFlatSpec
class DistilBertForSequenceClassificationTestSpec extends AnyFlatSpec {
import ResourceHelper.spark.implicits._
"DistilBertForSequenceClassification" should "correctly load custom model with extracted signatures" taggedAs SlowTest in {
val ddd = Seq(
"John Lenon was born in London and lived in Paris. My name is Sarah and I live in London.",
"Rare Hendrix song draft sells for almost $17,000.",
"EU rejects German call to boycott British lamb .",
"TORONTO 1996-08-21",
" carbon emissions have come down without impinging on our growth. . . .",
"\\u2009.carbon emissions have come down without impinging on our growth .\\u2009.\\u2009.")
.toDF("text")
val document = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("document")
val tokenizer = new Tokenizer()
.setInputCols(Array("document"))
.setOutputCol("token")
val tokenClassifier = DistilBertForSequenceClassification
.pretrained()
.setInputCols(Array("token", "document"))
.setOutputCol("label")
.setCaseSensitive(false)
.setCoalesceSentences(false)
val pipeline = new Pipeline().setStages(Array(document, tokenizer, tokenClassifier))
val pipelineModel = pipeline.fit(ddd)
val pipelineDF = pipelineModel.transform(ddd)
pipelineDF.select("label").show(20, truncate = false)
pipelineDF.select("document.result", "label.result").show(20, truncate = false)
pipelineDF
.withColumn("doc_size", size(col("document")))
.withColumn("label_size", size(col("label")))
.where(col("doc_size") =!= col("label_size"))
.select("doc_size", "label_size", "document.result", "label.result")
.show(20, truncate = false)
val totalDocs = pipelineDF.select(explode($"document.result")).count.toInt
val totalLabels = pipelineDF.select(explode($"label.result")).count.toInt
println(s"total tokens: $totalDocs")
println(s"total embeddings: $totalLabels")
assert(totalDocs == totalLabels)
}
"DistilBertForSequenceClassification" should "be saved and loaded correctly" taggedAs SlowTest in {
import ResourceHelper.spark.implicits._
val ddd = Seq(
"John Lenon was born in London and lived in Paris. My name is Sarah and I live in London",
"Rare Hendrix song draft sells for almost $17,000.",
"EU rejects German call to boycott British lamb .",
"TORONTO 1996-08-21").toDF("text")
val document = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("document")
val tokenizer = new Tokenizer()
.setInputCols(Array("document"))
.setOutputCol("token")
val tokenClassifier = DistilBertForSequenceClassification
.pretrained()
.setInputCols(Array("token", "document"))
.setOutputCol("label")
.setCaseSensitive(true)
val pipeline = new Pipeline().setStages(Array(document, tokenizer, tokenClassifier))
val pipelineModel = pipeline.fit(ddd)
val pipelineDF = pipelineModel.transform(ddd)
pipelineDF.select("label.result").show(false)
Benchmark.time("Time to save DistilBertForSequenceClassification pipeline model") {
pipelineModel.write.overwrite().save("./tmp_distilbertforsequence_pipeline")
}
Benchmark.time("Time to save DistilBertForSequenceClassification model") {
pipelineModel.stages.last
.asInstanceOf[DistilBertForSequenceClassification]
.write
.overwrite()
.save("./tmp_distilbertforsequence_model")
}
val loadedPipelineModel = PipelineModel.load("./tmp_distilbertforsequence_pipeline")
loadedPipelineModel.transform(ddd).select("label.result").show(false)
val loadedSequenceModel =
DistilBertForSequenceClassification.load("./tmp_distilbertforsequence_model")
println(loadedSequenceModel.getClasses.mkString("Array(", ", ", ")"))
}
"DistilBertForSequenceClassification" should "benchmark test" taggedAs SlowTest in {
val conll = CoNLL()
val training_data =
conll.readDataset(ResourceHelper.spark, "src/test/resources/conll2003/eng.train")
val tokenClassifier = DistilBertForSequenceClassification
.pretrained()
.setInputCols(Array("token", "document"))
.setOutputCol("class")
.setCaseSensitive(true)
val pipeline = new Pipeline()
.setStages(Array(tokenClassifier))
val pipelineDF = pipeline.fit(training_data).transform(training_data).cache()
Benchmark.time("Time to save pipeline results") {
pipelineDF.write.mode("overwrite").parquet("./tmp_sequence_classifier")
}
pipelineDF.select("label").show(2, false)
pipelineDF.select("document.result", "label.result").show(2, false)
// only works if it's softmax - one lable per row
pipelineDF
.withColumn("doc_size", size(col("document")))
.withColumn("label_size", size(col("class")))
.where(col("doc_size") =!= col("label_size"))
.select("doc_size", "label_size", "document.result", "class.result")
.show(20, false)
val totalDocs = pipelineDF.select(explode($"document.result")).count.toInt
val totalLabels = pipelineDF.select(explode($"class.result")).count.toInt
println(s"total docs: $totalDocs")
println(s"total classes: $totalLabels")
assert(totalDocs == totalLabels)
}
}