huggingface--transformers
e06fe8e8c6
Secret Leaks / trufflehog (push) Failing after 1s
Build documentation / build (push) Failing after 1s
Build documentation / build_other_lang (push) Failing after 0s
CodeQL Security Analysis / CodeQL Analysis (push) Failing after 0s
PR CI / pr-ci (push) Failing after 1s
Slow tests on important models (on Push - A10) / Get all modified files (push) Failing after 1s
Slow tests on important models (on Push - A10) / Model CI (push) Has been skipped
Self-hosted runner (benchmark) / Benchmark (aws-g5-4xlarge-cache) (push) Has been cancelled
New model PR merged notification / Notify new model (push) Has been cancelled
Update Transformers metadata / build_and_package (push) Has been cancelled
312 ่ก
15 KiB
Markdown
312 ่ก
15 KiB
Markdown
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
|
the License. You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
|
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
specific language governing permissions and limitations under the License.
|
|
|
|
โ ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
|
rendered properly in your Markdown viewer.
|
|
|
|
-->
|
|
|
|
# ์ฌ์ ํ์ต๋ ๋ชจ๋ธ ๋ฏธ์ธ ํ๋ํ๊ธฐ[[finetune-a-pretrained-model]]
|
|
|
|
[[open-in-colab]]
|
|
|
|
์ฌ์ ํ์ต๋ ๋ชจ๋ธ์ ์ฌ์ฉํ๋ฉด ์๋นํ ์ด์ ์ด ์์ต๋๋ค. ๊ณ์ฐ ๋น์ฉ๊ณผ ํ์๋ฐ์๊ตญ์ ์ค์ด๊ณ , ์ฒ์๋ถํฐ ๋ชจ๋ธ์ ํ์ต์ํฌ ํ์ ์์ด ์ต์ ๋ชจ๋ธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๐ค Transformers๋ ๋ค์ํ ์์
์ ์ํด ์ฌ์ ํ์ต๋ ์์ฒ ๊ฐ์ ๋ชจ๋ธ์ ์ก์ธ์คํ ์ ์์ต๋๋ค. ์ฌ์ ํ์ต๋ ๋ชจ๋ธ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, ์์ ์ ์์
๊ณผ ๊ด๋ จ๋ ๋ฐ์ดํฐ์
์ ์ฌ์ฉํด ํ์ตํฉ๋๋ค. ์ด๊ฒ์ ๋ฏธ์ธ ํ๋์ด๋ผ๊ณ ํ๋ ๋งค์ฐ ๊ฐ๋ ฅํ ํ๋ จ ๊ธฐ๋ฒ์
๋๋ค. ์ด ํํ ๋ฆฌ์ผ์์๋ ๋น์ ์ด ์ ํํ ๋ฅ๋ฌ๋ ํ๋ ์์ํฌ๋ก ์ฌ์ ํ์ต๋ ๋ชจ๋ธ์ ๋ฏธ์ธ ํ๋ํฉ๋๋ค:
|
|
|
|
* ๐ค Transformers๋ก ์ฌ์ ํ์ต๋ ๋ชจ๋ธ ๋ฏธ์ธ ํ๋ํ๊ธฐ [`Trainer`].
|
|
* Keras๋ฅผ ์ฌ์ฉํ์ฌ TensorFlow์์ ์ฌ์ ํ์ต๋ ๋ชจ๋ธ์ ๋ฏธ์ธ ํ๋ํ๊ธฐ.
|
|
* ๊ธฐ๋ณธ PyTorch์์ ์ฌ์ ํ์ต๋ ๋ชจ๋ธ์ ๋ฏธ์ธ ํ๋ํ๊ธฐ.
|
|
|
|
<a id='data-processing'></a>
|
|
|
|
## ๋ฐ์ดํฐ์
์ค๋น[[prepare-a-dataset]]
|
|
|
|
<Youtube id="_BZearw7f0w"/>
|
|
|
|
์ฌ์ ํ์ต๋ ๋ชจ๋ธ์ ๋ฏธ์ธ ํ๋ํ๊ธฐ ์ํด์ ๋ฐ์ดํฐ์
์ ๋ค์ด๋ก๋ํ๊ณ ํ๋ จํ ์ ์๋๋ก ์ค๋นํ์ธ์. ์ด์ ํํ ๋ฆฌ์ผ์์ ํ๋ จ์ ์ํด ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ๋๋ ธ๋๋ฐ, ์ง๊ธ์ด ๋ฐฐ์ธ ๊ฑธ ๋์ง์ ๊ธฐํ์
๋๋ค!
|
|
|
|
๋จผ์ [Yelp ๋ฆฌ๋ทฐ](https://huggingface.co/datasets/Yelp/yelp_review_full) ๋ฐ์ดํฐ ์ธํธ๋ฅผ ๋ก๋ํฉ๋๋ค:
|
|
|
|
```py
|
|
>>> from datasets import load_dataset
|
|
|
|
>>> dataset = load_dataset("yelp_review_full")
|
|
>>> dataset["train"][100]
|
|
{'label': 0,
|
|
'text': 'My expectations for McDonalds are t rarely high. But for one to still fail so spectacularly...that takes something special!\\nThe cashier took my friends\'s order, then promptly ignored me. I had to force myself in front of a cashier who opened his register to wait on the person BEHIND me. I waited over five minutes for a gigantic order that included precisely one kid\'s meal. After watching two people who ordered after me be handed their food, I asked where mine was. The manager started yelling at the cashiers for \\"serving off their orders\\" when they didn\'t have their food. But neither cashier was anywhere near those controls, and the manager was the one serving food to customers and clearing the boards.\\nThe manager was rude when giving me my order. She didn\'t make sure that I had everything ON MY RECEIPT, and never even had the decency to apologize that I felt I was getting poor service.\\nI\'ve eaten at various McDonalds restaurants for over 30 years. I\'ve worked at more than one location. I expect bad days, bad moods, and the occasional mistake. But I have yet to have a decent experience at this store. It will remain a place I avoid unless someone in my party needs to avoid illness from low blood sugar. Perhaps I should go back to the racially biased service of Steak n Shake instead!'}
|
|
```
|
|
|
|
ํ
์คํธ๋ฅผ ์ฒ๋ฆฌํ๊ณ ์๋ก ๋ค๋ฅธ ๊ธธ์ด์ ์ํ์ค ํจ๋ฉ ๋ฐ ์๋ผ๋ด๊ธฐ ์ ๋ต์ ํฌํจํ๋ ค๋ฉด ํ ํฌ๋์ด์ ๊ฐ ํ์ํฉ๋๋ค. ๋ฐ์ดํฐ์
์ ํ ๋ฒ์ ์ฒ๋ฆฌํ๋ ค๋ฉด ๐ค Dataset [`map`](https://huggingface.co/docs/datasets/process#map) ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ฒด ๋ฐ์ดํฐ์
์ ์ ์ฒ๋ฆฌ ํจ์๋ฅผ ์ ์ฉํ์ธ์:
|
|
|
|
```py
|
|
>>> from transformers import AutoTokenizer
|
|
|
|
>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-cased")
|
|
|
|
|
|
>>> def tokenize_function(examples):
|
|
... return tokenizer(examples["text"], padding="max_length", truncation=True)
|
|
|
|
|
|
>>> tokenized_datasets = dataset.map(tokenize_function, batched=True)
|
|
```
|
|
|
|
ํ์ํ ๊ฒฝ์ฐ ๋ฏธ์ธ ํ๋์ ์ํด ๋ฐ์ดํฐ์
์ ์์ ๋ถ๋ถ ์งํฉ์ ๋ง๋ค์ด ๋ฏธ์ธ ํ๋ ์์
์๊ฐ์ ์ค์ผ ์ ์์ต๋๋ค:
|
|
|
|
```py
|
|
>>> small_train_dataset = tokenized_datasets["train"].shuffle(seed=42).select(range(1000))
|
|
>>> small_eval_dataset = tokenized_datasets["test"].shuffle(seed=42).select(range(1000))
|
|
```
|
|
|
|
<a id='trainer'></a>
|
|
|
|
## Train
|
|
|
|
์ฌ๊ธฐ์๋ถํฐ๋ ์ฌ์ฉํ๋ ค๋ ํ๋ ์์ํฌ์ ํด๋นํ๋ ์น์
์ ๋ฐ๋ผ์ผ ํฉ๋๋ค. ์ค๋ฅธ์ชฝ ์ฌ์ด๋๋ฐ์ ๋งํฌ๋ฅผ ์ฌ์ฉํ์ฌ ์ํ๋ ํ๋ ์์ํฌ๋ก ์ด๋ํ ์ ์์ผ๋ฉฐ, ํน์ ํ๋ ์์ํฌ์ ๋ชจ๋ ์ฝํ
์ธ ๋ฅผ ์จ๊ธฐ๋ ค๋ฉด ํด๋น ํ๋ ์์ํฌ ๋ธ๋ก์ ์ค๋ฅธ์ชฝ ์๋จ์ ์๋ ๋ฒํผ์ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค!
|
|
|
|
<Youtube id="nvBXf7s7vTI"/>
|
|
|
|
## ํ์ดํ ์น Trainer๋ก ํ๋ จํ๊ธฐ[[train-with-pytorch-trainer]]
|
|
|
|
๐ค Transformers๋ ๐ค Transformers ๋ชจ๋ธ ํ๋ จ์ ์ต์ ํ๋ [`Trainer`] ํด๋์ค๋ฅผ ์ ๊ณตํ์ฌ ํ๋ จ ๋ฃจํ๋ฅผ ์ง์ ์์ฑํ์ง ์๊ณ ๋ ์ฝ๊ฒ ํ๋ จ์ ์์ํ ์ ์์ต๋๋ค. [`Trainer`] API๋ ๋ก๊น
(logging), ๊ฒฝ์ฌ ๋์ (gradient accumulation), ํผํฉ ์ ๋ฐ๋(mixed precision) ๋ฑ ๋ค์ํ ํ๋ จ ์ต์
๊ณผ ๊ธฐ๋ฅ์ ์ง์ํฉ๋๋ค.
|
|
|
|
๋จผ์ ๋ชจ๋ธ์ ๊ฐ์ ธ์ค๊ณ ์์๋๋ ๋ ์ด๋ธ ์๋ฅผ ์ง์ ํฉ๋๋ค. Yelp ๋ฆฌ๋ทฐ [๋ฐ์ดํฐ์
์นด๋](https://huggingface.co/datasets/Yelp/yelp_review_full#data-fields)์์ 5๊ฐ์ ๋ ์ด๋ธ์ด ์์์ ์ ์ ์์ต๋๋ค:
|
|
|
|
```py
|
|
>>> from transformers import AutoModelForSequenceClassification
|
|
|
|
>>> model = AutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-cased", num_labels=5)
|
|
```
|
|
|
|
<Tip>
|
|
|
|
์ฌ์ ํ๋ จ๋ ๊ฐ์ค์น ์ค ์ผ๋ถ๊ฐ ์ฌ์ฉ๋์ง ์๊ณ ์ผ๋ถ ๊ฐ์ค์น๊ฐ ๋ฌด์์๋ก ํ์๋๋ค๋ ๊ฒฝ๊ณ ๊ฐ ํ์๋ฉ๋๋ค.
|
|
๊ฑฑ์ ๋ง์ธ์. ์ด๊ฒ์ ์ฌ๋ฐ๋ฅธ ๋์์
๋๋ค! ์ฌ์ ํ์ต๋ BERT ๋ชจ๋ธ์ ํค๋๋ ํ๊ธฐ๋๊ณ ๋ฌด์์๋ก ์ด๊ธฐํ๋ ๋ถ๋ฅ ํค๋๋ก ๋์ฒด๋ฉ๋๋ค. ์ด์ ์ฌ์ ํ์ต๋ ๋ชจ๋ธ์ ์ง์์ผ๋ก ์ํ์ค ๋ถ๋ฅ ์์
์ ์ํ ์๋ก์ด ๋ชจ๋ธ ํค๋๋ฅผ ๋ฏธ์ธ ํ๋ ํฉ๋๋ค.
|
|
|
|
</Tip>
|
|
|
|
### ํ์ดํผํ๋ผ๋ฏธํฐ ํ๋ จ[[training-hyperparameters]]
|
|
|
|
๋ค์์ผ๋ก ์ ํ ์ ์๋ ๋ชจ๋ ํ์ดํผํ๋ผ๋ฏธํฐ์ ๋ค์ํ ํ๋ จ ์ต์
์ ํ์ฑํํ๊ธฐ ์ํ ํ๋๊ทธ๋ฅผ ํฌํจํ๋ [`TrainingArguments`] ํด๋์ค๋ฅผ ์์ฑํฉ๋๋ค.
|
|
|
|
์ด ํํ ๋ฆฌ์ผ์์๋ ๊ธฐ๋ณธ ํ๋ จ [ํ์ดํผํ๋ผ๋ฏธํฐ](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments)๋ก ์์ํ์ง๋ง, ์์ ๋กญ๊ฒ ์คํํ์ฌ ์ฌ๋ฌ๋ถ๋ค์๊ฒ ๋ง๋ ์ต์ ์ ์ค์ ์ ์ฐพ์ ์ ์์ต๋๋ค.
|
|
|
|
ํ๋ จ์์ ์ฒดํฌํฌ์ธํธ(checkpoints)๋ฅผ ์ ์ฅํ ์์น๋ฅผ ์ง์ ํฉ๋๋ค:
|
|
|
|
```py
|
|
>>> from transformers import TrainingArguments
|
|
|
|
>>> training_args = TrainingArguments(output_dir="test_trainer")
|
|
```
|
|
|
|
### ํ๊ฐ ํ๊ธฐ[[evaluate]]
|
|
|
|
[`Trainer`]๋ ํ๋ จ ์ค์ ๋ชจ๋ธ ์ฑ๋ฅ์ ์๋์ผ๋ก ํ๊ฐํ์ง ์์ต๋๋ค. ํ๊ฐ ์งํ๋ฅผ ๊ณ์ฐํ๊ณ ๋ณด๊ณ ํ ํจ์๋ฅผ [`Trainer`]์ ์ ๋ฌํด์ผ ํฉ๋๋ค.
|
|
[๐ค Evaluate](https://huggingface.co/docs/evaluate/index) ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ [`evaluate.load`](https://huggingface.co/spaces/evaluate-metric/accuracy) ํจ์๋ก ๋ก๋ํ ์ ์๋ ๊ฐ๋จํ [`accuracy`]ํจ์๋ฅผ ์ ๊ณตํฉ๋๋ค (์์ธํ ๋ด์ฉ์ [๋๋ฌ๋ณด๊ธฐ](https://huggingface.co/docs/evaluate/a_quick_tour)๋ฅผ ์ฐธ์กฐํ์ธ์):
|
|
|
|
```py
|
|
>>> import numpy as np
|
|
>>> import evaluate
|
|
|
|
>>> metric = evaluate.load("accuracy")
|
|
```
|
|
|
|
`metric`์์ [`~evaluate.compute`]๋ฅผ ํธ์ถํ์ฌ ์์ธก์ ์ ํ๋๋ฅผ ๊ณ์ฐํฉ๋๋ค. ์์ธก์ `compute`์ ์ ๋ฌํ๊ธฐ ์ ์ ์์ธก์ ๋ก์ง์ผ๋ก ๋ณํํด์ผ ํฉ๋๋ค(๋ชจ๋ ๐ค Transformers ๋ชจ๋ธ์ ๋ก์ง์ผ๋ก ๋ฐํํ๋ค๋ ์ ์ ๊ธฐ์ตํ์ธ์):
|
|
|
|
```py
|
|
>>> def compute_metrics(eval_pred):
|
|
... logits, labels = eval_pred
|
|
... predictions = np.argmax(logits, axis=-1)
|
|
... return metric.compute(predictions=predictions, references=labels)
|
|
```
|
|
|
|
๋ฏธ์ธ ํ๋ ์ค์ ํ๊ฐ ์งํ๋ฅผ ๋ชจ๋ํฐ๋งํ๋ ค๋ฉด ํ๋ จ ์ธ์์ `eval_strategy` ํ๋ผ๋ฏธํฐ๋ฅผ ์ง์ ํ์ฌ ๊ฐ ์ํญ์ด ๋๋ ๋ ํ๊ฐ ์งํ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค:
|
|
|
|
```py
|
|
>>> from transformers import TrainingArguments, Trainer
|
|
|
|
>>> training_args = TrainingArguments(output_dir="test_trainer", eval_strategy="epoch")
|
|
```
|
|
|
|
### ํ๋ จ ํ๊ธฐ[[trainer]]
|
|
|
|
๋ชจ๋ธ, ํ๋ จ ์ธ์, ํ๋ จ ๋ฐ ํ
์คํธ ๋ฐ์ดํฐ์
, ํ๊ฐ ํจ์๊ฐ ํฌํจ๋ [`Trainer`] ๊ฐ์ฒด๋ฅผ ๋ง๋ญ๋๋ค:
|
|
|
|
```py
|
|
>>> trainer = Trainer(
|
|
... model=model,
|
|
... args=training_args,
|
|
... train_dataset=small_train_dataset,
|
|
... eval_dataset=small_eval_dataset,
|
|
... compute_metrics=compute_metrics,
|
|
... )
|
|
```
|
|
|
|
๊ทธ๋ฆฌ๊ณ [`~transformers.Trainer.train`]์ ํธ์ถํ์ฌ ๋ชจ๋ธ์ ๋ฏธ์ธ ํ๋ํฉ๋๋ค:
|
|
|
|
```py
|
|
>>> trainer.train()
|
|
```
|
|
|
|
<a id='pytorch_native'></a>
|
|
|
|
## ๊ธฐ๋ณธ ํ์ดํ ์น๋ก ํ๋ จํ๊ธฐ[[train-in-native-pytorch]]
|
|
|
|
<Youtube id="Dh9CL8fyG80"/>
|
|
|
|
[`Trainer`]๋ ํ๋ จ ๋ฃจํ๋ฅผ ์ฒ๋ฆฌํ๋ฉฐ ํ ์ค์ ์ฝ๋๋ก ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํ ์ ์์ต๋๋ค. ์ง์ ํ๋ จ ๋ฃจํ๋ฅผ ์์ฑํ๋ ๊ฒ์ ์ ํธํ๋ ์ฌ์ฉ์์ ๊ฒฝ์ฐ, ๊ธฐ๋ณธ PyTorch์์ ๐ค Transformers ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํ ์๋ ์์ต๋๋ค.
|
|
|
|
์ด ์์ ์์ ๋
ธํธ๋ถ์ ๋ค์ ์์ํ๊ฑฐ๋ ๋ค์ ์ฝ๋๋ฅผ ์คํํด ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ๋ณดํด์ผ ํ ์ ์์ต๋๋ค:
|
|
|
|
```py
|
|
del model
|
|
del trainer
|
|
torch.cuda.empty_cache()
|
|
```
|
|
|
|
๋ค์์ผ๋ก, 'ํ ํฐํ๋ ๋ฐ์ดํฐ์
'์ ์๋์ผ๋ก ํ์ฒ๋ฆฌํ์ฌ ํ๋ จ๋ จ์ ์ฌ์ฉํ ์ ์๋๋ก ์ค๋นํฉ๋๋ค.
|
|
|
|
1. ๋ชจ๋ธ์ด ์์ ํ
์คํธ๋ฅผ ์
๋ ฅ์ผ๋ก ํ์ฉํ์ง ์์ผ๋ฏ๋ก `text` ์ด์ ์ ๊ฑฐํฉ๋๋ค:
|
|
|
|
```py
|
|
>>> tokenized_datasets = tokenized_datasets.remove_columns(["text"])
|
|
```
|
|
|
|
2. ๋ชจ๋ธ์์ ์ธ์์ ์ด๋ฆ์ด `labels`๋ก ์ง์ ๋ ๊ฒ์ผ๋ก ์์ํ๋ฏ๋ก `label` ์ด์ ์ด๋ฆ์ `labels`๋ก ๋ณ๊ฒฝํฉ๋๋ค:
|
|
|
|
```py
|
|
>>> tokenized_datasets = tokenized_datasets.rename_column("label", "labels")
|
|
```
|
|
|
|
3. ๋ฐ์ดํฐ์
์ ํ์์ List ๋์ PyTorch ํ
์๋ฅผ ๋ฐํํ๋๋ก ์ค์ ํฉ๋๋ค:
|
|
|
|
```py
|
|
>>> tokenized_datasets.set_format("torch")
|
|
```
|
|
|
|
๊ทธ๋ฆฌ๊ณ ์์ ํ์๋ ๋๋ก ๋ฐ์ดํฐ์
์ ๋ ์์ ํ์ ์งํฉ์ ์์ฑํ์ฌ ๋ฏธ์ธ ์กฐ์ ์๋๋ฅผ ๋์
๋๋ค:
|
|
|
|
```py
|
|
>>> small_train_dataset = tokenized_datasets["train"].shuffle(seed=42).select(range(1000))
|
|
>>> small_eval_dataset = tokenized_datasets["test"].shuffle(seed=42).select(range(1000))
|
|
```
|
|
|
|
### DataLoader[[dataloader]]
|
|
|
|
ํ๋ จ ๋ฐ ํ
์คํธ ๋ฐ์ดํฐ์
์ ๋ํ 'DataLoader'๋ฅผ ์์ฑํ์ฌ ๋ฐ์ดํฐ ๋ฐฐ์น๋ฅผ ๋ฐ๋ณตํ ์ ์์ต๋๋ค:
|
|
|
|
```py
|
|
>>> from torch.utils.data import DataLoader
|
|
|
|
>>> train_dataloader = DataLoader(small_train_dataset, shuffle=True, batch_size=8)
|
|
>>> eval_dataloader = DataLoader(small_eval_dataset, batch_size=8)
|
|
```
|
|
|
|
์์ธก์ ์ํ ๋ ์ด๋ธ ๊ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ๋ก๋ํฉ๋๋ค:
|
|
|
|
```py
|
|
>>> from transformers import AutoModelForSequenceClassification
|
|
|
|
>>> model = AutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-cased", num_labels=5)
|
|
```
|
|
|
|
### ์ตํฐ๋ง์ด์ ๋ฐ ํ์ต ์๋ ์ค์ผ์ค๋ฌ[[optimizer-and-learning-rate-scheduler]]
|
|
|
|
์ตํฐ๋ง์ด์ ์ ํ์ต ์๋ ์ค์ผ์ค๋ฌ๋ฅผ ์์ฑํ์ฌ ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํฉ๋๋ค. ํ์ดํ ์น์์ ์ ๊ณตํ๋ [`AdamW`](https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html) ์ตํฐ๋ง์ด์ ๋ฅผ ์ฌ์ฉํด ๋ณด๊ฒ ์ต๋๋ค:
|
|
|
|
```py
|
|
>>> from torch.optim import AdamW
|
|
|
|
>>> optimizer = AdamW(model.parameters(), lr=5e-5)
|
|
```
|
|
|
|
[`Trainer`]์์ ๊ธฐ๋ณธ ํ์ต ์๋ ์ค์ผ์ค๋ฌ๋ฅผ ์์ฑํฉ๋๋ค:
|
|
|
|
```py
|
|
>>> from transformers import get_scheduler
|
|
|
|
>>> num_epochs = 3
|
|
>>> num_training_steps = num_epochs * len(train_dataloader)
|
|
>>> lr_scheduler = get_scheduler(
|
|
... name="linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps
|
|
... )
|
|
```
|
|
|
|
๋ง์ง๋ง์ผ๋ก, GPU์ ์ก์ธ์คํ ์ ์๋ ๊ฒฝ์ฐ 'device'๋ฅผ ์ง์ ํ์ฌ GPU๋ฅผ ์ฌ์ฉํ๋๋ก ํฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด CPU์์ ํ๋ จํ๋ฉฐ ๋ช ๋ถ์ด ์๋ ๋ช ์๊ฐ์ด ๊ฑธ๋ฆด ์ ์์ต๋๋ค.
|
|
|
|
```py
|
|
>>> import torch
|
|
|
|
>>> device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
|
>>> model.to(device)
|
|
```
|
|
|
|
<Tip>
|
|
|
|
[Colaboratory](https://colab.research.google.com/) ๋๋ [SageMaker StudioLab](https://studiolab.sagemaker.aws/)๊ณผ ๊ฐ์ ํธ์คํ
๋
ธํธ๋ถ์ด ์๋ ๊ฒฝ์ฐ ํด๋ผ์ฐ๋ GPU์ ๋ฌด๋ฃ๋ก ์ก์ธ์คํ ์ ์์ต๋๋ค.
|
|
|
|
</Tip>
|
|
|
|
์ด์ ํ๋ จํ ์ค๋น๊ฐ ๋์์ต๋๋ค! ๐ฅณ
|
|
|
|
### ํ๋ จ ๋ฃจํ[[training-loop]]
|
|
|
|
ํ๋ จ ์งํ ์ํฉ์ ์ถ์ ํ๋ ค๋ฉด [tqdm](https://tqdm.github.io/) ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ํธ๋ ์ด๋ ๋จ๊ณ ์์ ์งํ๋ฅ ํ์์ค์ ์ถ๊ฐํ์ธ์:
|
|
|
|
```py
|
|
>>> from tqdm.auto import tqdm
|
|
|
|
>>> progress_bar = tqdm(range(num_training_steps))
|
|
|
|
>>> model.train()
|
|
>>> for epoch in range(num_epochs):
|
|
... for batch in train_dataloader:
|
|
... batch = {k: v.to(device) for k, v in batch.items()}
|
|
... outputs = model(**batch)
|
|
... loss = outputs.loss
|
|
... loss.backward()
|
|
|
|
... optimizer.step()
|
|
... lr_scheduler.step()
|
|
... optimizer.zero_grad()
|
|
... progress_bar.update(1)
|
|
```
|
|
|
|
### ํ๊ฐ ํ๊ธฐ[[evaluate]]
|
|
|
|
[`Trainer`]์ ํ๊ฐ ํจ์๋ฅผ ์ถ๊ฐํ ๋ฐฉ๋ฒ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก, ํ๋ จ ๋ฃจํ๋ฅผ ์ง์ ์์ฑํ ๋๋ ๋์ผํ ์์
์ ์ํํด์ผ ํฉ๋๋ค. ํ์ง๋ง ์ด๋ฒ์๋ ๊ฐ ์ํฌํฌ๊ฐ ๋๋ ๋๋ง๋ค ํ๊ฐ์งํ๋ฅผ ๊ณ์ฐํ์ฌ ๋ณด๊ณ ํ๋ ๋์ , [`~evaluate.add_batch`]๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ ๋ฐฐ์น๋ฅผ ๋์ ํ๊ณ ๋งจ ๋ง์ง๋ง์ ํ๊ฐ์งํ๋ฅผ ๊ณ์ฐํฉ๋๋ค.
|
|
|
|
```py
|
|
>>> import evaluate
|
|
|
|
>>> metric = evaluate.load("accuracy")
|
|
>>> model.eval()
|
|
>>> for batch in eval_dataloader:
|
|
... batch = {k: v.to(device) for k, v in batch.items()}
|
|
... with torch.no_grad():
|
|
... outputs = model(**batch)
|
|
|
|
... logits = outputs.logits
|
|
... predictions = torch.argmax(logits, dim=-1)
|
|
... metric.add_batch(predictions=predictions, references=batch["labels"])
|
|
|
|
>>> metric.compute()
|
|
```
|
|
|
|
<a id='additional-resources'></a>
|
|
|
|
## ์ถ๊ฐ ์๋ฃ[[additional-resources]]
|
|
|
|
๋ ๋ง์ ๋ฏธ์ธ ํ๋ ์์ ๋ ๋ค์์ ์ฐธ์กฐํ์ธ์:
|
|
|
|
- [๐ค Trnasformers ์์ ](https://github.com/huggingface/transformers/tree/main/examples)์๋ PyTorch ๋ฐ ํ
์ํ๋ก์ฐ์์ ์ผ๋ฐ์ ์ธ NLP ์์
์ ํ๋ จํ ์ ์๋ ์คํฌ๋ฆฝํธ๊ฐ ํฌํจ๋์ด ์์ต๋๋ค.
|
|
|
|
- [๐ค Transformers ๋
ธํธ๋ถ](notebooks)์๋ PyTorch ๋ฐ ํ
์ํ๋ก์ฐ์์ ํน์ ์์
์ ์ํด ๋ชจ๋ธ์ ๋ฏธ์ธ ํ๋ํ๋ ๋ฐฉ๋ฒ์ ๋ํ ๋ค์ํ ๋
ธํธ๋ถ์ด ํฌํจ๋์ด ์์ต๋๋ค.
|