Spaces:
Paused
A newer version of the Gradio SDK is available:
5.23.3
์ง์ ์๋ต(Question Answering)[[question-answering]]
[[open-in-colab]]
์ง์ ์๋ต ํ์คํฌ๋ ์ฃผ์ด์ง ์ง๋ฌธ์ ๋ํ ๋ต๋ณ์ ์ ๊ณตํฉ๋๋ค. Alexa, Siri ๋๋ Google๊ณผ ๊ฐ์ ๊ฐ์ ๋น์์๊ฒ ๋ ์จ๊ฐ ์ด๋ค์ง ๋ฌผ์ด๋ณธ ์ ์ด ์๋ค๋ฉด ์ง์ ์๋ต ๋ชจ๋ธ์ ์ฌ์ฉํด๋ณธ ์ ์ด ์์ ๊ฒ์ ๋๋ค. ์ง์ ์๋ต ํ์คํฌ์๋ ์ผ๋ฐ์ ์ผ๋ก ๋ ๊ฐ์ง ์ ํ์ด ์์ต๋๋ค.
- ์ถ์ถ์ (Extractive) ์ง์ ์๋ต: ์ฃผ์ด์ง ๋ฌธ๋งฅ์์ ๋ต๋ณ์ ์ถ์ถํฉ๋๋ค.
- ์์ฑ์ (Abstractive) ์ง์ ์๋ต: ๋ฌธ๋งฅ์์ ์ง๋ฌธ์ ์ฌ๋ฐ๋ฅด๊ฒ ๋ตํ๋ ๋ต๋ณ์ ์์ฑํฉ๋๋ค.
์ด ๊ฐ์ด๋๋ ๋ค์๊ณผ ๊ฐ์ ๋ฐฉ๋ฒ๋ค์ ๋ณด์ฌ์ค๋๋ค.
- ์ถ์ถ์ ์ง์ ์๋ต์ ํ๊ธฐ ์ํด SQuAD ๋ฐ์ดํฐ ์ธํธ์์ DistilBERT ๋ฏธ์ธ ์กฐ์ ํ๊ธฐ
- ์ถ๋ก ์ ๋ฏธ์ธ ์กฐ์ ๋ ๋ชจ๋ธ ์ฌ์ฉํ๊ธฐ
ALBERT, BART, BERT, BigBird, BigBird-Pegasus, BLOOM, CamemBERT, CANINE, ConvBERT, Data2VecText, DeBERTa, DeBERTa-v2, DistilBERT, ELECTRA, ERNIE, ErnieM, FlauBERT, FNet, Funnel Transformer, GPT-J, I-BERT, LayoutLMv2, LayoutLMv3, LED, LiLT, Longformer, LUKE, LXMERT, MarkupLM, mBART, MEGA, Megatron-BERT, MobileBERT, MPNet, MVP, Nezha, Nystrรถmformer, OPT, QDQBert, Reformer, RemBERT, RoBERTa, RoBERTa-PreLayerNorm, RoCBert, RoFormer, Splinter, SqueezeBERT, XLM, XLM-RoBERTa, XLM-RoBERTa-XL, XLNet, X-MOD, YOSO
์์ํ๊ธฐ ์ ์, ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋ชจ๋ ์ค์น๋์ด ์๋์ง ํ์ธํ์ธ์:
pip install transformers datasets evaluate
์ฌ๋ฌ๋ถ์ ๋ชจ๋ธ์ ์ ๋ก๋ํ๊ณ ์ปค๋ฎค๋ํฐ์ ๊ณต์ ํ ์ ์๋๋ก Hugging Face ๊ณ์ ์ ๋ก๊ทธ์ธํ๋ ๊ฒ์ด ์ข์ต๋๋ค. ๋ฉ์์ง๊ฐ ํ์๋๋ฉด ํ ํฐ์ ์ ๋ ฅํด์ ๋ก๊ทธ์ธํฉ๋๋ค:
>>> from huggingface_hub import notebook_login
>>> notebook_login()
SQuAD ๋ฐ์ดํฐ ์ธํธ ๊ฐ์ ธ์ค๊ธฐ[[load-squad-dataset]]
๋จผ์ ๐ค Datasets ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ SQuAD ๋ฐ์ดํฐ ์ธํธ์ ์ผ๋ถ๋ฅผ ๊ฐ์ ธ์ต๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์ ์ฒด ๋ฐ์ดํฐ ์ธํธ๋ก ํ๋ จํ๋ฉฐ ๋ ๋ง์ ์๊ฐ์ ํ ์ ํ๊ธฐ ์ ์ ๋ชจ๋ ๊ฒ์ด ์ ์๋ํ๋์ง ์คํํ๊ณ ํ์ธํ ์ ์์ต๋๋ค.
>>> from datasets import load_dataset
>>> squad = load_dataset("squad", split="train[:5000]")
๋ฐ์ดํฐ ์ธํธ์ ๋ถํ ๋ train
์ [~datasets.Dataset.train_test_split
] ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ํ๋ จ ๋ฐ์ดํฐ ์ธํธ์ ํ
์คํธ ๋ฐ์ดํฐ ์ธํธ๋ก ๋๋์ด์ค๋๋ค:
>>> squad = squad.train_test_split(test_size=0.2)
๊ทธ๋ฆฌ๊ณ ๋์ ์์๋ก ๋ฐ์ดํฐ๋ฅผ ํ๋ ์ดํด๋ด ๋๋ค:
>>> squad["train"][0]
{'answers': {'answer_start': [515], 'text': ['Saint Bernadette Soubirous']},
'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.',
'id': '5733be284776f41900661182',
'question': 'To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?',
'title': 'University_of_Notre_Dame'
}
์ด ์ค์์ ๋ช ๊ฐ์ง ์ค์ํ ํญ๋ชฉ์ด ์์ต๋๋ค:
answers
: ๋ต์ ํ ํฐ์ ์์ ์์น์ ๋ต์ ํ ์คํธcontext
: ๋ชจ๋ธ์ด ๋ต์ ์ถ์ถํ๋๋ฐ ํ์ํ ๋ฐฐ๊ฒฝ ์ง์question
: ๋ชจ๋ธ์ด ๋ตํด์ผ ํ๋ ์ง๋ฌธ
์ ์ฒ๋ฆฌ[[preprocess]]
๋ค์ ๋จ๊ณ์์๋ question
๋ฐ context
ํญ๋ชฉ์ ์ฒ๋ฆฌํ๊ธฐ ์ํด DistilBERT ํ ํฌ๋์ด์ ๋ฅผ ๊ฐ์ ธ์ต๋๋ค:
>>> from transformers import AutoTokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
์ง์ ์๋ต ํ์คํฌ์ ๊ด๋ จํด์ ํนํ ์ ์ํด์ผํ ๋ช ๊ฐ์ง ์ ์ฒ๋ฆฌ ๋จ๊ณ๊ฐ ์์ต๋๋ค:
- ๋ฐ์ดํฐ ์ธํธ์ ์ผ๋ถ ์์ ์๋ ๋ชจ๋ธ์ ์ต๋ ์
๋ ฅ ๊ธธ์ด๋ฅผ ์ด๊ณผํ๋ ๋งค์ฐ ๊ธด
context
๊ฐ ์์ ์ ์์ต๋๋ค. ๊ธด ์ํ์ค๋ฅผ ๋ค๋ฃจ๊ธฐ ์ํด์๋,truncation="only_second"
๋ก ์ค์ ํดcontext
๋ง ์๋ผ๋ด๋ฉด ๋ฉ๋๋ค. - ๊ทธ ๋ค์,
return_offset_mapping=True
๋ก ์ค์ ํด ๋ต๋ณ์ ์์๊ณผ ์ข ๋ฃ ์์น๋ฅผ ์๋์context
์ ๋งคํํฉ๋๋ค. - ๋งคํ์ ์๋ฃํ๋ฉด, ์ด์ ๋ต๋ณ์์ ์์ ํ ํฐ๊ณผ ์ข
๋ฃ ํ ํฐ์ ์ฐพ์ ์ ์์ต๋๋ค. ์คํ์
์ ์ด๋ ๋ถ๋ถ์ด
question
๊ณผcontext
์ ํด๋นํ๋์ง ์ฐพ์ ์ ์๋๋ก [~tokenizers.Encoding.sequence_ids
] ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ธ์.
๋ค์์ answer
์ ์์ ํ ํฐ๊ณผ ์ข
๋ฃ ํ ํฐ์ ์๋ผ๋ด์ context
์ ๋งคํํ๋ ํจ์๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์
๋๋ค:
>>> def preprocess_function(examples):
... questions = [q.strip() for q in examples["question"]]
... inputs = tokenizer(
... questions,
... examples["context"],
... max_length=384,
... truncation="only_second",
... return_offsets_mapping=True,
... padding="max_length",
... )
... offset_mapping = inputs.pop("offset_mapping")
... answers = examples["answers"]
... start_positions = []
... end_positions = []
... for i, offset in enumerate(offset_mapping):
... answer = answers[i]
... start_char = answer["answer_start"][0]
... end_char = answer["answer_start"][0] + len(answer["text"][0])
... sequence_ids = inputs.sequence_ids(i)
... # Find the start and end of the context
... idx = 0
... while sequence_ids[idx] != 1:
... idx += 1
... context_start = idx
... while sequence_ids[idx] == 1:
... idx += 1
... context_end = idx - 1
... # If the answer is not fully inside the context, label it (0, 0)
... if offset[context_start][0] > end_char or offset[context_end][1] < start_char:
... start_positions.append(0)
... end_positions.append(0)
... else:
... # Otherwise it's the start and end token positions
... idx = context_start
... while idx <= context_end and offset[idx][0] <= start_char:
... idx += 1
... start_positions.append(idx - 1)
... idx = context_end
... while idx >= context_start and offset[idx][1] >= end_char:
... idx -= 1
... end_positions.append(idx + 1)
... inputs["start_positions"] = start_positions
... inputs["end_positions"] = end_positions
... return inputs
๋ชจ๋ ๋ฐ์ดํฐ ์ธํธ์ ์ ์ฒ๋ฆฌ๋ฅผ ์ ์ฉํ๋ ค๋ฉด, ๐ค Datasets [~datasets.Dataset.map
] ํจ์๋ฅผ ์ฌ์ฉํ์ธ์. batched=True
๋ก ์ค์ ํด ๋ฐ์ดํฐ ์ธํธ์ ์ฌ๋ฌ ์์๋ค์ ํ ๋ฒ์ ์ฒ๋ฆฌํ๋ฉด map
ํจ์์ ์๋๋ฅผ ๋น ๋ฅด๊ฒ ํ ์ ์์ต๋๋ค. ํ์ํ์ง ์์ ์ด์ ๋ชจ๋ ์ ๊ฑฐํฉ๋๋ค:
>>> tokenized_squad = squad.map(preprocess_function, batched=True, remove_columns=squad["train"].column_names)
์ด์ [DefaultDataCollator
]๋ฅผ ์ด์ฉํด ์์ ๋ฐฐ์น๋ฅผ ์์ฑํฉ๋๋ค. ๐ค Transformers์ ๋ค๋ฅธ ๋ฐ์ดํฐ ์ฝ๋ ์ดํฐ(data collator)์ ๋ฌ๋ฆฌ, [DefaultDataCollator
]๋ ํจ๋ฉ๊ณผ ๊ฐ์ ์ถ๊ฐ ์ ์ฒ๋ฆฌ๋ฅผ ์ ์ฉํ์ง ์์ต๋๋ค:
data_collator = DefaultDataCollator()
</pt>
<tf>
```py
>>> from transformers import DefaultDataCollator
>>> data_collator = DefaultDataCollator(return_tensors="tf")
ํ๋ จ[[train]]
[Trainer
]๋ฅผ ์ด์ฉํด ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํ๋ ๊ฒ์ ์ต์ํ์ง ์๋ค๋ฉด, ์ฌ๊ธฐ์์ ๊ธฐ์ด ํํ ๋ฆฌ์ผ์ ์ดํด๋ณด์ธ์!
์ด์ ๋ชจ๋ธ ํ๋ จ์ ์์ํ ์ค๋น๊ฐ ๋์์ต๋๋ค! [AutoModelForQuestionAnswering
]์ผ๋ก DistilBERT๋ฅผ ๊ฐ์ ธ์ต๋๋ค:
>>> from transformers import AutoModelForQuestionAnswering, TrainingArguments, Trainer
>>> model = AutoModelForQuestionAnswering.from_pretrained("distilbert-base-uncased")
์ด์ ์ธ ๋จ๊ณ๋ง ๋จ์์ต๋๋ค:
- [
TrainingArguments
]์์ ํ๋ จ ํ์ดํผํ๋ผ๋ฏธํฐ๋ฅผ ์ ํฉ๋๋ค. ๊ผญ ํ์ํ ๋งค๊ฐ๋ณ์๋ ๋ชจ๋ธ์ ์ ์ฅํ ์์น๋ฅผ ์ง์ ํ๋output_dir
์ ๋๋ค.push_to_hub=True
๋ก ์ค์ ํด์ ์ด ๋ชจ๋ธ์ Hub๋ก ํธ์ํฉ๋๋ค (๋ชจ๋ธ์ ์ ๋ก๋ํ๋ ค๋ฉด Hugging Face์ ๋ก๊ทธ์ธํด์ผ ํฉ๋๋ค). - ๋ชจ๋ธ, ๋ฐ์ดํฐ ์ธํธ, ํ ํฌ๋์ด์ , ๋ฐ์ดํฐ ์ฝ๋ ์ดํฐ์ ํจ๊ป [
Trainer
]์ ํ๋ จ ์ธ์๋ค์ ์ ๋ฌํฉ๋๋ค. - [
~Trainer.train
]์ ํธ์ถํด์ ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํฉ๋๋ค.
>>> training_args = TrainingArguments(
... output_dir="my_awesome_qa_model",
... evaluation_strategy="epoch",
... learning_rate=2e-5,
... per_device_train_batch_size=16,
... per_device_eval_batch_size=16,
... num_train_epochs=3,
... weight_decay=0.01,
... push_to_hub=True,
... )
>>> trainer = Trainer(
... model=model,
... args=training_args,
... train_dataset=tokenized_squad["train"],
... eval_dataset=tokenized_squad["test"],
... tokenizer=tokenizer,
... data_collator=data_collator,
... )
>>> trainer.train()
ํ๋ จ์ด ์๋ฃ๋๋ฉด, [~transformers.Trainer.push_to_hub
] ๋งค์๋๋ฅผ ์ฌ์ฉํด ๋ชจ๋ธ์ Hub์ ๊ณต์ ํด์ ๋ชจ๋ ์ฌ๋๋ค์ด ์ฌ์ฉํ ์ ์๊ฒ ๊ณต์ ํด์ฃผ์ธ์:
>>> trainer.push_to_hub()
Keras๋ก ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํ๋ ๊ฒ์ ์ต์ํ์ง ์๋ค๋ฉด, ์ฌ๊ธฐ์์ ๊ธฐ์ด ํํ ๋ฆฌ์ผ์ ์ดํด๋ณด์ธ์!
TensorFlow๋ฅผ ์ด์ฉํ ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํ๋ ค๋ฉด ์ตํฐ๋ง์ด์ ํจ์, ํ์ต๋ฅ ์ค์ผ์ฅด ๋ฐ ๋ช ๊ฐ์ง ํ๋ จ ํ์ดํผํ๋ผ๋ฏธํฐ๋ฅผ ์ค์ ํ๋ ๊ฒ๋ถํฐ ์์ํด์ผํฉ๋๋ค:>>> from transformers import create_optimizer
>>> batch_size = 16
>>> num_epochs = 2
>>> total_train_steps = (len(tokenized_squad["train"]) // batch_size) * num_epochs
>>> optimizer, schedule = create_optimizer(
... init_lr=2e-5,
... num_warmup_steps=0,
... num_train_steps=total_train_steps,
... )
๊ทธ ๋ค์ [TFAutoModelForQuestionAnswering
]์ผ๋ก DistilBERT๋ฅผ ๊ฐ์ ธ์ต๋๋ค:
>>> from transformers import TFAutoModelForQuestionAnswering
>>> model = TFAutoModelForQuestionAnswering("distilbert-base-uncased")
[~transformers.TFPreTrainedModel.prepare_tf_dataset
]์ ์ฌ์ฉํด์ ๋ฐ์ดํฐ ์ธํธ๋ฅผ tf.data.Dataset
ํ์์ผ๋ก ๋ณํํฉ๋๋ค:
>>> tf_train_set = model.prepare_tf_dataset(
... tokenized_squad["train"],
... shuffle=True,
... batch_size=16,
... collate_fn=data_collator,
... )
>>> tf_validation_set = model.prepare_tf_dataset(
... tokenized_squad["test"],
... shuffle=False,
... batch_size=16,
... collate_fn=data_collator,
... )
compile
๋ก ํ๋ จํ ๋ชจ๋ธ์ ์ค์ ํฉ๋๋ค:
>>> import tensorflow as tf
>>> model.compile(optimizer=optimizer)
๋ง์ง๋ง์ผ๋ก ๋ชจ๋ธ์ Hub๋ก ํธ์ํ ๋ฐฉ๋ฒ์ ์ค์ ํฉ๋๋ค. [~transformers.PushToHubCallback
]์์ ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ฅผ ํธ์ํ ๊ฒฝ๋ก๋ฅผ ์ค์ ํฉ๋๋ค:
>>> from transformers.keras_callbacks import PushToHubCallback
>>> callback = PushToHubCallback(
... output_dir="my_awesome_qa_model",
... tokenizer=tokenizer,
... )
๋๋์ด ๋ชจ๋ธ ํ๋ จ์ ์์ํ ์ค๋น๊ฐ ๋์์ต๋๋ค! ํ๋ จ ๋ฐ์ดํฐ ์ธํธ์ ํ๊ฐ ๋ฐ์ดํฐ ์ธํธ, ์ํญ ์, ์ฝ๋ฐฑ์ ์ค์ ํ ํ fit
์ ์ด์ฉํด ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํฉ๋๋ค:
>>> model.fit(x=tf_train_set, validation_data=tf_validation_set, epochs=3, callbacks=[callback])
ํ๋ จ์ด ์๋ฃ๋๋ฉด ๋ชจ๋ธ์ด ์๋์ผ๋ก Hub์ ์ ๋ก๋๋์ด ๋๊ตฌ๋ ์ฌ์ฉํ ์ ์์ต๋๋ค!
์ง์ ์๋ต์ ์ํด ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํ๋ ๋ฐฉ๋ฒ์ ๋ํ ๋ ์์ธํ ์์๋ PyTorch notebook ๋๋ TensorFlow notebook์ ์ฐธ์กฐํ์ธ์.
ํ๊ฐ[[evaluate]]
์ง์ ์๋ต์ ํ๊ฐํ๋ ค๋ฉด ์๋นํ ์์ ํ์ฒ๋ฆฌ๊ฐ ํ์ํฉ๋๋ค. ์๊ฐ์ด ๋๋ฌด ๋ง์ด ๊ฑธ๋ฆฌ์ง ์๋๋ก ์ด ๊ฐ์ด๋์์๋ ํ๊ฐ ๋จ๊ณ๋ฅผ ์๋ตํฉ๋๋ค. [Trainer
]๋ ํ๋ จ ๊ณผ์ ์์ ํ๊ฐ ์์ค(evaluation loss)์ ๊ณ์ ๊ณ์ฐํ๊ธฐ ๋๋ฌธ์ ๋ชจ๋ธ์ ์ฑ๋ฅ์ ๋๋ต์ ์ผ๋ก ์ ์ ์์ต๋๋ค.
์๊ฐ์ ์ฌ์ ๊ฐ ์๊ณ ์ง์ ์๋ต ๋ชจ๋ธ์ ํ๊ฐํ๋ ๋ฐฉ๋ฒ์ ๊ด์ฌ์ด ์๋ค๋ฉด ๐ค Hugging Face Course์ Question answering ์ฑํฐ๋ฅผ ์ดํด๋ณด์ธ์!
์ถ๋ก [[inference]]
์ด์ ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํ์ผ๋ ์ถ๋ก ์ ์ฌ์ฉํ ์ ์์ต๋๋ค!
์ง๋ฌธ๊ณผ ๋ชจ๋ธ์ด ์์ธกํ๊ธฐ ์ํ๋ ๋ฌธ๋งฅ(context)๋ฅผ ์๊ฐํด๋ณด์ธ์:
>>> question = "How many programming languages does BLOOM support?"
>>> context = "BLOOM has 176 billion parameters and can generate text in 46 languages natural languages and 13 programming languages."
์ถ๋ก ์ ์ํด ๋ฏธ์ธ ์กฐ์ ํ ๋ชจ๋ธ์ ํ
์คํธํ๋ ๊ฐ์ฅ ์ฌ์ด ๋ฐฉ๋ฒ์ [pipeline
]์ ์ฌ์ฉํ๋ ๊ฒ ์
๋๋ค. ๋ชจ๋ธ์ ์ฌ์ฉํด ์ง์ ์๋ต์ ํ๊ธฐ ์ํด์ pipeline
์ ์ธ์คํด์คํํ๊ณ ํ
์คํธ๋ฅผ ์
๋ ฅํฉ๋๋ค:
>>> from transformers import pipeline
>>> question_answerer = pipeline("question-answering", model="my_awesome_qa_model")
>>> question_answerer(question=question, context=context)
{'score': 0.2058267742395401,
'start': 10,
'end': 95,
'answer': '176 billion parameters and can generate text in 46 languages natural languages and 13'}
์ํ๋ค๋ฉด pipeline
์ ๊ฒฐ๊ณผ๋ฅผ ์ง์ ๋ณต์ ํ ์๋ ์์ต๋๋ค:
>>> from transformers import AutoTokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_qa_model")
>>> inputs = tokenizer(question, context, return_tensors="pt")
๋ชจ๋ธ์ ์
๋ ฅ์ ์ ๋ฌํ๊ณ logits
์ ๋ฐํํฉ๋๋ค:
>>> from transformers import AutoModelForQuestionAnswering
>>> model = AutoModelForQuestionAnswering.from_pretrained("my_awesome_qa_model")
>>> with torch.no_grad():
... outputs = model(**inputs)
๋ชจ๋ธ์ ์ถ๋ ฅ์์ ์์ ๋ฐ ์ข ๋ฃ ์์น๊ฐ ์ด๋์ง ๊ฐ์ฅ ๋์ ํ๋ฅ ์ ์ป์ต๋๋ค:
>>> answer_start_index = outputs.start_logits.argmax()
>>> answer_end_index = outputs.end_logits.argmax()
์์ธก๋ ํ ํฐ์ ํด๋ ํด์ ๋ต์ ์ป์ต๋๋ค:
>>> predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1]
>>> tokenizer.decode(predict_answer_tokens)
'176 billion parameters and can generate text in 46 languages natural languages and 13'
ํ
์คํธ๋ฅผ ํ ํฐํํด์ TensorFlow ํ
์๋ฅผ ๋ฐํํฉ๋๋ค:
>>> from transformers import AutoTokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_qa_model")
>>> inputs = tokenizer(question, text, return_tensors="tf")
๋ชจ๋ธ์ ์
๋ ฅ์ ์ ๋ฌํ๊ณ logits
์ ๋ฐํํฉ๋๋ค:
>>> from transformers import TFAutoModelForQuestionAnswering
>>> model = TFAutoModelForQuestionAnswering.from_pretrained("my_awesome_qa_model")
>>> outputs = model(**inputs)
๋ชจ๋ธ์ ์ถ๋ ฅ์์ ์์ ๋ฐ ์ข ๋ฃ ์์น๊ฐ ์ด๋์ง ๊ฐ์ฅ ๋์ ํ๋ฅ ์ ์ป์ต๋๋ค:
>>> answer_start_index = int(tf.math.argmax(outputs.start_logits, axis=-1)[0])
>>> answer_end_index = int(tf.math.argmax(outputs.end_logits, axis=-1)[0])
์์ธก๋ ํ ํฐ์ ํด๋ ํด์ ๋ต์ ์ป์ต๋๋ค:
>>> predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1]
>>> tokenizer.decode(predict_answer_tokens)
'176 billion parameters and can generate text in 46 languages natural languages and 13'