bert+lstm+crf ner实体识别

参考:
https://github.com/BrikerMan/Kashgari

https://eliyar.biz/nlp_chinese_text_classification_in_15mins/

bert预训练中文模型下载:百度云下载地址:https://pan.baidu.com/s/16JJp6NMCjmIT8PqP9OfN4g
包含这些内容的整体下载下来
bert+lstm+crf ner实体识别_第1张图片

!pip install kashgari

!pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow==1.14

from kashgari.corpus import ChineseDailyNerCorpus

train_x, train_y = ChineseDailyNerCorpus.load_data('train')
valid_x, valid_y = ChineseDailyNerCorpus.load_data('validate')
test_x, test_y  = ChineseDailyNerCorpus.load_data('test')


print(f"train data count: {len(train_x)}")
print(f"validate data count: {len(valid_x)}")
print(f"test data count: {len(test_x)}")




import kashgari
from kashgari.embeddings import BERTEmbedding

bert_embed = BERTEmbedding('/---刚下载的bert预训练模型目录-/uncased_L-12_H-768_A-12',
                           task=kashgari.LABELING,
                           sequence_length=100)


from kashgari.tasks.labeling import BiLSTM_CRF_Model

# 还可以选择 `CNN_LSTM_Model`, `BiLSTM_Model`, `BiGRU_Model` 或 `BiGRU_CRF_Model`

model = BiLSTM_CRF_Model(bert_embed)
model.fit(train_x,
          train_y,
          x_validate=valid_x,
          y_validate=valid_y,
          epochs=5,
          batch_size=512)

model.evaluate(test_x, test_y)

odel.save('./model')

import random
import kashgari
# 加载模型
loaded_model = kashgari.utils.load_model('cnn_classification_model')
loaded_model.predict(random.sample(train_x, 10))

# 预测指定样本
news_sample = """「DeepMind 击败人类职业玩家的方式与他们声称的 AI 使命,以及所声称的『正确』方式完全相反。」
DeepMind 的人工智能 AlphaStar 一战成名,击败两名人类职业选手。掌声和欢呼之余,它也引起了一些质疑。在前天 DeepMind 举办的 AMA 中,AlphaStar 项目领导者 Oriol Vinyals 和 David Silver、职业玩家 LiquidTLO 与 LiquidMaNa 回答了一些疑问。不过困惑依然存在……
"""
x = list(jieba.cut(news_sample))
y = loaded_model.predict([x])
print(y[0]) # 输出游戏

bert+lstm+crf ner实体识别_第2张图片

bert+lstm+crf ner实体识别_第3张图片

你可能感兴趣的:(深度学习)