Update README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# DeCLUTR-base
|
| 2 |
+
|
| 3 |
+
## Model description
|
| 4 |
+
|
| 5 |
+
The "DeCLUTR-base" model from our paper: [DeCLUTR: Deep Contrastive Learning for Unsupervised Textual Representations](https://arxiv.org/abs/2006.03659).
|
| 6 |
+
|
| 7 |
+
## Intended uses & limitations
|
| 8 |
+
|
| 9 |
+
The model is intended to be used as a universal sentence encoder, similar to [Google's Universal Sentence Encoder](https://tfhub.dev/google/universal-sentence-encoder/4) or [Sentence Transformers](https://github.com/UKPLab/sentence-transformers).
|
| 10 |
+
|
| 11 |
+
#### How to use
|
| 12 |
+
|
| 13 |
+
```python
|
| 14 |
+
import torch
|
| 15 |
+
from scipy.spatial.distance import cosine
|
| 16 |
+
|
| 17 |
+
from transformers import AutoModel, AutoTokenizer
|
| 18 |
+
|
| 19 |
+
# Load the model
|
| 20 |
+
tokenizer = AutoTokenizer.from_pretrained("johngiorgi/declutr-base")
|
| 21 |
+
model = AutoModel.from_pretrained("johngiorgi/declutr-base")
|
| 22 |
+
|
| 23 |
+
# Prepare some text to embed
|
| 24 |
+
text = [
|
| 25 |
+
"A smiling costumed woman is holding an umbrella.",
|
| 26 |
+
"A happy woman in a fairy costume holds an umbrella.",
|
| 27 |
+
]
|
| 28 |
+
inputs = tokenizer(text, padding=True, truncation=True, return_tensors="pt")
|
| 29 |
+
|
| 30 |
+
# Embed the text
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
sequence_output, _ = model(**inputs, output_hidden_states=False)
|
| 33 |
+
|
| 34 |
+
# Mean pool the token-level embeddings to get sentence-level embeddings
|
| 35 |
+
embeddings = torch.sum(
|
| 36 |
+
sequence_output * inputs["attention_mask"].unsqueeze(-1), dim=1
|
| 37 |
+
) / torch.clamp(torch.sum(inputs["attention_mask"], dim=1, keepdims=True), min=1e-9)
|
| 38 |
+
|
| 39 |
+
# Compute a semantic similarity via the cosine distance
|
| 40 |
+
semantic_sim = 1 - cosine(embeddings[0], embeddings[1])
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
### BibTeX entry and citation info
|
| 44 |
+
|
| 45 |
+
```bibtex
|
| 46 |
+
@article{Giorgi2020DeCLUTRDC,
|
| 47 |
+
title={DeCLUTR: Deep Contrastive Learning for Unsupervised Textual Representations},
|
| 48 |
+
author={John M Giorgi and Osvald Nitski and Gary D. Bader and Bo Wang},
|
| 49 |
+
journal={ArXiv},
|
| 50 |
+
year={2020},
|
| 51 |
+
volume={abs/2006.03659}
|
| 52 |
+
}
|
| 53 |
+
```
|