Advertisement

使用DeepKE训练命名实体识别模型DEMO(官方DEMO)

阅读量:

使用DeepKE训练命名实体识别模型DEMO(官方DEMO)

说明:

如果需要,设置Github镜像

复制代码
    git config --system url."https://githubfast.com/".insteadOf https://github.com/
    
    如果要取消,则输入:
    git config --system --unset url.https://githubfast.com/.insteadof

创建conda环境

复制代码
    conda create -n deepke python=3.8
    conda activate deepke
    
    # 安装torch
    pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu113
    
    # 使用阿里云镜像安装torch 1.11.0
    # pip install https://mirrors.aliyun.com/pytorch-wheels/cu113/torch-1.11.0+cu113-cp38-cp38-linux_x86_64.whl https://mirrors.aliyun.com/pytorch-wheels/cu113/torchvision-0.12.0+cu113-cp38-cp38-linux_x86_64.whl https://mirrors.aliyun.com/pytorch-wheels/cu113/torchaudio-0.11.0+cu113-cp38-cp38-linux_x86_64.whl -i https://mirrors.aliyun.com/pypi/simple/

安装DeepKE:

复制代码
    git clone https://github.com/zjunlp/DeepKE.git
    cd DeepKE
    
    pip install pip==24.0
    
    pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
    python setup.py install
    python setup.py develop
复制代码
    pip install prettytable==2.4.0
    pip install ipython==8.12.0

下载数据集

复制代码
    # apt-get install wget
    cd example/ner/standard
    wget 120.27.214.45/Data/ner/standard/data.tar.gz
    tar -xzvf data.tar.gz

可以看到data文件夹下有:

  • train.txt: Training set
  • valid.txt : Validation set
  • test.txt: Test set

配置wandb

请访问 https://wandb.ai/ 创建账户,并随后创建一个 project,请给项目命名。例如,请使用名称 deepke-ner-official-demo 进行操作。

打开 https://wandb.ai/authorize 获取 API key

运行 wandb init,输入刚获取的 API key 和创建的project

运行训练和预测

删除之前训练时保存的checkpoints和logs文件夹(如果有):

复制代码
    rm -r checkpoints/
    rm -r logs/

lstmcrf

打开 example/ner/standard/run_lstmcrf.py , 安装wandb和yaml库并成功导入:

复制代码
    import wandb
    import yaml

修改wandb的project名称:

复制代码
    if config['use_wandb']:
    wandb.init(project="deepke-ner-official-demo")

修改 example/ner/standard/conf/config.yaml 中的 use_wandbTrue

若使用多个GPU进行训练,则需在配置文件example/ner/standard/conf/train.yaml中将参数use\_multi\_gpu设置为True

开始训练:

复制代码
    python run_lstmcrf.py
复制代码
    >> total: 109870 loss: 27.181508426008552
              precision    recall  f1-score   support
    
       B-LOC     0.8920    0.8426    0.8666      1951
       B-ORG     0.8170    0.7439    0.7787       984
       B-PER     0.8783    0.8167    0.8464       884
       I-LOC     0.8650    0.8264    0.8453      2581
       I-ORG     0.8483    0.8365    0.8424      3945
       I-PER     0.8860    0.8436    0.8643      1714
           O     0.9861    0.9912    0.9886     97811
    
    accuracy                         0.9732    109870
       macro avg     0.8818    0.8430    0.8618    109870
    weighted avg     0.9727    0.9732    0.9729    109870

被用于的预测任务保存在example/ner/standard/conf/predict.yaml

复制代码
    text: "“热水器等以旧换新,节省了2000多元。”10月3日,在湖北省襄阳市的一家购物广场,市民金煜轻触手机,下单、付款、登记。湖北着力推动大规模设备更新和消费品以旧换新。“力争到今年底,全省汽车报废更新、置换更新分别达到4.5万辆、12.5万辆,家电以旧换新170万套。”湖北省商务厅厅长龙小红介绍。"

运行预测:

复制代码
    python predict.py

NER结果:

复制代码
    [('湖', 'B-LOC'), ('北', 'I-LOC'), ('省', 'I-LOC'), ('襄', 'B-LOC'), ('阳', 'I-LOC'), ('市', 'I-LOC'), ('场', 'I-LOC'), ('煜', 'I-PER'), ('湖', 'B-ORG'), ('北', 'I-ORG'), ('省', 'I-ORG'), ('商', 'I-ORG'), ('务', 'I-ORG'), ('厅', 'I-ORG'), ('厅', 'I-ORG'), ('龙', 'B-PER'), ('小', 'I-PER'), ('红', 'I-PER')]

bert

修改 example/ner/standard/conf/config.yaml中的hydra/modelbert

bert的超参数配置在 example/ner/standard/conf/hydra/model/bert.yaml

修改 example/ner/standard/conf/config.yaml 中的 use_wandbTrue

修改 example/ner/standard/run_bert.py 中的wandb的project名称:

复制代码
    if cfg.use_wandb:
        wandb.init(project="deepke-ner-official-demo")

基于需求,需对example/ner/standard/conf/train.yaml文件中的train_batch_size参数进行调整;建议设置其最低值为64

开始训练:

复制代码
    export HF_ENDPOINT=https://hf-mirror.com
    python run_bert.py

w2ner

w2ner是一个新的SOTA模型。

W2NER (AAAI’22)为基础的实体识别技术能够有效地应对多种应用场景,并且相关研究可参考论文Unified Named Entity Recognition as Word-Word Relation Classification

覆盖主要类型的是命名实体识别(NER),这些类型包括平面、重叠(亦称嵌套)以及不连续的NER,并各自被单独研究。近年来,“统一NER”已成为研究重点,在这一领域中,“W2NER”通过单一模型实现了对上述三项工作的整合处理。

基于单卡GPU配置的情况下,在文件路径example/ner/standard/w2ner/conf/train.yaml$中将参数device$设置为整数0。

修改example/ner/standard/w2ner/conf/train.yaml中的data_dirdo_train

复制代码
    data_dir: "../data"
    do_train: True

以便使用之前下载的数据集和开始训练。

运行训练:

复制代码
    python run.py

全部评论 (0)

还没有任何评论哟~