자기 지도 학습에서는 모델이 훈련 데이터의 내재적 구조를 기반으로 학습됩니다. 언어 모델의 경우, 주어진 순서에서 다음 단어나 토큰을 예측하는 것이 일반적입니다.(next token prediction)
초기 모델 개발 이후, 자기 지도 학습은 세세한 조정에 적용될 수 있으며, 예제 텍스트를 기반으로 특정 쓰기 스타일을 에뮬레이트하는 모델을 만드는 등의 용도로 활용될 수 있습니다.
Supervised Learning
지도 학습은 모델 세세한 조정을 위한 인기 있는 방법 중 하나로 두드러집니다. 이는 특정 작업에 대한 입력-출력 쌍을 기반으로 모델을 훈련시키는 것을 포함합니다.
예를 들어, 지시 조정은 모델이 질문에 답하거나 사용자 프롬프트에 응답하는 능력을 향상시키는 것을 목표로 합니다.
Reinforcement Learning
강화 학습(RL)을 사용하여 모델을 세세하게 조정하는 것입니다. RL은 보상 모델을 활용하여 기본 모델의 훈련을 안내하며, 언어 모델의 완성을 인간 라벨러의 선호도에 맞추려고 합니다. 보상 모델을 Proximal Policy Optimization (PPO)과 같은 강화 학습 알고리즘과 결합하여, 사전 훈련된 모델이 효과적으로 세세하게 조정됩니다
Huggingface Fine-tuning
LLM Fine-tuning에서 적용되는 기본 요소는 여러가지가 있습니다. Huggingface에서는 이러한 기본 요소를 Transformer 기본 라이브러리에서 Class로 제공하고 이외에 peft, trl, bitsandbytes, accelerate를 함께 사용하여 fine-tuning을 지원합니다.
Huggingface Fine-tuning 개발
1. 일반적인 모델 Fine-tuning: Transformer Trainer 클래스 사용
2. LLM에서 PEFT(Parameter Efficient Fine-tuning)은 아래 라이브러리를 함께 사용합니다.
transformer :기본 라이브러리
peft: PEFT를 위한 LoRa, QLoRa 사용
trl: SFTTraine 클래스 사용
bitsandbytes: 양자화를 위한 라이브러리
accelerate: GPU 분산 학습을 위한 라이브러리
3. Reinforcement Learning은 TRL 라이브러리를 사용합니다.
Supervised fine-tuning: SFTTrainer
Direct Preference Optimization: DPOTrainer
Reward: RewardTrainer
Proximal Policy Optimization: PPOTrainer
Contrastive Preference Optimization: CPOTrainer
Optimization without Reference Model: ORPOTrainer
Huggingface: Basic Fine-tuning
가장 기본적인 Transformer 모델을 Dataset을 통해서 Fine-tuning 하겠습니다. 절차는 아래와 같습니다:
{'sentence1': 'Amrozi accused his brother , whom he called " the witness " , of deliberately distorting his evidence .',
'sentence2': 'Referring to him as only " the witness " , Amrozi accused his brother of deliberately distorting his evidence .',
'label': 1,
'idx': 0}
이미 label이 숫자표기로 되어있습니다. 어떤 label 값을 가지고 있는지 확인해 보자
input_ids ,attention_mask, token_type_ids 가 추가된 것을 확인할 수 있다.
3. Dynamic padding
Dataset을 DataLoader에 담아 데이터를 꺼내어 사용하게 되는데 우리는 불필요한 패딩을 줄이기위해 각 batch별로 가장 큰 길이를 지정하여 padding을 생성하게 된다.
여기서는 DataCollatorWithPadding 함수를 사용해 padding을 만들어 본다.
from transformers import DataCollatorWithPadding
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
samples = tokenized_datasets["train"][:8]
samples = {
k: v for k, v in samples.items() if k not in ["idx", "sentence1", "sentence2"]
}
[len(x) for x in samples["input_ids"]]
[50, 59, 47, 67, 59, 50, 62, 32]
샘플 데이터를 추출하여 data_collator 에 넣어보면
batch = data_collator(samples)
{k: v.shape for k, v in batch.items()}
from transformers import TrainingArguments
training_args = TrainingArguments("test-trainer")
from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)
Some weights of BertForSequenceClassification were not initialized from the model checkpoint at bert-base-uncased and are newly initialized: ['classifier.bias', 'classifier.weight']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.