训练数据准备

使用 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...