-
Notifications
You must be signed in to change notification settings - Fork 717
/
XXXForClassification.scala
442 lines (376 loc) · 15.6 KB
/
XXXForClassification.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
* 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.ml.ai
import com.johnsnowlabs.ml.util.TensorFlow
import com.johnsnowlabs.nlp.annotators.common._
import com.johnsnowlabs.nlp.{ActivationFunction, Annotation, AnnotatorType}
private[johnsnowlabs] trait XXXForClassification {
protected val sentencePadTokenId: Int
protected val sentenceStartTokenId: Int
protected val sentenceEndTokenId: Int
protected val sigmoidThreshold: Float
def predict(
tokenizedSentences: Seq[TokenizedSentence],
batchSize: Int,
maxSentenceLength: Int,
caseSensitive: Boolean,
tags: Map[String, Int]): Seq[Annotation] = {
val wordPieceTokenizedSentences =
tokenizeWithAlignment(tokenizedSentences, maxSentenceLength, caseSensitive)
/*Run calculation by batches*/
wordPieceTokenizedSentences.zipWithIndex
.grouped(batchSize)
.flatMap { batch =>
val encoded = encode(batch, maxSentenceLength)
val logits = tag(encoded)
/*Combine tokens and calculated logits*/
batch.zip(logits).flatMap { case (sentence, tokenVectors) =>
val tokenLength = sentence._1.tokens.length
/*All wordpiece logits*/
val tokenLogits: Array[Array[Float]] = tokenVectors.slice(1, tokenLength + 1)
val labelsWithScores = wordAndSpanLevelAlignmentWithTokenizer(
tokenLogits,
tokenizedSentences,
sentence,
tags)
labelsWithScores
}
}
.toSeq
}
def predictSequence(
tokenizedSentences: Seq[TokenizedSentence],
sentences: Seq[Sentence],
batchSize: Int,
maxSentenceLength: Int,
caseSensitive: Boolean,
coalesceSentences: Boolean = false,
tags: Map[String, Int],
activation: String = ActivationFunction.softmax): Seq[Annotation] = {
val wordPieceTokenizedSentences =
tokenizeWithAlignment(tokenizedSentences, maxSentenceLength, caseSensitive)
/*Run calculation by batches*/
wordPieceTokenizedSentences
.zip(sentences)
.zipWithIndex
.grouped(batchSize)
.flatMap { batch =>
val tokensBatch = batch.map(x => (x._1._1, x._2))
val encoded = encode(tokensBatch, maxSentenceLength)
val logits = tagSequence(encoded, activation)
activation match {
case ActivationFunction.softmax =>
if (coalesceSentences) {
val scores = logits.transpose.map(_.sum / logits.length)
val label = scoresToLabelForSequenceClassifier(tags, scores)
val meta = constructMetaForSequenceClassifier(tags, scores)
Array(constructAnnotationForSequenceClassifier(sentences.head, label, meta))
} else {
sentences.zip(logits).map { case (sentence, scores) =>
val label = scoresToLabelForSequenceClassifier(tags, scores)
val meta = constructMetaForSequenceClassifier(tags, scores)
constructAnnotationForSequenceClassifier(sentence, label, meta)
}
}
case ActivationFunction.sigmoid =>
if (coalesceSentences) {
val scores = logits.transpose.map(_.sum / logits.length)
val labels = scores.zipWithIndex
.filter(x => x._1 > sigmoidThreshold)
.flatMap(x => tags.filter(_._2 == x._2))
val meta = constructMetaForSequenceClassifier(tags, scores)
labels.map(label =>
constructAnnotationForSequenceClassifier(sentences.head, label._1, meta))
} else {
sentences.zip(logits).flatMap { case (sentence, scores) =>
val labels = scores.zipWithIndex
.filter(x => x._1 > sigmoidThreshold)
.flatMap(x => tags.filter(_._2 == x._2))
val meta = constructMetaForSequenceClassifier(tags, scores)
labels.map(label =>
constructAnnotationForSequenceClassifier(sentence, label._1, meta))
}
}
}
}
.toSeq
}
def predictSequenceWithZeroShot(
tokenizedSentences: Seq[TokenizedSentence],
sentences: Seq[Sentence],
candidateLabels: Array[String],
entailmentId: Int,
contradictionId: Int,
batchSize: Int,
maxSentenceLength: Int,
caseSensitive: Boolean,
coalesceSentences: Boolean = false,
tags: Map[String, Int],
activation: String = ActivationFunction.softmax): Seq[Annotation] = {
val wordPieceTokenizedSentences =
tokenizeWithAlignment(tokenizedSentences, maxSentenceLength, caseSensitive)
val candidateLabelsKeyValue = candidateLabels.zipWithIndex.toMap
val contradiction_id: Int = if (entailmentId == 0) contradictionId else 0
val labelsTokenized =
tokenizeSeqString(candidateLabels, maxSentenceLength, caseSensitive)
/*Run calculation by batches*/
wordPieceTokenizedSentences
.zip(sentences)
.zipWithIndex
.grouped(batchSize)
.flatMap { batch =>
val tokensBatch = batch.map(x => (x._1._1, x._2))
/* Start internal batching for zero shot */
val encodedTokensLabels = tokensBatch.map { sent =>
labelsTokenized.flatMap(labels =>
encodeSequence(Seq(sent._1), Seq(labels), maxSentenceLength))
}
val logits = encodedTokensLabels.map { encodedSeq =>
tagZeroShotSequence(encodedSeq, entailmentId, contradictionId, activation)
}
val multiClassScores =
logits.map(scores => calculateSoftmax(scores.map(x => x(entailmentId)))).toArray
val multiLabelScores =
logits
.map(scores =>
scores
.map(x => calculateSoftmax(Array(x(contradiction_id), x(entailmentId))))
.map(_.last))
.toArray
activation match {
case ActivationFunction.softmax =>
if (coalesceSentences) {
val scores = multiClassScores.transpose.map(_.sum / multiClassScores.length)
val label = scoresToLabelForSequenceClassifier(candidateLabelsKeyValue, scores)
val meta = constructMetaForSequenceClassifier(candidateLabelsKeyValue, scores)
Array(constructAnnotationForSequenceClassifier(sentences.head, label, meta))
} else {
sentences.zip(multiClassScores).map { case (sentence, scores) =>
val label = scoresToLabelForSequenceClassifier(candidateLabelsKeyValue, scores)
val meta = constructMetaForSequenceClassifier(candidateLabelsKeyValue, scores)
constructAnnotationForSequenceClassifier(sentence, label, meta)
}
}
case ActivationFunction.sigmoid =>
if (coalesceSentences) {
val scores = multiLabelScores.transpose.map(_.sum / multiLabelScores.length)
val labels = scores.zipWithIndex
.filter(x => x._1 > 0.5)
.flatMap(x => candidateLabelsKeyValue.filter(_._2 == x._2))
val meta = constructMetaForSequenceClassifier(candidateLabelsKeyValue, scores)
labels.map(label =>
constructAnnotationForSequenceClassifier(sentences.head, label._1, meta))
} else {
sentences.zip(multiLabelScores).flatMap { case (sentence, scores) =>
val labels = scores.zipWithIndex
.filter(x => x._1 > 0.5)
.flatMap(x => candidateLabelsKeyValue.filter(_._2 == x._2))
val meta = constructMetaForSequenceClassifier(candidateLabelsKeyValue, scores)
labels.map(label =>
constructAnnotationForSequenceClassifier(sentence, label._1, meta))
}
}
}
}
.toSeq
}
def scoresToLabelForSequenceClassifier(tags: Map[String, Int], scores: Array[Float]): String = {
tags.find(_._2 == scores.zipWithIndex.maxBy(_._1)._2).map(_._1).getOrElse("NA")
}
def constructMetaForSequenceClassifier(
tags: Map[String, Int],
scores: Array[Float]): Array[(String, String)] = {
scores.zipWithIndex.flatMap(x =>
Map(tags.find(_._2 == x._2).map(_._1).getOrElse("NA") -> x._1.toString))
}
def constructAnnotationForSequenceClassifier(
sentence: Sentence,
label: String,
meta: Array[(String, String)]): Annotation = {
Annotation(
annotatorType = AnnotatorType.CATEGORY,
begin = sentence.start,
end = sentence.end,
result = label,
metadata = Map("sentence" -> sentence.index.toString) ++ meta)
}
def predictSpan(
documents: Seq[Annotation],
maxSentenceLength: Int,
caseSensitive: Boolean,
mergeTokenStrategy: String = MergeTokenStrategy.vocab,
engine: String = TensorFlow.name): Seq[Annotation] = {
val questionAnnot = Seq(documents.head)
val contextAnnot = documents.drop(1)
val wordPieceTokenizedQuestion =
tokenizeDocument(questionAnnot, maxSentenceLength, caseSensitive)
val wordPieceTokenizedContext =
tokenizeDocument(contextAnnot, maxSentenceLength, caseSensitive)
val encodedInput =
encodeSequence(wordPieceTokenizedQuestion, wordPieceTokenizedContext, maxSentenceLength)
val (startLogits, endLogits) = tagSpan(encodedInput)
val startScores = startLogits.transpose.map(_.sum / startLogits.length)
val endScores = endLogits.transpose.map(_.sum / endLogits.length)
val startIndex = startScores.zipWithIndex.maxBy(_._1)
val endIndex = endScores.zipWithIndex.maxBy(_._1)
val offsetStartIndex = if (engine == TensorFlow.name) 2 else 1
val offsetEndIndex = if (engine == TensorFlow.name) 1 else 0
val allTokenPieces =
wordPieceTokenizedQuestion.head.tokens ++ wordPieceTokenizedContext.flatMap(x => x.tokens)
val decodedAnswer =
allTokenPieces.slice(startIndex._2 - offsetStartIndex, endIndex._2 - offsetEndIndex)
val content =
mergeTokenStrategy match {
case MergeTokenStrategy.vocab =>
decodedAnswer.filter(_.isWordStart).map(x => x.token).mkString(" ")
case MergeTokenStrategy.sentencePiece =>
val token = ""
decodedAnswer
.map(x =>
if (x.isWordStart) " " + token + x.token
else token + x.token)
.mkString("")
.trim
}
Seq(
Annotation(
annotatorType = AnnotatorType.CHUNK,
begin = 0,
end = if (content.isEmpty) 0 else content.length - 1,
result = content,
metadata = Map(
"sentence" -> "0",
"chunk" -> "0",
"start" -> startIndex._2.toString,
"start_score" -> startIndex._1.toString,
"end" -> endIndex._2.toString,
"end_score" -> endIndex._1.toString,
"score" -> ((startIndex._1 + endIndex._1) / 2).toString)))
}
def tokenizeWithAlignment(
sentences: Seq[TokenizedSentence],
maxSeqLength: Int,
caseSensitive: Boolean): Seq[WordpieceTokenizedSentence]
def tokenizeSeqString(
candidateLabels: Seq[String],
maxSeqLength: Int,
caseSensitive: Boolean): Seq[WordpieceTokenizedSentence]
def tokenizeDocument(
docs: Seq[Annotation],
maxSeqLength: Int,
caseSensitive: Boolean): Seq[WordpieceTokenizedSentence]
/** Encode the input sequence to indexes IDs adding padding where necessary */
def encode(
sentences: Seq[(WordpieceTokenizedSentence, Int)],
maxSequenceLength: Int): Seq[Array[Int]] = {
val maxSentenceLength =
Array(
maxSequenceLength - 2,
sentences.map { case (wpTokSentence, _) =>
wpTokSentence.tokens.length
}.max).min
sentences
.map { case (wpTokSentence, _) =>
val tokenPieceIds = wpTokSentence.tokens.map(t => t.pieceId)
val padding = Array.fill(maxSentenceLength - tokenPieceIds.length)(sentencePadTokenId)
Array(sentenceStartTokenId) ++ tokenPieceIds.take(maxSentenceLength) ++ Array(
sentenceEndTokenId) ++ padding
}
}
def encodeSequence(
seq1: Seq[WordpieceTokenizedSentence],
seq2: Seq[WordpieceTokenizedSentence],
maxSequenceLength: Int): Seq[Array[Int]] = {
val question = seq1
.flatMap { wpTokSentence =>
wpTokSentence.tokens.map(t => t.pieceId)
}
.toArray
.take(maxSequenceLength - 2) ++ Array(sentenceEndTokenId)
val context = seq2
.flatMap { wpTokSentence =>
wpTokSentence.tokens.map(t => t.pieceId)
}
.toArray
.take(maxSequenceLength - question.length - 2) ++ Array(sentenceEndTokenId)
Seq(Array(sentenceStartTokenId) ++ question ++ context)
}
def tag(batch: Seq[Array[Int]]): Seq[Array[Array[Float]]]
def tagSequence(batch: Seq[Array[Int]], activation: String): Array[Array[Float]]
def tagZeroShotSequence(
batch: Seq[Array[Int]],
entailmentId: Int,
contradictionId: Int,
activation: String): Array[Array[Float]]
def tagSpan(batch: Seq[Array[Int]]): (Array[Array[Float]], Array[Array[Float]])
/** Calculate softmax from returned logits
* @param scores
* logits output from output layer
* @return
*/
def calculateSoftmax(scores: Array[Float]): Array[Float] = {
val exp = scores.map(x => math.exp(x))
exp.map(x => x / exp.sum).map(_.toFloat)
}
/** Calculate sigmoid from returned logits
* @param scores
* logits output from output layer
* @return
*/
def calculateSigmoid(scores: Array[Float]): Array[Float] = {
scores.map(x => 1 / (1 + Math.exp(-x)).toFloat)
}
/** Word-level and span-level alignment with Tokenizer
* https://github.com/google-research/bert#tokenization
*
* ### Input orig_tokens = ["John", "Johanson", "'s", "house"] labels = ["NNP", "NNP", "POS",
* "NN"]
*
* # bert_tokens == ["[CLS]", "john", "johan", "##son", "'", "s", "house", "[SEP]"] #
* orig_to_tok_map == [1, 2, 4, 6]
*/
def wordAndSpanLevelAlignmentWithTokenizer(
tokenLogits: Array[Array[Float]],
tokenizedSentences: Seq[TokenizedSentence],
sentence: (WordpieceTokenizedSentence, Int),
tags: Map[String, Int]): Seq[Annotation] = {
val labelsWithScores =
sentence._1.tokens.zip(tokenLogits).flatMap { case (tokenPiece, scores) =>
val indexedToken = findIndexedToken(tokenizedSentences, sentence, tokenPiece)
indexedToken.map { token =>
val label =
tags.find(_._2 == scores.zipWithIndex.maxBy(_._1)._2).map(_._1).getOrElse("NA")
val meta = scores.zipWithIndex.flatMap(x =>
Map(tags.find(_._2 == x._2).map(_._1).getOrElse("NA") -> x._1.toString))
Annotation(
annotatorType = AnnotatorType.NAMED_ENTITY,
begin = token.begin,
end = token.end,
result = label,
metadata = Map("sentence" -> sentence._2.toString, "word" -> token.token) ++ meta)
}
}
labelsWithScores.toSeq
}
def findIndexedToken(
tokenizedSentences: Seq[TokenizedSentence],
sentence: (WordpieceTokenizedSentence, Int),
tokenPiece: TokenPiece): Option[IndexedToken]
}
object MergeTokenStrategy {
val vocab = "vocab"
val sentencePiece = "sp"
}