-
Notifications
You must be signed in to change notification settings - Fork 705
/
AlbertEmbeddingsTestSpec.scala
164 lines (130 loc) · 5.92 KB
/
AlbertEmbeddingsTestSpec.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
/*
* 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.embeddings
import com.johnsnowlabs.nlp.annotator._
import com.johnsnowlabs.nlp.base._
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
import org.apache.spark.sql.functions.{col, explode, size}
import org.scalatest.flatspec.AnyFlatSpec
class AlbertEmbeddingsTestSpec extends AnyFlatSpec {
"AlbertEmbeddings" should "correctly load pretrained model" taggedAs SlowTest in {
val smallCorpus = ResourceHelper.spark.read
.option("header", "true")
.csv("src/test/resources/embeddings/sentence_embeddings.csv")
val documentAssembler = new DocumentAssembler()
.setInputCol("text")
.setOutputCol("document")
val sentence = new SentenceDetector()
.setInputCols("document")
.setOutputCol("sentence")
val tokenizer = new Tokenizer()
.setInputCols(Array("sentence"))
.setOutputCol("token")
val embeddings = AlbertEmbeddings
.pretrained()
.setInputCols("sentence", "token")
.setOutputCol("embeddings")
val pipeline = new Pipeline()
.setStages(Array(documentAssembler, sentence, tokenizer, embeddings))
val pipelineDF = pipeline.fit(smallCorpus).transform(smallCorpus)
pipelineDF.select("token.result").show(1, truncate = false)
pipelineDF.select("embeddings.result").show(1, truncate = false)
pipelineDF.select("embeddings.metadata").show(1, truncate = false)
pipelineDF.select("embeddings.embeddings").show(1, truncate = 300)
pipelineDF.select(size(pipelineDF("embeddings.embeddings")).as("embeddings_size")).show(1)
Benchmark.time("Time to save BertEmbeddings results") {
pipelineDF.select("embeddings").write.mode("overwrite").parquet("./tmp_albert_embeddings")
}
}
"AlbertEmbeddings" should "benchmark test" taggedAs SlowTest in {
import ResourceHelper.spark.implicits._
val conll = CoNLL()
val training_data =
conll.readDataset(ResourceHelper.spark, "src/test/resources/conll2003/eng.train")
val embeddings = AlbertEmbeddings
.pretrained()
.setInputCols("sentence", "token")
.setOutputCol("embeddings")
.setMaxSentenceLength(512)
val pipeline = new Pipeline()
.setStages(Array(embeddings))
val pipelineDF = pipeline.fit(training_data).transform(training_data)
Benchmark.time("Time to save AlbertEmbeddings results") {
pipelineDF.write.mode("overwrite").parquet("./tmp_bert_embeddings")
}
Benchmark.time("Time to finish checking counts in results") {
println("missing tokens/embeddings: ")
pipelineDF
.withColumn("sentence_size", size(col("sentence")))
.withColumn("token_size", size(col("token")))
.withColumn("embed_size", size(col("embeddings")))
.where(col("token_size") =!= col("embed_size"))
.select("sentence_size", "token_size", "embed_size")
.show(false)
}
Benchmark.time("Time to finish explod/count in results") {
println("total sentences: ", pipelineDF.select(explode($"sentence.result")).count)
val totalTokens = pipelineDF.select(explode($"token.result")).count.toInt
val totalEmbeddings = pipelineDF.select(explode($"embeddings.embeddings")).count.toInt
println(s"total tokens: $totalTokens")
println(s"total embeddings: $totalEmbeddings")
// it is normal that the embeddings is less than total tokens in a sentence/document
// tokens generate multiple sub-wrods or pieces which won't be included in the final results
assert(totalTokens >= totalEmbeddings)
}
}
"AlbertEmbeddings" should "be aligned with custom tokens from Tokenizer" taggedAs SlowTest in {
import ResourceHelper.spark.implicits._
val ddd = Seq(
"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 . . .",
"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 embeddings = AlbertEmbeddings
.pretrained()
.setInputCols("document", "token")
.setOutputCol("embeddings")
.setMaxSentenceLength(512)
val pipeline = new Pipeline().setStages(Array(document, tokenizer, embeddings))
val pipelineModel = pipeline.fit(ddd)
val pipelineDF = pipelineModel.transform(ddd)
pipelineDF.select("token").show(false)
pipelineDF.select("embeddings.result").show(false)
pipelineDF
.withColumn("token_size", size(col("token")))
.withColumn("embed_size", size(col("embeddings")))
.where(col("token_size") =!= col("embed_size"))
.select("token_size", "embed_size", "token.result", "embeddings.result")
.show(false)
val totalTokens = pipelineDF.select(explode($"token.result")).count.toInt
val totalEmbeddings = pipelineDF.select(explode($"embeddings.embeddings")).count.toInt
println(s"total tokens: $totalTokens")
println(s"total embeddings: $totalEmbeddings")
assert(totalTokens == totalEmbeddings)
}
}