hooman650 commited on
Commit
aa0b863
1 Parent(s): 4208518

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +42 -0
README.md ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ language:
4
+ - en
5
+ library_name: transformers
6
+ ---
7
+ # PubMedBERT Embeddings Matryoshka - ONNX - O4
8
+
9
+ O4 optimized weights of [`NeuML/pubmedbert-base-embeddings-matryoshka`](https://huggingface.co/NeuML/pubmedbert-base-embeddings-matryoshka).
10
+
11
+ ```python
12
+ from optimum.onnxruntime import ORTModelForFeatureExtraction
13
+ from transformers import AutoTokenizer
14
+ import torch
15
+
16
+ # Mean Pooling - Take attention mask into account for correct averaging
17
+ def meanpooling(output, mask):
18
+ embeddings = output[0] # First element of model_output contains all token embeddings
19
+ mask = mask.unsqueeze(-1).expand(embeddings.size()).float()
20
+ return torch.sum(embeddings * mask, 1) / torch.clamp(mask.sum(1), min=1e-9)
21
+
22
+ # Sentences we want sentence embeddings for
23
+ sentences = ['This is an example sentence', 'Each sentence is converted']
24
+
25
+ model = ORTModelForFeatureExtraction.from_pretrained("hooman650/pubmedbert-base-embeddings-matryoshka-onnx-04",provider="CUDAExecutionProvider")
26
+ tokenizer = AutoTokenizer.from_pretrained("hooman650/pubmedbert-base-embeddings-matryoshka-onnx-04")
27
+
28
+ # Tokenize sentences
29
+ inputs = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
30
+
31
+ # Compute token embeddings
32
+ with torch.no_grad():
33
+ output = model(**inputs)
34
+
35
+ # Perform pooling. In this case, mean pooling.
36
+ embeddings = meanpooling(output, inputs['attention_mask'])
37
+
38
+ # Requested matryoshka dimensions
39
+ dimensions = 256
40
+
41
+ print("Sentence embeddings:")
42
+ print(embeddings[:, :dimensions])