mandarjoshi/trivia_qa
Viewer • Updated • 848k • 87.7k • 193
How to use textualization/all-distilroberta-v1 with sentence-transformers:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("textualization/all-distilroberta-v1")
sentences = [
"That is a happy person",
"That is a happy dog",
"That is a very happy person",
"Today is a sunny day"
]
embeddings = model.encode(sentences)
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [4, 4]This is a ONNX export of sentence-transformers/all-distilroberta-v1.
The export was done using HF Optimum:
from optimum.exporters.onnx import main_export
main_export('sentence-transformers/all-distilroberta-v1', "./output", cache_dir='./cache', optimize='O1')
Please note, this ONNX model does not contain the mean pooling layer, it needs to be done in code afterwards or the embeddings won't work.
Code like this:
#Mean Pooling - Take attention mask into account for correct averaging
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
See the example code from the original model in the "Usage (HuggingFace Transformers)" section.