본문 바로가기
NLP

Instruct learning, fine tuning, and T5

by 아르카눔 2025. 1. 28.
def preprocess_data(example):
    # Instruction, Input, Output 가져오기
    instruction = example["instruction"]
    input_text = example["input"]
    output_text = example["output"]
    
    # Prompt 생성
    prompt = f"Instruction: {instruction}\nInput: {input_text}\nOutput:"
    target = output_text

    # Prompt와 Target 텍스트를 각각 토큰화
    tokenized_input = tokenizer(prompt, truncation=True, max_length=512, padding="max_length")
    tokenized_target = tokenizer(target, truncation=True, max_length=512, padding="max_length")
    
    # Labels 추가 (Output 토큰으로 학습)
    tokenized_input["labels"] = tokenized_target["input_ids"]

    return tokenized_input

 

Instruct Fine tuning을 위한 전처리 코드는 위와 같다.

Huggingface의 encode_plus 결과라면 토큰들의 id인 input_ids와 attention_masks가 반환 된다.

그리고 예시 데이터는 아래와 같다고 하자.

 

# Example Datasets
[
  {
    "instruction": "Translate to French",
    "input": "I love pizza",
    "output": "J'adore la pizza"
  },
  {
    "instruction": "Summarize the text",
    "input": "Machine learning enables systems to learn from data.",
    "output": "ML allows systems to learn from data."
  }
]

 

 

0번째 데이터 하나만을 input으로 넣는다고 가정하면,

instruction에 Translate to French가, input에 영어 문장이 들어가고, target에 output 문장이 들어간다.

 

이는 T5의 전처리와 매우 유사하다.

아래의 RTE 예시를 살펴보면 알 수 있다. 

 

 

 

T5와 유사하지만 특정 데이터에 국한된게 아니라 보다 보편적인 목적과 보다 증가한 결과물을 생성하도록 설정하면 된다고 이해하면 될 듯 하다.