Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
pipeline_tag: sentence-similarity
|
3 |
+
language: en
|
4 |
+
tags:
|
5 |
+
- sentence-similarity
|
6 |
+
- transformers
|
7 |
+
- Education
|
8 |
+
- en
|
9 |
+
- bert
|
10 |
+
- sentence-transformers
|
11 |
+
- feature-extraction
|
12 |
+
- xnli
|
13 |
+
- stsb_multi_mt
|
14 |
+
datasets:
|
15 |
+
- xnli
|
16 |
+
- stsb_multi_mt
|
17 |
+
---
|
18 |
+
|
19 |
+
# inokufu/bertheo-en
|
20 |
+
|
21 |
+
A [sentence-transformers](https://www.SBERT.net) model fine-tuned on course sentences. It maps sentences & paragraphs to a 768 dimensional dense vector space and can be used for tasks like clustering or semantic search.
|
22 |
+
|
23 |
+
## Details
|
24 |
+
|
25 |
+
This model is based on the English bert-base-uncased pre-trained model [1, 2].
|
26 |
+
|
27 |
+
It was first fine-tuned on our learning object (LO) sentences dataset. This dataset consists of a sample of 500k sentences of course descriptions. We used standard parameter settings for fine-tuning as mentioned in the original BERT paper [2]. This allows the model to improve its performance on the target task (Masked Language Model) for domain-specific sentences.
|
28 |
+
|
29 |
+
It was then fine-tuned on a natural language inference task (XNLI) [3]. This task consists in training the model to recognize relations between sentences (contradiction, neutral, implication).
|
30 |
+
|
31 |
+
It was then fine-tuned on a text semantic similarity task (on STS data) [4]. This task consists in training the model to estimate the similarity between two sentences.
|
32 |
+
|
33 |
+
This fine-tuning process allows our model to have a semantic representation of words that is much better than the one proposed by the base model.
|
34 |
+
|
35 |
+
## Usage (Sentence-Transformers)
|
36 |
+
|
37 |
+
Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:
|
38 |
+
|
39 |
+
```
|
40 |
+
pip install -U sentence-transformers
|
41 |
+
```
|
42 |
+
|
43 |
+
Then you can use the model like this:
|
44 |
+
|
45 |
+
```python
|
46 |
+
from sentence_transformers import SentenceTransformer
|
47 |
+
sentences = ["Learn to code in python", "Become an expert in accounting"]
|
48 |
+
|
49 |
+
model = SentenceTransformer('inokufu/bert-base-uncased-xnli-sts-finetuned-education')
|
50 |
+
embeddings = model.encode(sentences)
|
51 |
+
print(embeddings)
|
52 |
+
```
|
53 |
+
|
54 |
+
|
55 |
+
|
56 |
+
## Usage (HuggingFace Transformers)
|
57 |
+
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.
|
58 |
+
|
59 |
+
```python
|
60 |
+
from transformers import AutoTokenizer, AutoModel
|
61 |
+
import torch
|
62 |
+
|
63 |
+
|
64 |
+
#Mean Pooling - Take attention mask into account for correct averaging
|
65 |
+
def mean_pooling(model_output, attention_mask):
|
66 |
+
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
67 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
68 |
+
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
69 |
+
|
70 |
+
|
71 |
+
# Sentences we want sentence embeddings for
|
72 |
+
sentences = ["Learn to code in python", "Become an expert in accounting"]
|
73 |
+
|
74 |
+
# Load model from HuggingFace Hub
|
75 |
+
tokenizer = AutoTokenizer.from_pretrained('inokufu/bert-base-uncased-xnli-sts-finetuned-education')
|
76 |
+
model = AutoModel.from_pretrained('inokufu/bert-base-uncased-xnli-sts-finetuned-education')
|
77 |
+
|
78 |
+
# Tokenize sentences
|
79 |
+
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
|
80 |
+
|
81 |
+
# Compute token embeddings
|
82 |
+
with torch.no_grad():
|
83 |
+
model_output = model(**encoded_input)
|
84 |
+
|
85 |
+
# Perform pooling. In this case, mean pooling.
|
86 |
+
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
87 |
+
|
88 |
+
print("Sentence embeddings:")
|
89 |
+
print(sentence_embeddings)
|
90 |
+
```
|
91 |
+
|
92 |
+
## Evaluation Results
|
93 |
+
|
94 |
+
STS (en) score: 84.61%
|
95 |
+
|
96 |
+
|
97 |
+
## Model Architecture
|
98 |
+
```
|
99 |
+
SentenceTransformer(
|
100 |
+
(0): Transformer({'max_seq_length': 512, 'do_lower_case': True}) with Transformer model: BertModel
|
101 |
+
(1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})
|
102 |
+
)
|
103 |
+
```
|
104 |
+
|
105 |
+
## References
|
106 |
+
|
107 |
+
[1] https://huggingface.co/bert-base-uncased <br>
|
108 |
+
[2] https://arxiv.org/abs/1810.04805 <br>
|
109 |
+
[3] https://arxiv.org/abs/1809.05053 <br>
|
110 |
+
[4] https://huggingface.co/datasets/stsb_multi_mt <br>
|