使用 AutoTrain 進行抽取式問答

社群文章 釋出於 2024 年 8 月 20 日

抽取式問答 (Extractive Question Answering) 是一項訓練模型從給定上下文中提取問題答案的任務。模型被訓練來預測答案在上下文中的起始和結束位置。這項任務常用於問答系統中,從大量文字語料庫中提取相關資訊。

有時候,你需要的不僅僅是生成式模型 ;)

在這篇部落格中,我們將討論如何使用 AutoTrain 來訓練一個抽取式問答模型。AutoTrain(又名 AutoTrain Advanced)是一個開源的、無需程式碼的解決方案,它簡化了在各種領域和模態型別中訓練最先進模型的過程。它使你只需點選幾下即可訓練模型,無需任何編碼或機器學習專業知識。

AutoTrain 的 GitHub 倉庫可以在這裡找到。

準備資料

要訓練一個抽取式問答模型,你需要一個包含以下列的資料集

  • context: 用於提取答案的上下文或段落。
  • question: 需要提取答案的問題。
  • answer: 答案在上下文中的起始位置和答案文字。

answer 列應該是一個包含 textanswer_start 鍵的字典。

例如:

{
    "context":"Architecturally, the school has a Catholic character. Atop the Main Building's gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend \"Venite Ad Me Omnes\". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.",
    "question":"To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?",
    "answers":{"text":["Saint Bernadette Soubirous"],"answer_start":[515]}
}

AutoTrain 支援 CSV 和 JSONL 格式的訓練資料。如果你想使用 CSV,answer 列應該是包含 textanswer_start 鍵的字串化 JSON。JSONL 是問答任務的首選格式。

你也可以使用 Hugging Face Hub 上的資料集,例如 lhoestq/squad

資料集看起來是這樣的

列對映:

列對映對於 AutoTrain 至關重要。AutoTrain 根據提供的列對映來理解資料。對於抽取式問答,列對映應如下所示

{"text": "context", "question": "question", "answer": "answers"}

其中 answer 是一個包含 textanswer_start 鍵的字典。

如你所見,AutoTrain 的列是:textquestionanswer

在本地訓練模型

要在本地使用 AutoTrain,你需要安裝 pip 包:autotrain-advanced

$ pip install -U autotrain-advanced

安裝包後,你可以使用以下命令訓練模型

$ export HF_USERNAME=<your_hf_username>
$ export HF_TOKEN=<your_hf_write_token>
$ autotrain --config <path_to_config_file>

其中配置檔案看起來像這樣

task: extractive-qa
base_model: google-bert/bert-base-uncased
project_name: autotrain-bert-ex-qa1
log: tensorboard
backend: local

data:
  path: lhoestq/squad
  train_split: train
  valid_split: validation
  column_mapping:
    text_column: context
    question_column: question
    answer_column: answers

params:
  max_seq_length: 512
  max_doc_stride: 128
  epochs: 3
  batch_size: 4
  lr: 2e-5
  optimizer: adamw_torch
  scheduler: linear
  gradient_accumulation: 1
  mixed_precision: fp16

hub:
  username: ${HF_USERNAME}
  token: ${HF_TOKEN}
  push_to_hub: true

上述配置將在 lhoestq/squad 資料集上訓練一個 BERT 模型,訓練 3 個 epoch,批大小為 4,學習率為 2e-5。你可以在文件中找到所有引數。

如果使用本地檔案,你只需將配置檔案的 data 部分更改為

data:
  path: data/ # this must be the path to the directory containing the train and valid files
  train_split: train # this must be either train.csv or train.json
  valid_split: valid # this must be either valid.csv or valid.json, can also be null

注意:如果你不將模型推送到 Hub,或者不使用受限/私有的模型/資料集,則無需匯出你的 HF_USERNAME 和 HF_TOKEN。

在 Hugging Face Hub 上訓練模型

要在 Hugging Face Hub 上訓練模型,你需要建立一個具有適當硬體的 AutoTrain Space。要建立 AutoTrain Space,請訪問 AutoTrain 並按照說明操作,或點選此處

完成後,你將看到如下介面

image/png

選擇 Extractive Question Answering 任務,填寫所需資訊:資料集和列對映,如果需要可以更改引數,然後點選“Start Training”。

你也可以使用以下命令在本地執行 UI

$ export HF_TOKEN=<your_hf_write_token>
$ autotrain app

就是這樣!現在你可以使用 AutoTrain 在本地或 Hugging Face Hub 上訓練你自己的抽取式問答模型了。

訓練愉快! 🚀

如果你有任何問題或需要幫助,請隨時在 GitHub 上與我們聯絡。

社群

註冊登入 以發表評論

© . This site is unofficial and not affiliated with Hugging Face, Inc.