I have a tflite version of the hugging face model sentence-transformers/all-MiniLM-L6-v2…
But i dont know how to shape the input and output tensor…basically the model gives encoding of a sentence but also does multiple other thigs…I just want to use it for encoding
When I added the model to ml folder of android studio it gave me the below code for running the model by analyzing it…
val model = Model.newInstance(context)
// Creates inputs for reference.
val inputFeature0 = TensorBuffer.createFixedSize(intArrayOf(1, 128), DataType.INT64)
inputFeature0.loadBuffer(byteBuffer)
val inputFeature1 = TensorBuffer.createFixedSize(intArrayOf(1, 128), DataType.INT64)
inputFeature1.loadBuffer(byteBuffer)
val inputFeature2 = TensorBuffer.createFixedSize(intArrayOf(1, 128), DataType.INT64)
inputFeature2.loadBuffer(byteBuffer)
// Runs model inference and gets result.
val outputs = model.process(inputFeature0, inputFeature1, inputFeature2)
val outputFeature0 = outputs.outputFeature0AsTensorBuffer
// Releases model resources if no longer used.
model.close()
But when I run this code it gives me error r: tensorflow/lite/kernels/gather.cc:159 indices_has_only_positive_elements was not true.
I actually have no clue as to how should i use the model to get encodings…Any advice would help
Below is the code I am using…
private val tflite: Interpreter
init {
// Initialize the TFLite interpreter with the model
tflite = Interpreter(loadModelFile())
// Get the output tensor
val outputTensor = tflite.getOutputTensor(0)
// Get the output tensor type
// val outputTensorType = tflite.getTensorInfo(outputTensor).dataType
}
private fun loadModelFile(): ByteBuffer {
val modelPath = "model.tflite"
val assetManager = c.assets
val inputStream = assetManager.open(modelPath)
val fileDescriptor = assetManager.openFd(modelPath)
val startOffset = fileDescriptor.startOffset
val declaredLength = fileDescriptor.declaredLength
val fileChannel = FileInputStream(fileDescriptor.fileDescriptor).channel
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength)
}
fun computeCosineSimilarity(sentence1: String, sentence2: String): Double {
// Convert the sentences to LongArrays
val sentence1Embedding = computeEmbedding(sentence1)
val sentence2Embedding = computeEmbedding(sentence2)
// Compute the cosine similarity between the sentence embeddings
val cosineSimilarity = cosineSimilarity(sentence1Embedding, sentence2Embedding)
return cosineSimilarity
}
fun computeEmbedding(sentence: String): FloatArray {
val model = Model.newInstance(c)
val padded_sentence = sentence.padEnd(4 * sentence.length, ' ')
// var byteBuffer = ByteBuffer.allocate(1024)
// byteBuffer = ByteBuffer.wrap(padded_sentence.toByteArray())
val byteBuffer = ByteBuffer.allocate(1024).put(padded_sentence.toByteArray())
// Creates inputs for reference.
val inputFeature0 = TensorBuffer.createFixedSize(intArrayOf(1, 256), DataType.FLOAT32)
inputFeature0.loadBuffer(byteBuffer)
val inputFeature1 = TensorBuffer.createFixedSize(intArrayOf(1, 256), DataType.FLOAT32)
inputFeature1.loadBuffer(byteBuffer)
val inputFeature2 = TensorBuffer.createFixedSize(intArrayOf(1, 256), DataType.FLOAT32)
inputFeature2.loadBuffer(byteBuffer)
// Runs model inference and gets result.
val outputs = model.process(inputFeature0, inputFeature1, inputFeature2)
val outputFeature0 = outputs.outputFeature0AsTensorBuffer
// Extracts the embedding from the output.
val embedding = outputFeature0.floatArray
// Releases model resources if no longer used.
model.close()
return embedding
}}
here is the sentence_transformer model link : sentence-transformers/all-MiniLM-L6-v2 · Hugging Face