Views
No views yet
pip install git+https://github.com/JunnYu/WoBERT_pytorch.git1import torch
2from transformers import BertForMaskedLM as WoBertForMaskedLM
3from wobert import WoBertTokenizer
4
5pretrained_model_or_path_list = [
6 "junnyu/wobert_chinese_plus_base", "junnyu/wobert_chinese_base"
7]
8for path in pretrained_model_or_path_list:
9 text = "今天[MASK]很好,我[MASK]去公园玩。"
10 tokenizer = WoBertTokenizer.from_pretrained(path)
11 model = WoBertForMaskedLM.from_pretrained(path)
12 inputs = tokenizer(text, return_tensors="pt")
13 with torch.no_grad():
14 outputs = model(**inputs).logits[0]
15 outputs_sentence = ""
16 for i, id in enumerate(tokenizer.encode(text)):
17 if id == tokenizer.mask_token_id:
18 tokens = tokenizer.convert_ids_to_tokens(outputs[i].topk(k=5)[1])
19 outputs_sentence += "[" + "||".join(tokens) + "]"
20 else:
21 outputs_sentence += "".join(
22 tokenizer.convert_ids_to_tokens([id],
23 skip_special_tokens=True))
24 print(outputs_sentence)
25# RoFormer 今天[天气||天||心情||阳光||空气]很好,我[想||要||打算||准备||喜欢]去公园玩。
26# PLUS WoBERT 今天[天气||阳光||天||心情||空气]很好,我[想||要||打算||准备||就]去公园玩。
27# WoBERT 今天[天气||阳光||天||心情||空气]很好,我[想||要||就||准备||也]去公园玩。1@techreport{zhuiyiwobert,
2 title={WoBERT: Word-based Chinese BERT model - ZhuiyiAI},
3 author={Jianlin Su},
4 year={2020},
5 url="https://github.com/ZhuiyiTechnology/WoBERT",
6}