Xiaoxi2333 commited on
Commit
096f062
·
verified ·
1 Parent(s): 73a0943

Upload 6 files

Browse files
Files changed (6) hide show
  1. README.md +66 -0
  2. config.json +25 -0
  3. pytorch_model.bin +3 -0
  4. special_tokens_map.json +7 -0
  5. tokenizer_config.json +58 -0
  6. vocab.txt +0 -0
README.md CHANGED
@@ -1,3 +1,69 @@
1
  ---
 
 
 
 
 
 
 
2
  license: mit
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ language: zh
3
+ tags:
4
+ - bert
5
+ - multilabel-classification
6
+ - chinese
7
+ - intent-classification
8
+ - time-lbs
9
  license: mit
10
  ---
11
+
12
+ # 中文多标签意图识别模型(BERT)
13
+
14
+ 这是一个基于 `bert-base-chinese` 微调的多标签分类模型,支持以下任务:
15
+
16
+ 对中文query进行分类
17
+ - 多分类:意图识别(chat / simple question / complex question)
18
+ - 二分类:是否时间相关、是否位置(LBS)相关
19
+
20
+ ## 模型结构
21
+
22
+ - 基础模型:[`bert-base-chinese`](https://huggingface.co/bert-base-chinese)
23
+ - 输出层:一个 5 维的 sigmoid 多标签输出向量
24
+ - `[意图-chat, 意图-simple, 意图-complex, 是否时间相关, 是否LBS相关]`
25
+
26
+ ## 使用方法
27
+
28
+ ```python
29
+ import torch
30
+ from transformers import BertTokenizer
31
+ from bert_classifier_3 import BertMultiLabelClassifier
32
+
33
+ # 加载 tokenizer 和模型
34
+ tokenizer = BertTokenizer.from_pretrained("your-username/bert-multilabel-chinese")
35
+ model = BertMultiLabelClassifier(pretrained_model_path="your-username/bert-multilabel-chinese")
36
+ model.load_state_dict(torch.load("pytorch_model.bin", map_location="cpu"))
37
+ model.eval()
38
+
39
+ # 定义标签
40
+ intent_labels = ["chat", "simple question", "complex question"]
41
+ yesno_labels = ["否", "是"]
42
+
43
+ # 定义预测函数
44
+ def predict(query):
45
+ enc = tokenizer(
46
+ query,
47
+ truncation=True,
48
+ padding="max_length",
49
+ max_length=128,
50
+ return_tensors="pt"
51
+ )
52
+ with torch.no_grad():
53
+ logits = model(enc["input_ids"], enc["attention_mask"])
54
+ probs = torch.sigmoid(logits).squeeze(0)
55
+ intent_index = torch.argmax(probs[:3]).item()
56
+ is_time = int(probs[3] > 0.5)
57
+ is_lbs = int(probs[4] > 0.5)
58
+
59
+ return {
60
+ "query": query,
61
+ "意图": intent_labels[intent_index],
62
+ "是否时间相关": yesno_labels[is_time],
63
+ "是否lbs相关": yesno_labels[is_lbs],
64
+ "原始概率": probs.tolist()
65
+ }
66
+
67
+ # 示例查询
68
+ result = predict("明天北京天气怎么样?")
69
+ print(result)
config.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "BertForMaskedLM"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "directionality": "bidi",
7
+ "hidden_act": "gelu",
8
+ "hidden_dropout_prob": 0.1,
9
+ "hidden_size": 768,
10
+ "initializer_range": 0.02,
11
+ "intermediate_size": 3072,
12
+ "layer_norm_eps": 1e-12,
13
+ "max_position_embeddings": 512,
14
+ "model_type": "bert",
15
+ "num_attention_heads": 12,
16
+ "num_hidden_layers": 12,
17
+ "pad_token_id": 0,
18
+ "pooler_fc_size": 768,
19
+ "pooler_num_attention_heads": 12,
20
+ "pooler_num_fc_layers": 3,
21
+ "pooler_size_per_head": 128,
22
+ "pooler_type": "first_token_transform",
23
+ "type_vocab_size": 2,
24
+ "vocab_size": 21128
25
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9b02f410dda9ca48c233a818e7626d9b23327fa53c872d499bba0a513fedf0a1
3
+ size 210660184
special_tokens_map.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": "[CLS]",
3
+ "mask_token": "[MASK]",
4
+ "pad_token": "[PAD]",
5
+ "sep_token": "[SEP]",
6
+ "unk_token": "[UNK]"
7
+ }
tokenizer_config.json ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "[PAD]",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "100": {
12
+ "content": "[UNK]",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "101": {
20
+ "content": "[CLS]",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "102": {
28
+ "content": "[SEP]",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "103": {
36
+ "content": "[MASK]",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ }
43
+ },
44
+ "clean_up_tokenization_spaces": true,
45
+ "cls_token": "[CLS]",
46
+ "do_basic_tokenize": true,
47
+ "do_lower_case": false,
48
+ "extra_special_tokens": {},
49
+ "mask_token": "[MASK]",
50
+ "model_max_length": 512,
51
+ "never_split": null,
52
+ "pad_token": "[PAD]",
53
+ "sep_token": "[SEP]",
54
+ "strip_accents": null,
55
+ "tokenize_chinese_chars": true,
56
+ "tokenizer_class": "BertTokenizer",
57
+ "unk_token": "[UNK]"
58
+ }
vocab.txt ADDED
The diff for this file is too large to render. See raw diff