Upload README.md
Browse files
README.md
CHANGED
@@ -90,41 +90,6 @@ embeddings = model.encode(sentences)
|
|
90 |
print(embeddings)
|
91 |
```
|
92 |
|
93 |
-
### Usage (HuggingFace Transformers)
|
94 |
-
Without [sentence-transformers](https://www.SBERT.net), 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.
|
95 |
-
|
96 |
-
```python
|
97 |
-
from transformers import AutoTokenizer, AutoModel
|
98 |
-
import torch
|
99 |
-
|
100 |
-
|
101 |
-
# Mean Pooling - Take attention mask into account for correct averaging
|
102 |
-
def mean_pooling(model_output, attention_mask):
|
103 |
-
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
104 |
-
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
105 |
-
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
106 |
-
|
107 |
-
# Sentences we want sentence embeddings for
|
108 |
-
sentences = ["Kini olu ilu England", "Kini eranko ti o gbona julọ ni agbaye?"]
|
109 |
-
|
110 |
-
# Load model from HuggingFace Hub
|
111 |
-
tokenizer = AutoTokenizer.from_pretrained('0xnu/pmmlv2-fine-tuned-yoruba')
|
112 |
-
model = AutoModel.from_pretrained('0xnu/pmmlv2-fine-tuned-yoruba')
|
113 |
-
|
114 |
-
# Tokenize sentences
|
115 |
-
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
|
116 |
-
|
117 |
-
# Compute token embeddings
|
118 |
-
with torch.no_grad():
|
119 |
-
model_output = model(**encoded_input)
|
120 |
-
|
121 |
-
# Perform pooling. In this case, max pooling.
|
122 |
-
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
123 |
-
|
124 |
-
print("Sentence embeddings:")
|
125 |
-
print(sentence_embeddings)
|
126 |
-
```
|
127 |
-
|
128 |
### License
|
129 |
|
130 |
This project is licensed under the [MIT License](./LICENSE).
|
|
|
90 |
print(embeddings)
|
91 |
```
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
### License
|
94 |
|
95 |
This project is licensed under the [MIT License](./LICENSE).
|