Views
No views yet
"bhys" -> "不好意思""ys" -> "原神"
| Model | 模型权重 | Memory Usage (FP16) | Model Size | QPS | MRR | Acc |
|---|---|---|---|---|---|---|
| CNMBert-Default | Huggingface | 0.4GB | 131M | 12.56 | 59.70 | 49.74 |
| CNMBert-MoE | Huggingface | 0.8GB | 329M | 3.20 | 61.53 | 51.86 |
1from transformers import AutoTokenizer, BertConfig
2
3from CustomBertModel import predict
4from MoELayer import BertWwmMoE1# use CNMBert with MoE
2# To use CNMBert without MoE, replace all "Midsummra/CNMBert-MoE" with "Midsummra/CNMBert" and use BertForMaskedLM instead of using BertWwmMoE
3tokenizer = AutoTokenizer.from_pretrained("Midsummra/CNMBert-MoE")
4config = BertConfig.from_pretrained('Midsummra/CNMBert-MoE')
5model = BertWwmMoE.from_pretrained('Midsummra/CNMBert-MoE', config=config).to('cuda')
6
7# model = BertForMaskedLM.from_pretrained('Midsummra/CNMBert').to('cuda')1print(predict("我有两千kq", "kq", model, tokenizer)[:5])
2print(predict("快去给魔理沙看b吧", "b", model, tokenizer[:5]))['块钱', 1.2056937473156175], ['块前', 0.05837443749364857], ['开千', 0.0483869208528063], ['可千', 0.03996622172280445], ['口气', 0.037183335575008414]
['病', 1.6893256306648254], ['吧', 0.1642467901110649], ['呗', 0.026976384222507477], ['包', 0.021441461518406868], ['报', 0.01396679226309061]
1# 默认的predict函数使用束搜索
2def predict(sentence: str,
3 predict_word: str,
4 model,
5 tokenizer,
6 top_k=8,
7 beam_size=16, # 束宽
8 threshold=0.005, # 阈值
9 fast_mode=True, # 是否使用快速模式
10 strict_mode=True): # 是否对输出结果进行检查
11
12# 使用回溯的无剪枝暴力搜索
13def backtrack_predict(sentence: str,
14 predict_word: str,
15 model,
16 tokenizer,
17 top_k=10,
18 fast_mode=True,
19 strict_mode=True):由于BERT的自编码特性,导致其在预测MASK时,顺序不同会导致预测结果不同,如果启用fast_mode,则会正向和反向分别对输入进行预测,可以提升一点准确率(2%左右),但是会带来更大的性能开销。
strict_mode会对输入进行检查,以判断其是否为一个真实存在的汉语词汇。
fast_mode和strict_mode为False。 模型是在很小的数据集(200w)上进行的预训练,所以泛化能力不足很正常,,,可以在更大数据集或者更加细分的领域进行微调,具体微调方式和Chinese-BERT-wwm差别不大,只需要将DataCollactor替换为CustomBertModel.py中的DataCollatorForMultiMask。@misc{feng2024cnmbertmodelhanyupinyin,
title={CNMBert: A Model For Hanyu Pinyin Abbreviation to Character Conversion Task},
author={Zishuo Feng and Feng Cao},
year={2024},
eprint={2411.11770},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2411.11770},
}