始める無料で始める

spaCyモデルをゼロから学習する

spaCy には、独自モデルを学習するためのシンプルで効率的な方法があります。この演習では、実世界のコーパス(CORD-19 データ)を用いて NER モデルをゼロから学習します。

トレーニングデータは適切な形式で training_data に用意されています。この演習では、空の英語モデル(nlp)に NER コンポーネントを追加し、labels に保存されたラベル("Pathogen"、"MedicalCondition"、"Medicine")を使用します。目的の医療系 labels を NER パイプラインに追加し、その後モデルを 1 エポック学習します。前もってインポートされた Example クラスを使って、トレーニングデータを必要な形式に変換しましょう。学習の進捗を把握するために、.update() メソッドに losses 辞書を渡してトレーニングロスを確認できます。

この演習はコースの一部です

spaCyで学ぶNatural Language Processing

コースを見る

演習の手順

  • 空の spaCy モデルを作成し、モデルに NER コンポーネントを追加します。
  • ほかのパイプラインコンポーネントを無効化し、作成した optimizer オブジェクトを使って、Example 形式に変換したデータでモデル重みを更新します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Load a blank English model, add NER component, add given labels to the ner pipeline
nlp = spacy.____("____")
ner = nlp.____("ner")
for ent in labels:
    ner.add_label(ent)

# Disable other pipeline components, complete training loop and run training loop
other_pipes = [____ for pipe in nlp.____ if ____ != "____"]
nlp.disable_pipes(*____)
losses = {}
optimizer = nlp.begin_training()
for text, annotation in training_data:
    doc = nlp.____(text)
    example = Example.____(doc, annotation)
    nlp.____([example], sgd=____, losses=losses)
    print(losses)
コードを編集して実行