https://blog.csdn.net/lisuo1234/article/details/52244527
https://blog.csdn.net/AinUser/article/details/86374385?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-10&spm=1001.2101.3001.4242
https://blog.csdn.net/lisuo1234/article/details/52244527
https://blog.csdn.net/AinUser/article/details/86374385?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-10&spm=1001.2101.3001.4242
pip install --user keras-bert
# Keras BERT API Demo import sysimport numpy as npfrom keras_bert import ( load_vocabulary, load_trained_model_from_checkpoint, Tokenizer, get_checkpoint_paths,)from keras_bert.datasets import get_pretrained, PretrainedList # 導入已訓練中文模型model_path = get_pretrained(PretrainedList.chinese_base)paths = get_checkpoint_paths(model_path)model = load_trained_model_from_checkpoint( paths.config, paths.checkpoint, training=True, seq_len=None)model.summary(line_length=120)# 導入字典token_dict = load_vocabulary(paths.vocab)token_dict_inv = {v: k for k, v in token_dict.items()}# 要測試的字串# 舉例 : CLS 台 北 是 MASK MASK 的 首 都 SEP# MASK = 要猜的字串tokens = ["[CLS]"]mask = ["[MASK]", "[MASK]"]text1 = "台北是"text2 = "的首都"tokens.extend(list(text1))tokens.extend(mask)tokens.extend(list(text2))tokens.append("[SEP]")print("Tokens to predict:", tokens)indices = np.array([[token_dict[token] for token in tokens]])segments = np.array([[0] * len(tokens)])# 要預測的位置為1 其它為0masks = np.array([[0, 0, 0, 0, 1, 1, 0, 0, 0, 0]])predicts = model.predict([indices, segments, masks])[0].argmax(axis=-1).tolist()print("Fill with: ", list(map(lambda x: token_dict_inv[x], predicts[0][4:6])))
