How to get token_type_ids and attention_mask and get the result text? #32572
Replies: 3 comments
|
There are two separate issues here: constructing the model inputs and choosing a model that can produce the answer you want. In the linked repository's tensor-building loop, // idTensor and maskTensor are separate tensors with matching [1, length] shapes.
for (int i = 0; i < encodings.Count; i++)
{
idTensor[0, i] = encodings[i];
maskTensor[0, i] = 1;
}Ensure Before adding inputs, inspect If For questions about a text file, you can use MiniLM to embed chunks of the file and retrieve chunks similar to the question. A separate generative model can then answer using those chunks, or your app can simply display the retrieved passages. Could you share the model's original download/export link and the input/output metadata? That would establish the exact inputs and whether pooling is already included. |
|
thank you.
"this is a sample" to input_ids only have 4 I have to add other 0 to the input_ids? yes
I am sorry the model's original download link is in other repos and I believe it is download in huggingface. and input/output metadata where can I copy it? I have a vocab.txt. thank you again . I wonder if there is any chance give me an example? thank you. |
|
Hi ljzj2, No padding needed when you run one sentence at a time. Just make all three inputs the same length as the id list. For the model itself: I opened the all-MiniLM-L6-v2 onnx file, inputs are MiniLM is an embedding model, it won't write an answer. The usual way to use it on a txt file: split the file into chunks (paragraphs), embed each chunk once, embed the question, and show the chunks with the highest cosine similarity. Something like this: float[] Embed(string text)
{
var ids = tokenizer.EncodeToIds(text); // [CLS] ... [SEP] included
int n = ids.Count;
var inputIds = new DenseTensor<long>(new[] { 1, n });
var mask = new DenseTensor<long>(new[] { 1, n });
var typeIds = new DenseTensor<long>(new[] { 1, n });
for (int i = 0; i < n; i++) { inputIds[0, i] = ids[i]; mask[0, i] = 1; typeIds[0, i] = 0; }
using var results = _session.Run(new[] {
NamedOnnxValue.CreateFromTensor("input_ids", inputIds),
NamedOnnxValue.CreateFromTensor("attention_mask", mask),
NamedOnnxValue.CreateFromTensor("token_type_ids", typeIds),
});
var hidden = results.First().AsTensor<float>(); // [1, n, 384]
// mean pooling over tokens, then L2 normalize
var emb = new float[384];
for (int i = 0; i < n; i++)
for (int d = 0; d < 384; d++) emb[d] += hidden[0, i, d];
float norm = 0;
for (int d = 0; d < 384; d++) { emb[d] /= n; norm += emb[d] * emb[d]; }
norm = MathF.Sqrt(norm);
for (int d = 0; d < 384; d++) emb[d] /= norm;
return emb;
}Since the vectors are normalized, cosine similarity is just the dot product of the two arrays. Pick the top 2-3 chunks for the question and display them. If you want a generated answer on top of that you need a second, generative model, MiniLM can't do it. |
Uh oh!
There was an error while loading. Please reload this page.
I am using android application 10
I have get InferenceSession and tokenizer but I searched that I have to get token_type_ids and attention_mask but there is
EncodingToIdsthere is no token_type_ids.
what I want
1.ask questions about my txt file.there is the repo
https://github.com/ljzj2/XMedicalAndroidAll reactions