shibing624
commited on
Commit
•
6e062de
1
Parent(s):
c6e875c
Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,136 @@
|
|
1 |
---
|
|
|
2 |
license: apache-2.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
pipeline_tag: sentence-similarity
|
3 |
license: apache-2.0
|
4 |
+
tags:
|
5 |
+
- text2vec
|
6 |
+
- feature-extraction
|
7 |
+
- sentence-similarity
|
8 |
+
- transformers
|
9 |
+
datasets:
|
10 |
+
- https://huggingface.co/datasets/shibing624/nli-zh-all/tree/main/text2vec-base-chinese-paraphrase-dataset
|
11 |
+
language:
|
12 |
+
- zh
|
13 |
+
metrics:
|
14 |
+
- spearmanr
|
15 |
+
library_name: transformers
|
16 |
---
|
17 |
+
# shibing624/text2vec-base-chinese-paraphrase
|
18 |
+
This is a CoSENT(Cosine Sentence) model: shibing624/text2vec-base-chinese-paraphrase.
|
19 |
+
|
20 |
+
It maps sentences to a 768 dimensional dense vector space and can be used for tasks
|
21 |
+
like sentence embeddings, text matching or semantic search.
|
22 |
+
|
23 |
+
- training dataset: https://huggingface.co/datasets/shibing624/nli-zh-all/tree/main/text2vec-base-chinese-paraphrase-dataset
|
24 |
+
- base model: nghuyong/ernie-3.0-base-zh
|
25 |
+
- max_seq_length: 256
|
26 |
+
- best epoch: 3
|
27 |
+
- sentence embedding dim: 768
|
28 |
+
|
29 |
+
## Evaluation
|
30 |
+
For an automated evaluation of this model, see the *Evaluation Benchmark*: [text2vec](https://github.com/shibing624/text2vec)
|
31 |
+
|
32 |
+
### Release Models
|
33 |
+
- 本项目release模型的中文匹配评测结果:
|
34 |
+
|
35 |
+
| Arch | BaseModel | Model | ATEC | BQ | LCQMC | PAWSX | STS-B | SOHU-dd | SOHU-dc | Avg | QPS |
|
36 |
+
|:-----------|:----------------------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------|:-----:|:-----:|:-----:|:-----:|:-----:|:-------:|:-------:|:---------:|:-----:|
|
37 |
+
| Word2Vec | word2vec | [w2v-light-tencent-chinese](https://ai.tencent.com/ailab/nlp/en/download.html) | 20.00 | 31.49 | 59.46 | 2.57 | 55.78 | 55.04 | 20.70 | 35.03 | 23769 |
|
38 |
+
| SBERT | xlm-roberta-base | [sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2](https://huggingface.co/sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2) | 18.42 | 38.52 | 63.96 | 10.14 | 78.90 | 63.01 | 52.28 | 46.46 | 3138 |
|
39 |
+
| Instructor | hfl/chinese-roberta-wwm-ext | [moka-ai/m3e-base](https://huggingface.co/moka-ai/m3e-base) | 41.27 | 63.81 | 74.87 | 12.20 | 76.96 | 75.83 | 60.55 | 57.93 | 2980 |
|
40 |
+
| CoSENT | hfl/chinese-macbert-base | [shibing624/text2vec-base-chinese](https://huggingface.co/shibing624/text2vec-base-chinese) | 31.93 | 42.67 | 70.16 | 17.21 | 79.30 | 70.27 | 50.42 | 51.61 | 3008 |
|
41 |
+
| CoSENT | hfl/chinese-lert-large | [GanymedeNil/text2vec-large-chinese](https://huggingface.co/GanymedeNil/text2vec-large-chinese) | 32.61 | 44.59 | 69.30 | 14.51 | 79.44 | 73.01 | 59.04 | 53.12 | 2092 |
|
42 |
+
| CoSENT | nghuyong/ernie-3.0-base-zh | [shibing624/text2vec-base-chinese-sentence](https://huggingface.co/shibing624/text2vec-base-chinese-sentence) | 51.26 | 68.72 | 79.13 | 34.28 | 80.70 | 70.34 | 54.91 | 60.09 | 3066 |
|
43 |
+
| CoSENT | nghuyong/ernie-3.0-base-zh | [shibing624/text2vec-base-chinese-paraphrase](https://huggingface.co/shibing624/text2vec-base-chinese-paraphrase) | 44.89 | 63.58 | 74.24 | 40.90 | 78.93 | 76.70 | 63.30 | **63.08** | 3066 |
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
## Usage (text2vec)
|
48 |
+
Using this model becomes easy when you have [text2vec](https://github.com/shibing624/text2vec) installed:
|
49 |
+
|
50 |
+
```
|
51 |
+
pip install -U text2vec
|
52 |
+
```
|
53 |
+
|
54 |
+
Then you can use the model like this:
|
55 |
+
|
56 |
+
```python
|
57 |
+
from text2vec import SentenceModel
|
58 |
+
sentences = ['如何更换花呗绑定银行卡', '花呗更改绑定银行卡']
|
59 |
+
model = SentenceModel('shibing624/text2vec-base-chinese-paraphrase')
|
60 |
+
embeddings = model.encode(sentences)
|
61 |
+
print(embeddings)
|
62 |
+
```
|
63 |
+
|
64 |
+
## Usage (HuggingFace Transformers)
|
65 |
+
Without [text2vec](https://github.com/shibing624/text2vec), you can use the model like this:
|
66 |
+
|
67 |
+
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.
|
68 |
+
|
69 |
+
Install transformers:
|
70 |
+
```
|
71 |
+
pip install transformers
|
72 |
+
```
|
73 |
+
|
74 |
+
Then load model and predict:
|
75 |
+
```python
|
76 |
+
from transformers import BertTokenizer, BertModel
|
77 |
+
import torch
|
78 |
+
# Mean Pooling - Take attention mask into account for correct averaging
|
79 |
+
def mean_pooling(model_output, attention_mask):
|
80 |
+
token_embeddings = model_output[0] # First element of model_output contains all token embeddings
|
81 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
82 |
+
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
83 |
+
# Load model from HuggingFace Hub
|
84 |
+
tokenizer = BertTokenizer.from_pretrained('shibing624/text2vec-base-chinese-paraphrase')
|
85 |
+
model = BertModel.from_pretrained('shibing624/text2vec-base-chinese-paraphrase')
|
86 |
+
sentences = ['如何更换花呗绑定银行卡', '花呗更改绑定银行卡']
|
87 |
+
# Tokenize sentences
|
88 |
+
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
|
89 |
+
# Compute token embeddings
|
90 |
+
with torch.no_grad():
|
91 |
+
model_output = model(**encoded_input)
|
92 |
+
# Perform pooling. In this case, mean pooling.
|
93 |
+
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
94 |
+
print("Sentence embeddings:")
|
95 |
+
print(sentence_embeddings)
|
96 |
+
```
|
97 |
+
|
98 |
+
## Usage (sentence-transformers)
|
99 |
+
[sentence-transformers](https://github.com/UKPLab/sentence-transformers) is a popular library to compute dense vector representations for sentences.
|
100 |
+
|
101 |
+
Install sentence-transformers:
|
102 |
+
```
|
103 |
+
pip install -U sentence-transformers
|
104 |
+
```
|
105 |
+
|
106 |
+
Then load model and predict:
|
107 |
+
|
108 |
+
```python
|
109 |
+
from sentence_transformers import SentenceTransformer
|
110 |
+
m = SentenceTransformer("shibing624/text2vec-base-chinese-paraphrase")
|
111 |
+
sentences = ['如何更换花呗绑定银行卡', '花呗更改绑定银行卡']
|
112 |
+
sentence_embeddings = m.encode(sentences)
|
113 |
+
print("Sentence embeddings:")
|
114 |
+
print(sentence_embeddings)
|
115 |
+
```
|
116 |
+
|
117 |
+
|
118 |
+
## Full Model Architecture
|
119 |
+
```
|
120 |
+
CoSENT(
|
121 |
+
(0): Transformer({'max_seq_length': 256, 'do_lower_case': False}) with Transformer model: BertModel
|
122 |
+
(1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_mean_tokens': True})
|
123 |
+
)
|
124 |
+
```
|
125 |
+
## Citing & Authors
|
126 |
+
This model was trained by [text2vec](https://github.com/shibing624/text2vec).
|
127 |
+
|
128 |
+
If you find this model helpful, feel free to cite:
|
129 |
+
```bibtex
|
130 |
+
@software{text2vec,
|
131 |
+
author = {Ming Xu},
|
132 |
+
title = {text2vec: A Tool for Text to Vector},
|
133 |
+
year = {2023},
|
134 |
+
url = {https://github.com/shibing624/text2vec},
|
135 |
+
}
|
136 |
+
```
|