更新:2024/07/14
deepsetのbert-large-uncased-whole-word-masking-squad2の使い方!Question Answering

目次
1. BERTとは
- Encoderタイプのモデル
- case-sensitiveでない
- 335M params
- SQuAD2.0 datasetでQAタスクにfine tuning
- bert-largeモデルをfine tuning
BERT(Bidirectional Encoder Representations from Transformers)はencoderのみの言語モデルです。bert-large-uncased-whole-word-masking-squad2はbertの一種で、QAタスクのためにfine tuningが行われています。SQuADとは、Stanford Question Answering Dataset のことです。bert-large-uncased-whole-word-masking-squad2 は335M paramsです。bert-large-uncased-whole-word-masking-squad2はcase-sensitiveではないので、大文字小文字を区別しないです。例えば、Japanaとjapanは同じであると扱われます。また、bert-large-uncasedという110M paramsの言語モデルもあります。
2. bert-large-uncased-whole-word-masking-squad2でできること
QAはQuestion Answeringのことで、質問応答を行うことができます。
2.1. QAの流れ
- 質問とコンテキストの入力:
- ユーザーからの質問(Question)と、それに関連するテキスト(Context)がシステムに入力されます。
- 回答の抽出:
- BERTモデルの出力から、質問に対する最も適切な回答を抽出します。通常、開始位置と終了位置のトークンを予測し、その範囲内のテキストを回答として返します。
- 回答の表示:
- 最終的に抽出された回答がユーザーに表示されます。
3. pythonコード
3.1. 実行環境
- RTX 4070ti super VRAM 16GB
- Windows11
- memory 64GB
- Python 3.11.9
3.2. 使用するモデルとデータ
- モデル: Hugging Faceからdeepset/bert-large-uncased-whole-word-masking-squad2を使用。
- 質問(question): “What are the main types of wine?”(主要なワインの種類は何ですか?)
- コンテキスト(context): “There are several main types of wine: red wine, white wine, rosé wine, sparkling wine, and dessert wine. Each type has distinct production methods and flavor profiles.”(いくつかの主要なワインの種類があります:赤ワイン、白ワイン、ロゼワイン、スパークリングワイン、デザートワイン。それぞれの種類には独自の生産方法と風味があります。)
3.3. python
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
model_name = "deepset/bert-large-uncased-whole-word-masking-squad2"
nlp = pipeline('question-answering', model=model_name, tokenizer=model_name,device=0)
QA_input = {
'question': 'What are the main types of wine?',
'context': 'There are several main types of wine: red wine, white wine, rosé wine, sparkling wine, and dessert wine. Each type has distinct production methods and flavor profiles.'
}
res = nlp(QA_input)
- start: 38
- コンテキスト内で答えの開始位置を示します。
- end: 103
- コンテキスト内で答えの終了位置を示します。
- answer: “red wine, white wine, rosé wine, sparkling wine, and dessert wine”
- モデルが抽出した答えです。文章内に含まれているワインの種類をあてることができました。
3.4. 使用された計算資源
4. Transformers・大規模言語モデルに関連する書籍
ブックオフ2号館 ヤフーショッピング店
bookfanプレミアム
大規模言語モデル入門II-生成型LLMの実装と評価 / 山田育矢 〔本〕
posted with カエレバ
HMV&BOOKS online Yahoo!店
PR