ssmits commited on
Commit
b1638d5
·
verified ·
1 Parent(s): b703267

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +38 -2
README.md CHANGED
@@ -27,7 +27,7 @@ The 'lm_head' layer of this model has been removed, which means it can be used f
27
  Additionaly, in stead of a normalization layer, the hidden layers are followed up by both a classical weight and bias 1-dimensional array of 4096 values.
28
  The basic Sentence-Transformers implementation is working correctly. This would imply other more sophisticated embeddings techniques such as adding a custom classification head, will work correctly as well.
29
 
30
- ## Inference
31
  ```python
32
  from sentence_transformers import SentenceTransformer
33
  import torch
@@ -56,4 +56,40 @@ print(similarities)
56
  # [0.5937, 0.5925, 1.0000]])
57
  ```
58
 
59
- Note: In my tests it utilizes more than 24GB (RTX 4090), so an A100 or A6000 would be required for inference.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  Additionaly, in stead of a normalization layer, the hidden layers are followed up by both a classical weight and bias 1-dimensional array of 4096 values.
28
  The basic Sentence-Transformers implementation is working correctly. This would imply other more sophisticated embeddings techniques such as adding a custom classification head, will work correctly as well.
29
 
30
+ ## Inference (sentence-transformers)
31
  ```python
32
  from sentence_transformers import SentenceTransformer
33
  import torch
 
56
  # [0.5937, 0.5925, 1.0000]])
57
  ```
58
 
59
+ Note: In my tests it utilizes more than 24GB (RTX 4090), so an A100 or A6000 would be required for inference.
60
+
61
+ ## Inference (HuggingFace Transformers)
62
+ Without sentence-transformers, you can use the model like this: First, you pass your input through the transformer model, then you have to apply the right pooling-operation on-top of the contextualized word embeddings.
63
+
64
+ ```python
65
+ from transformers import AutoTokenizer, AutoModel
66
+ import torch
67
+
68
+
69
+ #Mean Pooling - Take attention mask into account for correct averaging
70
+ def mean_pooling(model_output, attention_mask):
71
+ token_embeddings = model_output[0] #First element of model_output contains all token embeddings
72
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
73
+ return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
74
+
75
+
76
+ # Sentences we want sentence embeddings for
77
+ sentences = ['This is an example sentence', 'Each sentence is converted']
78
+
79
+ # Load model from HuggingFace Hub
80
+ tokenizer = AutoTokenizer.from_pretrained('ssmits/Falcon2-5.5B-multilingual-embed-base')
81
+ model = AutoModel.from_pretrained('ssmits/Falcon2-5.5B-multilingual-embed-base') # device = "cpu" when <= 24 GB VRAM
82
+
83
+ # Tokenize sentences
84
+ encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
85
+
86
+ # Compute token embeddings
87
+ with torch.no_grad():
88
+ model_output = model(**encoded_input)
89
+
90
+ # Perform pooling. In this case, mean pooling.
91
+ sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
92
+
93
+ print("Sentence embeddings:")
94
+ print(sentence_embeddings)
95
+ ```