Update README.md
Browse files
README.md
CHANGED
@@ -1,6 +1,34 @@
|
|
1 |
---
|
|
|
2 |
tags:
|
3 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
model-index:
|
5 |
- name: all-mpnet-base-v2-negation
|
6 |
results:
|
@@ -733,4 +761,98 @@ model-index:
|
|
733 |
value: 84.77197321507954
|
734 |
- type: max_f1
|
735 |
value: 76.91440595175472
|
736 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
pipeline_tag: sentence-similarity
|
3 |
tags:
|
4 |
+
- sentence-transformers
|
5 |
+
- feature-extraction
|
6 |
+
- sentence-similarity
|
7 |
+
language: en
|
8 |
+
license: apache-2.0
|
9 |
+
datasets:
|
10 |
+
- s2orc
|
11 |
+
- flax-sentence-embeddings/stackexchange_xml
|
12 |
+
- ms_marco
|
13 |
+
- gooaq
|
14 |
+
- yahoo_answers_topics
|
15 |
+
- code_search_net
|
16 |
+
- search_qa
|
17 |
+
- eli5
|
18 |
+
- snli
|
19 |
+
- multi_nli
|
20 |
+
- wikihow
|
21 |
+
- natural_questions
|
22 |
+
- trivia_qa
|
23 |
+
- embedding-data/sentence-compression
|
24 |
+
- embedding-data/flickr30k-captions
|
25 |
+
- embedding-data/altlex
|
26 |
+
- embedding-data/simple-wiki
|
27 |
+
- embedding-data/QQP
|
28 |
+
- embedding-data/SPECTER
|
29 |
+
- embedding-data/PAQ_pairs
|
30 |
+
- embedding-data/WikiAnswers
|
31 |
+
- tum-nlp/cannot-dataset
|
32 |
model-index:
|
33 |
- name: all-mpnet-base-v2-negation
|
34 |
results:
|
|
|
761 |
value: 84.77197321507954
|
762 |
- type: max_f1
|
763 |
value: 76.91440595175472
|
764 |
+
---
|
765 |
+
|
766 |
+
# all-mpnet-base-v2-negation
|
767 |
+
This is a fine-tuned [sentence-transformers](https://www.SBERT.net) model to perform better with negated pairs of sentences.
|
768 |
+
|
769 |
+
It maps sentences and paragraphs to a 768 dimensional dense vector space and can be used for tasks like clustering or semantic search.
|
770 |
+
|
771 |
+
## Usage (Sentence-Transformers)
|
772 |
+
Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:
|
773 |
+
|
774 |
+
```
|
775 |
+
pip install -U sentence-transformers
|
776 |
+
```
|
777 |
+
|
778 |
+
Then you can use the model like this:
|
779 |
+
```python
|
780 |
+
from sentence_transformers import SentenceTransformer
|
781 |
+
sentences = ["This is an example sentence", "Each sentence is converted"]
|
782 |
+
|
783 |
+
model = SentenceTransformer('dmlls/all-mpnet-base-v2-negation')
|
784 |
+
embeddings = model.encode(sentences)
|
785 |
+
print(embeddings)
|
786 |
+
```
|
787 |
+
|
788 |
+
## Usage (HuggingFace Transformers)
|
789 |
+
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.
|
790 |
+
|
791 |
+
```python
|
792 |
+
from transformers import AutoTokenizer, AutoModel
|
793 |
+
import torch
|
794 |
+
import torch.nn.functional as F
|
795 |
+
|
796 |
+
#Mean Pooling - Take attention mask into account for correct averaging
|
797 |
+
def mean_pooling(model_output, attention_mask):
|
798 |
+
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
799 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
800 |
+
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
801 |
+
|
802 |
+
|
803 |
+
# Sentences we want sentence embeddings for
|
804 |
+
sentences = ['This is an example sentence', 'Each sentence is converted']
|
805 |
+
|
806 |
+
# Load model from HuggingFace Hub
|
807 |
+
tokenizer = AutoTokenizer.from_pretrained('dmlls/all-mpnet-base-v2-negation')
|
808 |
+
model = AutoModel.from_pretrained('dmlls/all-mpnet-base-v2-negation')
|
809 |
+
|
810 |
+
# Tokenize sentences
|
811 |
+
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
|
812 |
+
|
813 |
+
# Compute token embeddings
|
814 |
+
with torch.no_grad():
|
815 |
+
model_output = model(**encoded_input)
|
816 |
+
|
817 |
+
# Perform pooling
|
818 |
+
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
819 |
+
|
820 |
+
# Normalize embeddings
|
821 |
+
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
|
822 |
+
|
823 |
+
print("Sentence embeddings:")
|
824 |
+
print(sentence_embeddings)
|
825 |
+
```
|
826 |
+
|
827 |
+
------
|
828 |
+
|
829 |
+
## Background
|
830 |
+
|
831 |
+
This model was finetuned within the context of the [*This is not correct! Negation-aware Evaluation of Language Generation Systems*](https://arxiv.org/abs/2307.13989) paper.
|
832 |
+
|
833 |
+
|
834 |
+
## Intended uses
|
835 |
+
|
836 |
+
Our model is intended to be used as a sentence and short paragraph encoder, performing well (i.e., reporting lower similarity scores) on negated pairs of sentences when compared to its base model.
|
837 |
+
|
838 |
+
Given an input text, it outputs a vector which captures the semantic information. The sentence vector may be used for information retrieval, clustering or sentence similarity tasks.
|
839 |
+
|
840 |
+
By default, input text longer than 384 word pieces is truncated.
|
841 |
+
|
842 |
+
|
843 |
+
## Training procedure
|
844 |
+
|
845 |
+
### Pre-training
|
846 |
+
|
847 |
+
We used [`sentence-transformers/all-mpnet-base-v2`](https://huggingface.co/sentence-transformers/all-mpnet-base-v2) as base model.
|
848 |
+
|
849 |
+
### Fine-tuning
|
850 |
+
|
851 |
+
We fine-tuned the model on the [CANNOT dataset](https://huggingface.co/datasets/tum-nlp/cannot-dataset) using a contrastive objective. Formally, we compute the cosine similarity from each possible sentence pairs from the batch. We then apply the cross entropy loss by comparing with true pairs.
|
852 |
+
|
853 |
+
#### Hyper parameters
|
854 |
+
We followed an analogous approach to [how other Sentence Transformers were trained](https://github.com/UKPLab/sentence-transformers/blob/3e1929fddef16df94f8bc6e3b10598a98f46e62d/examples/training/nli/training_nli_v2.py).
|
855 |
+
|
856 |
+
We took the first 90% of samples from the CANNOT dataset as the training split.
|
857 |
+
|
858 |
+
We used a batch size of 64 and trained for 1 epoch.
|