shibing624 commited on
Commit
c95594f
1 Parent(s): 2f6621c

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +134 -0
README.md CHANGED
@@ -1,3 +1,137 @@
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
+ - shibing624/nli_zh
11
+ language:
12
+ - zh
13
+ metrics:
14
+ - bleu
15
+ library_name: transformers
16
  ---
17
+ # shibing624/text2vec-base-chinese-nli
18
+ This is a CoSENT(Cosine Sentence) model: shibing624/text2vec-base-chinese-nli.
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
+ - using all 5 tasks' datasets, dataset: https://huggingface.co/datasets/shibing624/nli_zh
24
+ - base model: nghuyong/ernie-3.0-base-zh
25
+ - max_seq_length = 256
26
+ - best epoch: 2
27
+
28
+ ## Evaluation
29
+ For an automated evaluation of this model, see the *Evaluation Benchmark*: [text2vec](https://github.com/shibing624/text2vec)
30
+
31
+ - 本项目release模型的中文匹配评测结果:
32
+
33
+ | Arch | BaseModel | Model | ATEC | BQ | LCQMC | PAWSX | STS-B | Avg | QPS |
34
+ | :-- |:-----------------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------|:-----:|:-----:|:-----:|:-----:|:-----:|:---------:|:-----:|
35
+ | 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 | 33.86 | 23769 |
36
+ | 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 | 41.99 | 3138 |
37
+ | 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 | 48.25 | 3008 |
38
+ | 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 | 48.08 | 2092 |
39
+ | CoSENT | nghuyong/ernie-3.0-base-zh | [shibing624/text2vec-base-chinese-nli](https://huggingface.co/shibing624/text2vec-base-chinese-nli) | 51.26 | 68.72 | 79.13 | 34.28 | 80.70 | **62.81** | 3066 |
40
+
41
+
42
+ ## Usage (text2vec)
43
+ Using this model becomes easy when you have [text2vec](https://github.com/shibing624/text2vec) installed:
44
+
45
+ ```
46
+ pip install -U text2vec
47
+ ```
48
+
49
+ Then you can use the model like this:
50
+
51
+ ```python
52
+ from text2vec import SentenceModel
53
+ sentences = ['如何更换花呗绑定银行卡', '花呗更改绑定银行卡']
54
+
55
+ model = SentenceModel('shibing624/text2vec-base-chinese-nli')
56
+ embeddings = model.encode(sentences)
57
+ print(embeddings)
58
+ ```
59
+
60
+ ## Usage (HuggingFace Transformers)
61
+ Without [text2vec](https://github.com/shibing624/text2vec), you can use the model like this:
62
+
63
+ 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.
64
+
65
+ Install transformers:
66
+ ```
67
+ pip install transformers
68
+ ```
69
+
70
+ Then load model and predict:
71
+ ```python
72
+ from transformers import BertTokenizer, BertModel
73
+ import torch
74
+
75
+ # Mean Pooling - Take attention mask into account for correct averaging
76
+ def mean_pooling(model_output, attention_mask):
77
+ token_embeddings = model_output[0] # First element of model_output contains all token embeddings
78
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
79
+ return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
80
+
81
+ # Load model from HuggingFace Hub
82
+ tokenizer = BertTokenizer.from_pretrained('shibing624/text2vec-base-chinese-nli')
83
+ model = BertModel.from_pretrained('shibing624/text2vec-base-chinese-nli')
84
+ sentences = ['如何更换花呗绑定银行卡', '花呗更改绑定银行卡']
85
+ # Tokenize sentences
86
+ encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
87
+
88
+ # Compute token embeddings
89
+ with torch.no_grad():
90
+ model_output = model(**encoded_input)
91
+ # Perform pooling. In this case, mean pooling.
92
+ sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
93
+ print("Sentence embeddings:")
94
+ print(sentence_embeddings)
95
+ ```
96
+
97
+ ## Usage (sentence-transformers)
98
+ [sentence-transformers](https://github.com/UKPLab/sentence-transformers) is a popular library to compute dense vector representations for sentences.
99
+
100
+ Install sentence-transformers:
101
+ ```
102
+ pip install -U sentence-transformers
103
+ ```
104
+
105
+ Then load model and predict:
106
+
107
+ ```python
108
+ from sentence_transformers import SentenceTransformer
109
+
110
+ m = SentenceTransformer("shibing624/text2vec-base-chinese-nli")
111
+ sentences = ['如何更换花呗绑定银行卡', '花呗更改绑定银行卡']
112
+
113
+ sentence_embeddings = m.encode(sentences)
114
+ print("Sentence embeddings:")
115
+ print(sentence_embeddings)
116
+ ```
117
+
118
+
119
+ ## Full Model Architecture
120
+ ```
121
+ CoSENT(
122
+ (0): Transformer({'max_seq_length': 256, 'do_lower_case': False}) with Transformer model: BertModel
123
+ (1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_mean_tokens': True})
124
+ )
125
+ ```
126
+ ## Citing & Authors
127
+ This model was trained by [text2vec](https://github.com/shibing624/text2vec).
128
+
129
+ If you find this model helpful, feel free to cite:
130
+ ```bibtex
131
+ @software{text2vec,
132
+ author = {Ming Xu},
133
+ title = {text2vec: A Tool for Text to Vector},
134
+ year = {2023},
135
+ url = {https://github.com/shibing624/text2vec},
136
+ }
137
+ ```