基於BERT的中文命名實體識別任務(BERT-BiLSTM-CRF-NER)

基於BERT的中文命名實體識別任務(BERT-BiLSTM-CRF-NER)
  1. TensorFlow環境
    官方requirements.txt要求環境版本

    tensorflow >= 1.11.0   # CPU Version of TensorFlow
    #tensorflow-gpu  >= 1.11.0  # GPU version of TensorFlow.
    

    本人實現代碼TensorFlow環境版本

    tensorflow           1.15.0
    tensorflow-estimator 1.15.1
    tensorflow-gpu       1.15.0
    python=3.6
    
  2. 數據集地址

    #數據標註格式如下,合計三種實體:B-PER/B-LOC/B-ORG
    #每行得第一個是字,第二個是它的標籤,使用空格’ '分隔,請一定要使用空格。句與句之間使用空行劃分。
    海 O
    釣 O
    比 O
    賽 O
    地 O
    點 O
    在 O
    廈 B-LOC
    門 I-LOC
    與 O
    金 B-LOC
    門 I-LOC
    之 O
    間 O
    的 O
    海 O
    域 O
    。 O
    
  3. BERT-BiLSTM-CRF-NER源碼地址

  4. 代碼目錄

  5. 代碼運行流程

    1. 下載BERT-BiLSTM-CRF-NER源碼;

      git clone https://github.com/macanv/BERT-BiLSTM-CRF-NER
      cd BERT-BiLSTM-CRF-NER/
      python3 setup.py install
      
    2. 從google提供的BERT官方下載中文BERT預訓練模型chinese_L-12_H-768_A-12,將其放到BERT-BiLSTM-CRF-NER目錄下;

      """
      其中
      bert_model.ckpt開頭的文件是負責模型變量載入的
      vocab.txt是訓練時中文文本採用的字典
      bert_config.json是BERT在訓練時,可選調整的一些參數
      """
      

    1. train_helper參數解讀

      #執行改命令 查看train_helper提供的相關參數
      bert-base-ner-train -help
      #訓練的事例命名如下:
      bert-base-ner-train \
          -data_dir {your dataset dir}\#訓練數據,驗證數據和測試數據的所在目錄路徑
          -output_dir {training output dir}\#訓練完成後的指定輸出路徑
          -init_checkpoint {Google BERT model dir}\#下載的谷歌BERT模型
          -bert_config_file {bert_config.json under the Google BERT model dir} \# 谷歌BERT模型下面的bert_config.json
          -vocab_file {vocab.txt under the Google BERT model dir}#谷歌BERT模型下面的vocab.txt
      #本人實例
      bert-base-ner-train \
          -data_dir data \
          -output_dir output \
          -init_checkpoint chinese_L-12_H-768_A-12/bert_model.ckpt \
          -bert_config_file chinese_L-12_H-768_A-12/bert_config.json \
          -vocab_file chinese_L-12_H-768_A-12/vocab.txt \
          -batch_size 8 #batch大小,對於普通8GB的GPU,最大batch大小隻能是8,再大就會OOM
      #可以直接執行命令,也可以執行
      python3 run.py#這種情況下參數一般都是默認的,可以到BERT-BiLSTM-CRF-NER/bert_base/train/train_helper.py去修改相關參數!
      

    2. 訓練成功截圖!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章