Skip to content

Commit

Permalink
adding padded tokens (#14276)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedlone127 authored May 24, 2024
1 parent 3f59375 commit d083420
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/main/scala/com/johnsnowlabs/ml/ai/BertClassification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ private[johnsnowlabs] class BertClassification(
else TensorFlow.name
private val onnxSessionOptions: Map[String, String] = new OnnxSession().getSessionOptions

private def padArrayWithZeros(arr: Array[Int], maxLength: Int): Array[Int] = {
if (arr.length >= maxLength) {
arr
} else {
arr ++ Array.fill(maxLength - arr.length)(0)
}
}

def tokenizeWithAlignment(
sentences: Seq[TokenizedSentence],
maxSeqLength: Int,
Expand Down Expand Up @@ -419,11 +427,12 @@ private[johnsnowlabs] class BertClassification(
activation: String): Array[Array[Float]] = {

val maxSentenceLength = batch.map(encodedSentence => encodedSentence.length).max
val batchLength = batch.length
val paddedBatch = batch.map(arr => padArrayWithZeros(arr, maxSentenceLength))
val batchLength = paddedBatch.length

val rawScores = detectedEngine match {
case ONNX.name => computeZeroShotLogitsWithONNX(batch, maxSentenceLength)
case _ => computeZeroShotLogitsWithTF(batch, maxSentenceLength)
case ONNX.name => computeZeroShotLogitsWithONNX(paddedBatch, maxSentenceLength)
case _ => computeZeroShotLogitsWithTF(paddedBatch, maxSentenceLength)
}

val dim = rawScores.length / batchLength
Expand Down

0 comments on commit d083420

Please sign in to comment.