์ด๋ฒ ์ฅ์์๋ Transformer ๋ชจ๋ธ์ ์๋กญ๊ฒ ํ๋ ํ ์์ธก์ ํ๋ ค๊ณ ํ ๋ ๋ฐ์ํ ์ ์๋ ๋ช๊ฐ์ง ์ผ๋ฐ์ ์ธ ์๋ฌ๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
์ด๋ฒ ์ฅ์์ ๋ชจ๋ธ์ ์ ์ฅ์ ํ ํ๋ฆฟ์ด ์ค๋น๋์ด ์์ต๋๋ค. ๋ง์ฝ ์ด๋ฒ ๋จ์์์ ์ฝ๋๋ฅผ ์คํํ๋ ค๋ฉด ๋ชจ๋ธ์ Huggingface Hub์ ๊ฐ์ธ ๊ณ์ ์ ๋ชจ๋ธ์ ๋ณต์ฌํด์ผ ํฉ๋๋ค. ๋ชจ๋ธ์ ๊ณ์ ์ ์ ์ฅ์์ ๋ณต์ ํ๊ธฐ ์ํด ์ฃผํผํฐ ๋ ธํธ๋ถ์์ ์๋์ ์ฝ๋๋ฅผ ์คํํ๊ฑฐ๋:
from huggingface_hub import notebook_login
notebook_login()
๋๋ ์๋์ ์คํฌ๋ฆฝํธ๋ฅผ ์ํ๋ ํฐ๋ฏธ๋์์ ์คํํฉ๋๋ค:
huggingface-cli login
ํฐ๋ฏธ๋์์ ์์ด๋์ ๋น๋ฐ๋ฒํธ๋ฅผ ์ ๋ ฅํ๋ ํ๋กฌํํธ๊ฐ ๋ํ๋๋ฉฐ, ์๋ณ ํ ํฐ์ ~/.cache/huggingface/์ ์ ์ฅ๋ฉ๋๋ค. ํ๋ฒ ๋ก๊ทธ์ธ ํ๊ณ ๋๋ฉด ๋ชจ๋ธ์ ์ ์ฅ์ ํ ํ๋ฆฟ์ ์๋์ ํจ์๋ฅผ ์ฌ์ฉํด ๋ณต์ฌํ ์ ์์ต๋๋ค:
from distutils.dir_util import copy_tree
from huggingface_hub import Repository, snapshot_download, create_repo, get_full_repo_name
def copy_repository_template():
# Clone the repo and extract the local path
template_repo_id = "lewtun/distilbert-base-uncased-finetuned-squad-d5716d28"
commit_hash = "be3eaffc28669d7932492681cd5f3e8905e358b4"
template_repo_dir = snapshot_download(template_repo_id, revision=commit_hash)
# Create an empty repo on the Hub
model_name = template_repo_id.split("/")[1]
create_repo(model_name, exist_ok=True)
# Clone the empty repo
new_repo_id = get_full_repo_name(model_name)
new_repo_dir = model_name
repo = Repository(local_dir=new_repo_dir, clone_from=new_repo_id)
# Copy files
copy_tree(template_repo_dir, new_repo_dir)
# Push to Hub
repo.push_to_hub()
์ด์ copy_repository_template()
๋ฅผ ํธ์ถํ๋ฉด ๋ชจ๋ธ ์ ์ฅ์์ ํ
ํ๋ฆฟ์ด ๊ณ์ ์ ๋ณต์ฌ ๋ฉ๋๋ค.
Transformer ๋ชจ๋ธ๋ค์ ๋ฉ์ง ๋๋ฒ๊น ์ธ๊ณ๋ก ์ฌ์ ์ ๋ ๋๊ธฐ ์ํด, ๋ค์์ ์๋๋ฆฌ์ค๋ฅผ ์๊ฐํด๋ณด์ธ์: ์ฌ๋ฌ๋ถ์ E-commerce ์ฌ์ดํธ์ ๊ณ ๊ฐ์ด ์๋น์ ์ํ์ ๋ํ ๋ต๋ณ์ ์ฐพ๊ธฐ ์ํ ์ง๋ฌธ ๋ฐ ๋ต๋ณ ํ๋ก์ ํธ์์ ๋๋ฃ์ ํจ๊ป ์ผํ๊ณ ์์ผ๋ฉฐ, ๋๋ฃ๊ฐ ๋น์ ์๊ฒ ๋ค์๊ณผ ๊ฐ์ ๋ฉ์ธ์ง๋ฅผ ๋ณด๋์ต๋๋ค:
์๋ ํ์ธ์! Hugging Face ์ฝ์ค์ ์๋ 7 ๋จ์์ ๊ธฐ์ ์ ํ์ฉํด์ ์คํ์ ํด๋ดค๋๋ฐ, SQuAD์์ ์ข์ ๊ฒฐ๊ณผ๋ฅผ ์ป์์ต๋๋ค. ์ ํฌ ํ๋ก์ ํธ๋ฅผ ์ด ๋ชจ๋ธ๋ก ์์ํ ์ ์๋ค๊ณ ์๊ฐ์ด ๋ฉ๋๋ค. ํ๋ธ์ ์๋ ๋ชจ๋ธ ์์ด๋๋ โlewtun/distillbert-base-uncased-finetuned-squad-d5716d28โ ์ ๋๋ค. ๋ง์ ๊ป ํ ์คํธ ํด๋ณด์ธ์. :)
๐ค Transformers์ pipeline
์ ์ฌ์ฉ๋ ๋ชจ๋ธ์ ๋ถ๋ฌ์ค๊ธฐ ์ํด ์ฐ์ ๊ณ ๋ คํด์ผ ํ ๊ฒ์ด ์์ต๋๋ค:
from transformers import pipeline
model_checkpoint = get_full_repo_name("distillbert-base-uncased-finetuned-squad-d5716d28")
reader = pipeline("question-answering", model=model_checkpoint)
"""
OSError: Can't load config for 'lewtun/distillbert-base-uncased-finetuned-squad-d5716d28'. make sure that:
- 'lewtun/distillbert-base-uncased-finetuned-squad-d5716d28'์ด๋ผ๋ ๋ชจ๋ธ๋ช
์ด 'https://huggingface.co/models'์ ์กด์ฌํ๋์ง ํ์ธํ๊ฑฐ๋
'lewtun/distillbert-base-uncased-finetuned-squad-d5716d28'์ด๋ผ๋ ๊ฒฝ๋ก ๋๋ ํด๋๊ฐ config.json ํ์ผ ํฌํจํ๊ณ ์๋์ง ํ์ธํ์ธ์.
"""
์ ์ด๋ฐ, ๋ญ๊ฐ ์๋ชป๋ ๊ฒ ๊ฐ๋ค์! ๋ง์ฝ ํ๋ก๊ทธ๋๋ฐ์ด ์ฒ์์ด๋ผ๋ฉด, ์ด๋ฐ ์ข
๋ฅ์ ์๋ฌ๊ฐ ์ฒ์์๋ ๋ค์ ์ ๋นํ๊ฒ(OSError
๋ ๋๋์ฒด..) ๋ณด์ผ ์๋ ์์ต๋๋ค. ์ฌ๊ธฐ์์ ๋ณด์ด๋ ์๋ฌ๋ ํ์ด์ฌ์ traceback(stack trace๋ก ์๋ ค์ ธ์์)์ผ๋ก ๋ถ๋ฆฌ๋ ์ข ๋ ํฐ ์๋ฌ ๋ฆฌํฌํธ์ ๋ง์ง๋ง ๋ถ๋ถ ์
๋๋ค. ์๋ฅผ ๋ค์ด ์ด ์ฝ๋๋ฅผ Google์ Colab์์ ์คํํ๋ค๋ฉด ์๋์ ๊ฐ์ ์คํฌ๋ฆฐ์ท์ ๋ณด๊ฒ ๋ ๊ฒ๋๋ค:
์ด ๋ฆฌํฌํธ์๋ ๋ง์ ์ ๋ณด๋ฅผ ๋ด๊ณ ์์ผ๋, ๊ฐ์ด ํต์ฌ ๋ถ๋ถ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค. ์ฐ์ ๋ช
์ฌํด์ผํ ๊ฒ์ tracebacks์ ์๋๋ถํฐ ์๋ก ์ฝ์ด์ผ ํฉ๋๋ค. ์ด๋ฌํ ๋ง์ ์์ด ํ
์คํธ๋ฅผ ์์์ ์๋๋ก ์ฝ์ด์ค๊ณค ํ๋ค๋ฉด ์ด์ํ๊ฒ ๋ค๋ฆด ์ ์๊ฒ ์ง๋ง ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ฅผ ๋ค์ด๋ก๋ ํ ๋ pipeline
์ด ๋ง๋๋ ํจ์ ํธ์ถ ์์๋ฅผ ๋ณด์ฌ์ฃผ๋ traceback์ ๋ฐ์ํ๊ธฐ ๋๋ฌธ์
๋๋ค. ๋ด๋ถ์์ pipeline
์ด ์๋ํ๋ ๋ฐฉ์์ ๋ํ ์์ธํ ๋ด์ฉ์ ๋จ์ 2๋ฅผ ์ฐธ๊ณ ํ์ธ์.
Google Colab์ traceback์์ โ6 framesโ ์ฃผ๋ณ์ ํ๋ ์์๋ฅผ ๋ณด์ จ๋์? traceback์ โframesโ๋ก ์์ถํ๋ Colab์ ํน๋ณํ ๊ธฐ๋ฅ์ ๋๋ค. ๋ง์ฝ ์ค๋ฅ์ ์์ธ์ ์ฐพ์ ์ ์๋ค๋ฉด, ๋๊ฐ์ ์์ ํ์ดํ๋ฅผ ํด๋ฆญํด์ ์ ์ฒด traceback์ ํ์ฅ๋์ด ์๋์ง ์ฌ๋ถ๋ฅผ ํ์ธํ์ธ์.
์ฆ ๋ง์ง๋ง ์๋ฌ ๋ฉ์์ง์ ๋ฐ์ํ ์์ธ์ ์ด๋ฆ์ ๊ฐ๋ฆฌํค๋ traceback์ ๋ง์ง๋ง ์ค์ ๋ปํฉ๋๋ค. ์ด ๊ฒฝ์ฐ์ ์์ธ ์ ํ์ ์์คํ ๊ด๋ จ ์ค๋ฅ๋ฅผ ๋ํ๋ด๋ OS Error ์ ๋๋ค. ์ฒจ๋ถ๋ ์ค๋ฅ ๋ฉ์์ง๋ฅผ ์ฝ์ผ๋ฉด ๋ชจ๋ธ์ config.json ํ์ผ์ ๋ฌธ์ ๊ฐ ์๋ ๊ฒ์ผ๋ก ๋ณด์ด๋ฉฐ ์ด๋ฅผ ์์ ํ๊ธฐ ์ํด ๋ ๊ฐ์ง ์ ํ์ง๊ฐ ์์ต๋๋ค:
"""
make sure that:
- 'lewtun/distillbert-base-uncased-finetuned-squad-d5716d28' is a correct model identifier listed on 'https://huggingface.co/models'
- or 'lewtun/distillbert-base-uncased-finetuned-squad-d5716d28' is the correct path to a directory containing a config.json file
"""
๐ก ์ดํดํ๊ธฐ ์ด๋ ค์ด ์๋ฌ ๋ฉ์์ง๋ฅผ ์ ํ๊ฒ ๋๋ค๋ฉด, ๋ฉ์ธ์ง๋ฅผ ๋ณต์ฌํด์ Google ๋๋ ์คํ์ค๋ฒํ๋ก์ฐ ๊ฒ์์ฐฝ์ ๋ถ์ฌ ๋ฃ๊ธฐ๋ง ํ์ธ์(๋ค ์ง์งญ๋๋ค!). ์ด๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ ์ฒซ ์ฌ๋์ด ์๋ ๊ฐ๋ฅ์ฑ์ด ๋์๋ฟ๋๋ฌ, ์ปค๋ฎค๋ํฐ์ ๋ค๋ฅธ ์ฌ๋๋ค์ด ๊ฒ์ํ ์๋ฃจ์ ์ ์ฐพ๋ ์ข์ ๋ฐฉ๋ฒ์ ๋๋ค. ์๋ฅผ ๋ค์ด, ์คํ์ค๋ฒํ๋ก์ฐ์์ โOSError: Canโt load config forโ๋ฅผ ๊ฒ์ํ๋ฉด ์ฌ๋ฌ ํด๋ต์ ์ ๊ณตํ๋ฉฐ ๋ฌธ์ ํด๊ฒฐ์ ์ํ ์ถ๋ฐ์ ์ผ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ฒซ ๋ฒ์งธ ์ ์์ ๋ชจ๋ธ ID๊ฐ ์ค์ ๋ก ์ ํํ์ง ํ์ธํ๋๋ก ์์ฒญํ๋ ๊ฒ์ผ๋ก ๋น์ฆ๋์ค์ ์ฒซ ์์๋ ์๋ณ์(๋ชจ๋ธ ์ด๋ฆ)๋ฅผ ๋ณต์ฌํ์ฌ Hub์ ๊ฒ์ ์ฐฝ์ ๋ถ์ฌ๋ฃ๋ ๊ฒ์ ๋๋ค:
์, ๋๋ฃ์ ๋ชจ๋ธ์ด ํ๋ธ์ ์๋ ๊ฒ ๊ฐ์ต๋๋คโฆ ์ํ, ๋ชจ๋ธ์ ์ด๋ฆ์ ์คํ๊ฐ ์์์ต๋๋ค! DistilBERT๋ ์ด๋ฆ์ โlโ์ด ํ๋๋ง ์์ผ๋ฏ๋ก ์ด๋ฅผ ์์ ํ๊ณ ๋์ โlewtun/distilbert-base-uncased-finetuned-squad-d5716d28โ์ ์ฐพ์๋ณด๊ฒ ์ต๋๋ค:
์ข์ต๋๋ค, ์ฑ๊ณตํ๊ตฐ์. ์ด์ ์ฌ๋ฐ๋ฅธ ๋ชจ๋ธ ID๋ก ๋ชจ๋ธ์ ๋ค์ ๋ค์ด๋ก๋ ํด๋ด ์๋ค:
model_checkpoint = get_full_repo_name("distilbert-base-uncased-finetuned-squad-d5716d28")
reader = pipeline("question-answering", model=model_checkpoint)
"""
OSError: Can't load config for 'lewtun/distilbert-base-uncased-finetuned-squad-d5716d28'. Make sure that:
- 'lewtun/distilbert-base-uncased-finetuned-squad-d5716d28' is a correct model identifier listed on 'https://huggingface.co/models'
- or 'lewtun/distilbert-base-uncased-finetuned-squad-d5716d28' is the correct path to a directory containing a config.json file
"""
์์ค ๋ ์คํจ์
๋๋ค. ๋จธ์ ๋ฌ๋ ์์ง๋์ด์ ์ผ์์ ์ค์ ๊ฒ์ ํ์ํฉ๋๋ค! ๋ชจ๋ธ ID๋ฅผ ์์ ํ์ผ๋ฏ๋ก ๋ฌธ์ ๋ ์ ์ฅ์ ์์ฒด์ ์์ด์ผ ํฉ๋๋ค. ๐ค Hub์ ์ ์ฅ์ ์ปจํ
์ธ ์ ๋น ๋ฅด๊ฒ ์ก์ธ์คํ๋ ๋ฐฉ๋ฒ์ huggingface_hub
๋ผ์ด๋ธ๋ฌ๋ฆฌ์ list_repo_files()
ํจ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค:
from huggingface_hub import list_repo_files
list_repo_files(repo_id=model_checkpoint)
['.gitattributes', 'README.md', 'pytorch_model.bin', 'special_tokens_map.json', 'tokenizer_config.json', 'training_args.bin', 'vocab.txt']
ํฅ๋ฏธ๋กญ๋ค์ โ ์ด ์ ์ฅ์์๋ config.json๊ฐ ๋ณด์ด์ง ์์ต๋๋ค! ์ฐ๋ฆฌ์ pipeline
์ด ๋ชจ๋ธ์ ๋ถ๋ฌ์ฌ ์ ์๋ ๊ฒ์ด ๋น์ฐํ๊ตฐ์; ๋๋ฃ๊ฐ ํ์ธํ๋ ํ์ ํ๋ธ์ ํธ์ํ๋ ๊ฒ์ ์์ด๋ฒ๋ฆฐ ๋ชจ์์
๋๋ค. ์ด ๊ฒฝ์ฐ, ๋ฌธ์ ๋ ๋งค์ฐ ๊ฐ๋จํ๊ฒ ํด๊ฒฐํ ์ ์์ต๋๋ค: ๋๋ฃ์๊ฒ ํ์ผ์ ์ถ๊ฐํ๋๋ก ์์ฒญํ๊ฑฐ๋, ์ฌ์ ํ๋ จ(pretrained)๋ ๋ชจ๋ธ์ด distilbert-base-uncased
์ธ ๊ฒ์ ํ์ธ ํ ์ ์์ผ๋ฏ๋ก, ์ด ๋ชจ๋ธ์ ๋ํ config๋ฅผ ๋ค์ด๋ก๋ํ๊ณ ์ ์ฅ์์ ํธ์ํ์ฌ ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋๋์ง ํ์ธํ ์ ์์ต๋๋ค. ์๋ ํด๋ด
์๋ค. ๋จ์ 2์์ ๋ฐฐ์ด ๊ธฐ์ ์ ์ฌ์ฉํด AutoConfig
ํด๋์ค๋ก ๋ชจ๋ธ์ config ํ์ผ์ ๋ค์ด๋ก๋ํ ์ ์์ต๋๋ค.
from transformers import AutoConfig
pretrained_checkpoint = "distilbert-base-uncased"
config = AutoConfig.from_pretrained(pretrained_checkpoint)
๐จ ์ฌ๊ธฐ์์ ํ๋ ์ ๊ทผ ๋ฐฉ์์ ๋๋ฃ๊ฐ โdistilbert-base-uncasedโ์ config๋ฅผ ์์ ํ์ ์ ์์ผ๋ฏ๋ก ์ด ์ ๊ทผ ๋ฐฉ์์ ์์ ํ์ง ์์ต๋๋ค. ์ฐ๋ฆฌ๋ ๋๋ฃ์๊ฒ ๋จผ์ ํ์ธํ๊ณ ์ถ๊ฒ ์ง๋ง, ์ด๋ฒ ์ฅ์์์ ๋ชฉ์ ์, ๋๋ฃ๊ฐ ๋ํดํธ config๋ฅผ ์ฌ์ฉํ๋ค๊ณ ๊ฐ์ ํ๊ฒ ์ต๋๋ค.
๊ทธ๋ฐ ๋ค์ config ํด๋์ค์ push_to_hub()
๊ธฐ๋ฅ์ ์ฌ์ฉํด์ config ํ์ผ์ ๋ชจ๋ธ ์ ์ฅ์๋ก ํธ์ํ ์ ์์ต๋๋ค:
We can then push this to our model repository with the configurationโs push_to_hub()
function:
config.push_to_hub(model_checkpoint, commit_message="Add config.json")
์ด์ main
๋ธ๋์น์ ์ต์ ์ปค๋ฐ์์ ๋ชจ๋ธ์ ๋ก๋ํด์ ์๋ ์ฌ๋ถ๋ฅผ ํ
์คํธํ ์ ์์ต๋๋ค:
reader = pipeline("question-answering", model=model_checkpoint, revision="main")
context = r"""
Extractive Question Answering is the task of extracting an answer from a text
given a question. An example of a question answering dataset is the SQuAD
dataset, which is entirely based on that task. If you would like to fine-tune a
model on a SQuAD task, you may leverage the
examples/pytorch/question-answering/run_squad.py script.
๐ค Transformers is interoperable with the PyTorch, TensorFlow, and JAX
frameworks, so you can use your favourite tools for a wide variety of tasks!
"""
question = "What is extractive question answering?"
reader(question=question, context=context)
{'score': 0.38669535517692566,
'start': 34,
'end': 95,
'answer': 'the task of extracting an answer from a text given a question'}
์ ํ, ๋์ํ๋ค์! ๋ฐฉ๊ธ ๋ฐฐ์ด ๋ด์ฉ์ ์์ฝ ํด๋ณด๊ฒ ์ต๋๋ค:
huggingface_hub
// ๐ค Hub?
์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ํ๋ธ์ ์ ์ฅ์์ ์ํธ ์์ฉํ๊ณ ๋๋ฒ๊ทธํ๋ ๋ฐ ์ฌ์ฉํ ์ ์๋ ํด๋ค์ ์ ๊ณตํฉ๋๋ค.์ด์ ํ์ดํ๋ผ์ธ์ ๋๋ฒ๊น ํ๋ ๋ฐฉ๋ฒ์ ์์์ผ๋ ๋ชจ๋ธ ์์ฒด์ forward pass์์ ๋ ๊น๋ค๋ก์ด ์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
โpipelineโ์ ๋น ๋ฅด๊ฒ ์์ธก์ ์์ฑํด์ผ ํ๋ ๋๋ถ๋ถ์ ์ ํ๋ฆฌ์ผ์ด์
์ ์ ํฉํ์ง๋ง ๋๋ก๋ ๋ชจ๋ธ์ logits๊ฐ์ ์ ๊ทผํด์ผ ํ ์๋ ์์ต๋๋ค(์: ์ ์ฉํ๋ ค๋ ์ปค์คํ
ํ์ฒ๋ฆฌ ๊ณผ์ ์ด ์๋ ๊ฒฝ์ฐ). ์ด ๊ฒฝ์ฐ ๋ฌด์์ด ์๋ชป๋ ์ ์๋์ง ์์๋ณด๊ธฐ ์ํด ๋จผ์ pipeline
์์ ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ฅผ ๊ฐ์ ธ์ ๋ณด๊ฒ ์ต๋๋ค:
tokenizer = reader.tokenizer model = reader.model
๋ค์์ผ๋ก ์ง๋ฌธ์ด ํ์ํฉ๋๋ค. ์ ํธํ๋ ํ๋ ์์ํฌ๊ฐ ์ง์๋๋์ง ์ดํด๋ณด๊ฒ ์ต๋๋ค:
question = "Which frameworks can I use?"
๋จ์ 7์์ ๋ณด์๋ฏ์ด ์ผ๋ฐ์ ์ธ ๋จ๊ณ๋ ์ ๋ ฅ์ ํ ํฐํํ๊ณ ์์๊ณผ ๋ง์ง๋ง ํ ํฐ์ logits๋ฅผ ์ถ์ถํ ๋ค์ ์๋ต ๋ถ๋ถ์ ๋์ฝ๋ฉํ๋ ๊ฒ์ ๋๋ค:
import torch
inputs = tokenizer(question, context, add_special_tokens=True)
input_ids = inputs["input_ids"][0]
outputs = model(**inputs)
answer_start_scores = outputs.start_logits
answer_end_scores = outputs.end_logits
# Get the most likely beginning of answer with the argmax of the score
answer_start = torch.argmax(answer_start_scores)
# Get the most likely end of answer with the argmax of the score
answer_end = torch.argmax(answer_end_scores) + 1
answer = tokenizer.convert_tokens_to_string(
tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end])
)
print(f"Question: {question}")
print(f"Answer: {answer}")
"""
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/var/folders/28/k4cy5q7s2hs92xq7_h89_vgm0000gn/T/ipykernel_75743/2725838073.py in <module>
1 inputs = tokenizer(question, text, add_special_tokens=True)
2 input_ids = inputs["input_ids"]
----> 3 outputs = model(**inputs)
4 answer_start_scores = outputs.start_logits
5 answer_end_scores = outputs.end_logits
~/miniconda3/envs/huggingface/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1049 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1050 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1051 return forward_call(*input, **kwargs)
1052 # Do not call functions when jit is used
1053 full_backward_hooks, non_full_backward_hooks = [], []
~/miniconda3/envs/huggingface/lib/python3.8/site-packages/transformers/models/distilbert/modeling_distilbert.py in forward(self, input_ids, attention_mask, head_mask, inputs_embeds, start_positions, end_positions, output_attentions, output_hidden_states, return_dict)
723 return_dict = return_dict if return_dict is not None else self.config.use_return_dict
724
--> 725 distilbert_output = self.distilbert(
726 input_ids=input_ids,
727 attention_mask=attention_mask,
~/miniconda3/envs/huggingface/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1049 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1050 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1051 return forward_call(*input, **kwargs)
1052 # Do not call functions when jit is used
1053 full_backward_hooks, non_full_backward_hooks = [], []
~/miniconda3/envs/huggingface/lib/python3.8/site-packages/transformers/models/distilbert/modeling_distilbert.py in forward(self, input_ids, attention_mask, head_mask, inputs_embeds, output_attentions, output_hidden_states, return_dict)
471 raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
472 elif input_ids is not None:
--> 473 input_shape = input_ids.size()
474 elif inputs_embeds is not None:
475 input_shape = inputs_embeds.size()[:-1]
AttributeError: 'list' object has no attribute 'size'
"""
์ด๋ฐ, ์ฝ๋์ ๋ฒ๊ทธ๊ฐ ์๋ ๊ฒ ๊ฐ๋ค์! ํ์ง๋ง ์ฝ๊ฐ์ ๋๋ฒ๊น ์ ๋๋ ต์ง ์์ต๋๋ค. ๋ ธํธ๋ถ์์ ํ์ด์ฌ ๋๋ฒ๊ฑฐ๋ฅผ ์ฌ์ฉ ํ ์ ์์ต๋๋ค:
๋๋ ํฐ๋ฏธ๋์์:
์ฌ๊ธฐ์์ ์ค๋ฅ ๋ฉ์์ง๋ฅผ ์ฝ์ผ๋ฉด 'list' ๊ฐ์ฒด์๋ 'size' ์์ฑ์ด ์์ผ๋ฉฐ
model(**inputs)โ์์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ๋ผ์ธ์ ๊ฐ๋ฆฌํค๋ -->
ํ์ดํ๋ฅผ ๋ณผ ์ ์์ต๋๋ค. .Python ๋๋ฒ๊ฑฐ๋ฅผ ์ฌ์ฉํ์ฌ ๋ํ์์ผ๋ก ๋๋ฒ๊ทธํ ์ ์์ง๋ง ์ง๊ธ์ ๋จ์ํ
inputs` ๋ถ๋ถ์ ์ฌ๋ผ์ด์คํ์ฌ ์ด๋ค ๊ฐ์ด ์๋์ง ๋ณผ ๊ฒ์
๋๋ค:
inputs["input_ids"][:5]
[101, 2029, 7705, 2015, 2064]
ํ์คํ ์ผ๋ฐ์ ์ธ Python list
์ฒ๋ผ ๋ณด์ด์ง๋ง ํ์
์ ๋ค์ ํ์ธํฉ์๋ค:
type(inputs["input_ids"])
list
๋ค, ํ์คํ ํ์ด์ฌ์ list
์
๋๋ค. ๋ฌด์์ด ์๋ชป๋์์๊น์? ๋จ์ 2์์ ๐ค Transformers์ AutoModelForXxx
ํด๋์ค๋ tensors(PyTorch ๋๋ TensorFlow ํฌํจ)์์ ์๋ํ๋ฉฐ tensor์ dimensions๋ฅผ ์ถ์ถํ๊ธฐ ์ํด ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ์ผ๋ก PyTorch์ Tensor.size()
๋ฅผ ํ์ฉํฉ๋๋ค. ์ด๋ค ๋ผ์ธ์ด ์์ธ๋ฅผ ๋ฐ์์์ผฐ๋์ง ์์๋ณด๊ธฐ ์ํด traceback์ ๋ค์ ์ดํด๋ณด๊ฒ ์ต๋๋ค:
~/miniconda3/envs/huggingface/lib/python3.8/site-packages/transformers/models/distilbert/modeling_distilbert.py in forward(self, input_ids, attention_mask, head_mask, inputs_embeds, output_attentions, output_hidden_states, return_dict)
471 raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
472 elif input_ids is not None:
--> 473 input_shape = input_ids.size()
474 elif inputs_embeds is not None:
475 input_shape = inputs_embeds.size()[:-1]
AttributeError: 'list' object has no attribute 'size'
์ฝ๋๊ฐ input_ids.size()
๋ฅผ ํธ์ถํ๋ ค๊ณ ํ์ง๋ง, Python list
์์๋ ์ ๋ ๋์ํ์ง ์์ต๋๋ค. ์ด ๋ฌธ์ ๋ฅผ ์ด๋ป๊ฒ ํด๊ฒฐํ ์ ์์๊น์? ์คํ์ค๋ฒํ๋ก์ฐ์์ ์ค๋ฅ ๋ฉ์์ง๋ฅผ ๊ฒ์ํ๋ฉด ๊ฝค ๋ง์ ๊ด๋ จ ํด๊ฒฐ์ฑ
์ ์ ๊ณตํฉ๋๋ค. ์ฒซ ๋ฒ์งธ ์ง๋ฌธ์ ํด๋ฆญํ๋ฉด ์๋ ์คํฌ๋ฆฐ์ท์ ํ์๋ ๋ต๋ณ๊ณผ ํจ๊ป ์ฐ๋ฆฌ์ ์ ์ฌํ ์ง๋ฌธ์ด ํ์๋ฉ๋๋ค:
๋๋ต์ ํ ํฌ๋์ด์ ์ return_tensors='pt'
๋ฅผ ์ถ๊ฐํ ๊ฒ์ ๊ถ์ฅํ๋๋ฐ, ์ด๊ฒ ์๋ํ๋์ง ํ์ธํด ๋ณด๊ฒ ์ต๋๋ค:
inputs = tokenizer(question, context, add_special_tokens=True, return_tensors="pt")
input_ids = inputs["input_ids"][0]
outputs = model(**inputs)
answer_start_scores = outputs.start_logits
answer_end_scores = outputs.end_logits
# Get the most likely beginning of answer with the argmax of the score
answer_start = torch.argmax(answer_start_scores)
# Get the most likely end of answer with the argmax of the score
answer_end = torch.argmax(answer_end_scores) + 1
answer = tokenizer.convert_tokens_to_string(
tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end])
)
print(f"Question: {question}")
print(f"Answer: {answer}")
"""
Question: Which frameworks can I use?
Answer: pytorch, tensorflow, and jax
"""
์ ๋์ํ๋ค์! ์ด๊ฒ ๋ฐ๋ก ์คํ์ค๋ฒํ๋ก์ฐ๊ฐ ์ผ๋ง๋ ์ ์ฉํ์ง ๋ณด์ฌ์ฃผ๋ ์ข์ ์์ ๋๋ค. ์ ์ฌํ ๋ฌธ์ ๋ฅผ ์๋ณํ์ฌ ์ปค๋ฎค๋ํฐ์ ๋ค๋ฅธ ์ฌ๋๋ค์ ๊ฒฝํ์ ํ์ฉํ ์ ์์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ด์ ๊ฐ์ ๊ฒ์์ด ํญ์ ์ ์ ํ ๋ต๋ณ์ ์ ๊ณตํ๋ ๊ฒ์ ์๋๋๋ค. ์ด๋ฌํ ๊ฒฝ์ฐ์ ๋ฌด์์ ํ ์ ์์๊น์? ๋คํํ๋ Hugging Face forums์ ์ฌ๋ฌ๋ถ์ ๋ฐ๊ธฐ๊ณ ๋์์ค ์ ์๋ ๊ฐ๋ฐ์ ์ปค๋ฎค๋ํฐ๊ฐ ์์ต๋๋ค! ๋ค์ ์ฅ์์๋ ๋ต๋ณ์ ์ป์ ์ ์๋ ์ข์ ํฌ๋ผ ์ง๋ฌธ์ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.