訓練資料準備

使用 spaCy 的自然語言處理

Azadeh Mobasher

Principal data scientist

訓練步驟

 

  1. 標註並準備輸入資料
  2. 初始化模型權重
  3. 以當前權重預測數個範例
  4. 將預測與正確答案比較
  5. 用最佳化器計算能提升表現的權重
  6. 微幅更新權重
  7. 回到步驟 3。
使用 spaCy 的自然語言處理

資料標註與準備

  • 首先要將訓練資料整理成所需格式。
  • 蒐集完資料後,接著進行標註
  • 標註是為意圖、實體等加上標籤。
  • 這是標註後資料的範例:
annotated_data = {
"sentence": "An antiviral drugs used against influenza is neuraminidase inhibitors.",
"entities": {
             "label": "Medicine",
             "value": "neuraminidase inhibitors",
    }
}
使用 spaCy 的自然語言處理

資料標註與準備

  • 這是另一個標註後資料的範例:

 

annotated_data = {
"sentence": "Bill Gates visited the SFO Airport.",
"entities": [{"label": "PERSON", "value": "Bill Gates"}, 
             {"label": "LOC", "value": "SFO Airport"}]
}
使用 spaCy 的自然語言處理

spaCy 訓練資料格式

  • 資料標註可讓訓練資料對應到模型要學的內容。
  • 訓練資料集需以字典形式儲存:
training_data = [
("I will visit you in Austin.", {"entities": [(20, 26, "GPE")]}),
("I'm going to Sam's house.", {"entities": [(13,18, "PERSON"), (19, 24, "GPE")]}),
("I will go.", {"entities": []})
]

三組範例配對:

  • 每組配對的第一個元素是句子
  • 第二個元素是帶有起訖字元位置的實體標註清單
使用 spaCy 的自然語言處理

用於訓練的 Example 物件資料

  • 不能直接把原始文字餵給 spaCy。

  • 需要為每個訓練範例建立一個 Example 物件。

import spacy
from spacy.training import Example

nlp = spacy.load("en_core_web_sm")

doc = nlp("I will visit you in Austin.")

annotations = {"entities": [(20, 26, "GPE")]} example_sentence = Example.from_dict(doc, annotations)
print(example_sentence.to_dict())
使用 spaCy 的自然語言處理

一起來練習吧!

使用 spaCy 的自然語言處理

Preparing Video For Download...