Official Docs: https://huggingface.co/docs/transformers/v4.19.2/en/main_classes/trainer
Trainer
class๋ ๋ชจ๋ธํ์ต๋ถํฐ ํ๊ฐ๊น์ง ํ ๋ฒ์ ํด๊ฒฐํ ์ ์๋ API๋ฅผ ์ ๊ณตํ๋ค. ๋ค์์ ์ฌ์ฉ์์๋ฅผ ๋ณด๋ฉด ์ง๊ด์ ์ผ๋ก ์ดํดํ ์ ์๋ค.
from transformers import Trainer
#initialize Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset
eval_dataset=eval_dataset
compute_metrics,
tokenizer=tokenizer
)
#train
trainer.train()
#save
trainer.save_model()
#eval
metrics = trainer.evaluate(eval_dataset=eval_dataset)
Initialize Trainer
from transformers import Trainer
#initialize Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset
eval_dataset=eval_dataset
compute_metrics=compute_metrics,
)
- ๊ธฐ๋ณธ์ ์ผ๋ก ์์ ๊ฐ์ด Trainer์ ์ ์ธํ ์ ์๋ค. (์ด์ธ์๋ ๋ ๋ง์ argument๋ค์ด ์กด์ฌํ๋ค)
model
์ HuggingFace ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ์ ๊ณต๋๋ PretrainedModel์ ์ฌ์ฉํด๋ ๋์ง๋ง, torch.nn.Module์ ์ฌ์ฉํ ์๋ ์๋ค. ๋ชจ๋ธ์ ์ง์ ํ๋ ๋ฐฉ๋ฒ์ ์์ ๊ฐ์ดmodel
argument๋ก ์ค ์๋ ์๊ณ , ํน์, ์๋์ ๊ฐ์ด callableํ ๋ชจ๋ธ ์ด๊ธฐํ ํจ์๋ฅผmodel_init
argument๋ก ์ค ์๋ ์๋ค. ๋ง์ฝmodel_init
์ ์ด์ฉํด ๋ชจ๋ธ์ ์ง์ ํด์ฃผ๋ฉด, ๋งค train() method๊ฐ ํธ์ถ๋ ๋๋ง๋ค ๋ชจ๋ธ์ด ์๋กญ๊ฒ ์ด๊ธฐํ(์์ฑ)๋๋ค.
from transformers import Trainer, AutoModelForSequenceClassification
#initialize Trainer
trainer = Trainer(
model_init=AutoModelForSequenceClassification.from_pretrained(model_name),
args=training_args,
train_dataset=train_dataset
eval_dataset=eval_dataset
compute_metrics=compute_metrics,
)
args
๋ train์ ํ์ํ ํ๋ผ๋ฏธํฐ๋ค์ ๋ชจ์์ผ๋ก, TrainingArgs๋ฅผ ์ด์ฉํด ์ค ์ ์๋ค. Optimizer์ ์ข ๋ฅ, learning rate, epoch ์, scheduler, half precision ์ฌ์ฉ์ฌ๋ถ ๋ฑ์ ์ง์ ํ ์ ์์ผ๋ฉฐ, ๋ชจ๋ ํ๋ผ๋ฏธํฐ์ ๋ชฉ๋ก์ ์ฌ๊ธฐ์์ ํ์ธํ ์ ์๋ค. ์์๋ ๋ค์๊ณผ ๊ฐ๋ค.
from transformers import TrainingArguments
training_args = TrainingArguments(
output_dir='./results', # output directory
num_train_epochs=1, # total number of training epochs
per_device_train_batch_size=1, # batch size per device during training
per_device_eval_batch_size=10, # batch size for evaluation
warmup_steps=1000, # number of warmup steps for learning rate scheduler
weight_decay=0.01, # strength of weight decay
logging_dir='./logs', # directory for storing logs
logging_steps=200, # How often to print logs
do_train=True, # Perform training
do_eval=True, # Perform evaluation
evaluation_strategy="epoch", # evalute after eachh epoch
gradient_accumulation_steps=64, # total number of steps before back propagation
fp16=True, # Use mixed precision
fp16_opt_level="02", # mixed precision mode
run_name="ProBert-BFD-MS", # experiment name
seed=3 # Seed for experiment reproducibility 3x3
)
train_dataset
๊ณผeval_dataset
์ ๊ฐ๊ฐ train๊ณผ validation/test์ ์ฌ์ฉ๋๋ torch.utils.data.Dataset์ด๋ค. ๊ผญ ์ด๊ธฐํ ํ ๋ ์ง์ ํ์ง ์์๋ ๋๋ค.
compute_metrics
๋ evaluation์ ์ฌ์ฉํ metric์ ๊ณ์ฐํ๋ ํจ์์ด๋ค. ๋ชจ๋ธ์ output์ธ EvalPrediction์ input์ผ๋ก ๋ฐ์ metric์ dictionary ํํ๋ก returnํ๋ ํจ์๊ฐ ๋์ผ ํ๋ค. ์์๋ ๋ค์๊ณผ ๊ฐ๋ค.
from sklearn.metrics import accuracy_score, precision_recall_fscore_support, roc_auc_score
def compute_metrics(pred):
labels = pred.label_ids
preds = pred.predictions.argmax(-1)
precision, recall, f1, _ = precision_recall_fscore_support(labels, preds, average='binary')
acc = accuracy_score(labels, preds)
auc = roc_auc_score(labels, preds)
return {
'accuracy': acc,
'f1': f1,
'precision': precision,
'recall': recall,
'auroc': auc
}
๋ฐ์ํ
'๐ Python & library > HuggingFace' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
HuggingFace Space ๋ง๋ค๊ธฐ (1) | 2023.06.10 |
---|---|
[HuggingFace] Tokenizer์ ์ญํ ๊ณผ ๊ธฐ๋ฅ, Token ID, Input ID, Token type ID, Attention Mask (0) | 2022.06.03 |
[HuggingFace] Tokenizer class ์์๋ณด๊ธฐ (0) | 2022.06.03 |
[HuggingFace] Pipeline & AutoClass (0) | 2022.05.31 |