infgrad commited on
Commit
c06c6cf
1 Parent(s): 61f05ea

Upload 25 files

Browse files
1_Pooling/config.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "word_embedding_dimension": 1024,
3
+ "pooling_mode_cls_token": false,
4
+ "pooling_mode_mean_tokens": true,
5
+ "pooling_mode_max_tokens": false,
6
+ "pooling_mode_mean_sqrt_len_tokens": false
7
+ }
2_Dense/config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "in_features": 1024,
3
+ "out_features": 4096,
4
+ "bias": true,
5
+ "activation_function": "torch.nn.modules.linear.Identity"
6
+ }
2_Dense/pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c8011cb0638bcd225e7bd0b391e9f7d595dedb801c42f9c8dc36463f0b1b39d7
3
+ size 16795132
2_Dense_1024/config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "in_features": 1024,
3
+ "out_features": 1024,
4
+ "bias": true,
5
+ "activation_function": "torch.nn.modules.linear.Identity"
6
+ }
2_Dense_1024/pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b39c1d3b787212240a1c2a20833ce4a2107e84bf56a3c68df924250c735697e1
3
+ size 4199932
2_Dense_1536/config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "in_features": 1024,
3
+ "out_features": 1536,
4
+ "bias": true,
5
+ "activation_function": "torch.nn.modules.linear.Identity"
6
+ }
2_Dense_1536/pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:04b65c97b6553c42428e0ffc2066725a2436cd676ff956886c5857d5f5c7e829
3
+ size 6299132
2_Dense_1792/config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "in_features": 1024,
3
+ "out_features": 1792,
4
+ "bias": true,
5
+ "activation_function": "torch.nn.modules.linear.Identity"
6
+ }
2_Dense_1792/pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4ffab47ef95714827aba550f49fbd11919572eff02f4313f9e3564ebd839ec9e
3
+ size 7348732
2_Dense_2048/config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "in_features": 1024,
3
+ "out_features": 2048,
4
+ "bias": true,
5
+ "activation_function": "torch.nn.modules.linear.Identity"
6
+ }
2_Dense_2048/pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6eef68978561189b35651a7035e177d58b79c1c9e843efe2ce9a3f511d42f625
3
+ size 8398332
2_Dense_256/config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "in_features": 1024,
3
+ "out_features": 256,
4
+ "bias": true,
5
+ "activation_function": "torch.nn.modules.linear.Identity"
6
+ }
2_Dense_256/pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1a5f629d5ecb9866b0903dc52363ac0771741673dbfe109c8684d52c678362e0
3
+ size 1051132
2_Dense_4096/config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "in_features": 1024,
3
+ "out_features": 4096,
4
+ "bias": true,
5
+ "activation_function": "torch.nn.modules.linear.Identity"
6
+ }
2_Dense_4096/pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c8011cb0638bcd225e7bd0b391e9f7d595dedb801c42f9c8dc36463f0b1b39d7
3
+ size 16795132
2_Dense_768/config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "in_features": 1024,
3
+ "out_features": 768,
4
+ "bias": true,
5
+ "activation_function": "torch.nn.modules.linear.Identity"
6
+ }
2_Dense_768/pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6b70dcfda0413991abf475fc44f6b146c49064d106065701ea23f85b64f4a251
3
+ size 3150332
README.md ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # 模型介绍
3
+
4
+ 主要针对检索和语义匹配任务,本人实测要好于当前大多数向量模型。
5
+
6
+ 支持多个向量维度:256,768,1024,1563,1792,2048,4096
7
+
8
+ 支持中英互搜,但是英文表征能力要弱于中文
9
+
10
+ # 模型目录结构
11
+
12
+ 结构很简单,就是标准的SentenceTransformer文件目录 + 一系列`2_Dense_{dims}`文件夹,dims代表最终的向量维度。
13
+
14
+ 举个例子,`2_Dense_256`文件夹里存储了把向量维度转换为256维的Linear权重,具体如何使用请看下面的章节
15
+
16
+ # 模型使用方法
17
+
18
+ 可直接用SentenceTransformer加载,也可以使用transformer加载使用:
19
+
20
+ ```python
21
+ import os
22
+ import torch
23
+ from transformers import AutoModel, AutoTokenizer
24
+ from sentence_transformers import SentenceTransformer
25
+ from sklearn.preprocessing import normalize
26
+
27
+ # 待编码文本
28
+ texts = ["通用向量编码", "hello world", "支持中英互搜,不建议纯英文场景使用"]
29
+ # 模型目录
30
+ model_dir = "{MODEL_PATH}"
31
+
32
+ #### 方法1:使用SentenceTransformer
33
+ # !!!!!!!!!!!!!!默认是4096维度,如需其他维度,请自行复制2_Dense_{dims}中的文件到2_Dense文件夹中覆盖!!!!!!!!!!!!!!
34
+ model = SentenceTransformer(model_dir)
35
+ vectors = model.encode(texts, convert_to_numpy=True, normalize_embeddings=True)
36
+ print(vectors.shape)
37
+ print(vectors[:, :4])
38
+
39
+ #### 方法2:使用transformers库
40
+ # !!!!!!!!!!!!!! 本代码会根据vector_dim值会读取对应的Linear层权重,请按需选择vector_dim !!!!!!!!!!!!!!
41
+ vector_dim = 4096
42
+ model = AutoModel.from_pretrained(model_dir).eval()
43
+ tokenizer = AutoTokenizer.from_pretrained(model_dir)
44
+ vector_linear = torch.nn.Linear(in_features=model.config.hidden_size, out_features=vector_dim)
45
+ vector_linear_dict = {
46
+ k.replace("linear.", ""): v for k, v in
47
+ torch.load(os.path.join(model_dir, f"2_Dense_{vector_dim}/pytorch_model.bin")).items()
48
+ }
49
+ vector_linear.load_state_dict(vector_linear_dict)
50
+ with torch.no_grad():
51
+ input_data = tokenizer(texts, padding="longest", truncation=True, max_length=512, return_tensors="pt")
52
+ attention_mask = input_data["attention_mask"]
53
+ last_hidden_state = model(**input_data)[0]
54
+ last_hidden = last_hidden_state.masked_fill(~attention_mask[..., None].bool(), 0.0)
55
+ vectors = last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
56
+ vectors = normalize(vector_linear(vectors).cpu().numpy())
57
+ print(vectors.shape)
58
+ print(vectors[:, :4])
59
+
60
+ ```
config.json ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "XLMRobertaModel"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "bos_token_id": 0,
7
+ "classifier_dropout": null,
8
+ "eos_token_id": 2,
9
+ "hidden_act": "gelu",
10
+ "hidden_dropout_prob": 0.1,
11
+ "hidden_size": 1024,
12
+ "initializer_range": 0.02,
13
+ "intermediate_size": 4096,
14
+ "layer_norm_eps": 1e-05,
15
+ "max_position_embeddings": 8194,
16
+ "model_type": "xlm-roberta",
17
+ "num_attention_heads": 16,
18
+ "num_hidden_layers": 24,
19
+ "output_past": true,
20
+ "pad_token_id": 1,
21
+ "position_embedding_type": "absolute",
22
+ "torch_dtype": "float32",
23
+ "transformers_version": "4.37.2",
24
+ "type_vocab_size": 1,
25
+ "use_cache": true,
26
+ "vocab_size": 250002
27
+ }
modules.json ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "idx": 0,
4
+ "name": "0",
5
+ "path": "",
6
+ "type": "sentence_transformers.models.Transformer"
7
+ },
8
+ {
9
+ "idx": 1,
10
+ "name": "1",
11
+ "path": "1_Pooling",
12
+ "type": "sentence_transformers.models.Pooling"
13
+ },
14
+ {
15
+ "idx": 2,
16
+ "name": "2",
17
+ "path": "2_Dense",
18
+ "type": "sentence_transformers.models.Dense"
19
+ }
20
+ ]
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:30cd755f63be9334d2dfef6d643908b49853a0e723e897c4eb31d4ff111fd8ec
3
+ size 2472524610
sentence_bert_config.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "max_seq_length": 512,
3
+ "do_lower_case": false
4
+ }
sentencepiece.bpe.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cfc8146abe2a0488e9e2a0c56de7952f7c11ab059eca145a0a727afce0db2865
3
+ size 5069051
special_tokens_map.json ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "<s>",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "cls_token": {
10
+ "content": "<s>",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "eos_token": {
17
+ "content": "</s>",
18
+ "lstrip": false,
19
+ "normalized": false,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ },
23
+ "mask_token": {
24
+ "content": "<mask>",
25
+ "lstrip": true,
26
+ "normalized": false,
27
+ "rstrip": false,
28
+ "single_word": false
29
+ },
30
+ "pad_token": {
31
+ "content": "<pad>",
32
+ "lstrip": false,
33
+ "normalized": false,
34
+ "rstrip": false,
35
+ "single_word": false
36
+ },
37
+ "sep_token": {
38
+ "content": "</s>",
39
+ "lstrip": false,
40
+ "normalized": false,
41
+ "rstrip": false,
42
+ "single_word": false
43
+ },
44
+ "unk_token": {
45
+ "content": "<unk>",
46
+ "lstrip": false,
47
+ "normalized": false,
48
+ "rstrip": false,
49
+ "single_word": false
50
+ }
51
+ }
tokenizer_config.json ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "<s>",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "1": {
12
+ "content": "<pad>",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "2": {
20
+ "content": "</s>",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "3": {
28
+ "content": "<unk>",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "250001": {
36
+ "content": "<mask>",
37
+ "lstrip": true,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ }
43
+ },
44
+ "bos_token": "<s>",
45
+ "clean_up_tokenization_spaces": true,
46
+ "cls_token": "<s>",
47
+ "eos_token": "</s>",
48
+ "mask_token": "<mask>",
49
+ "model_max_length": 8192,
50
+ "pad_token": "<pad>",
51
+ "sep_token": "</s>",
52
+ "sp_model_kwargs": {},
53
+ "tokenizer_class": "XLMRobertaTokenizer",
54
+ "unk_token": "<unk>"
55
+ }