T5 사용법 — 조건부 생성과 파이프라인

T5 사용법

T5를 쓰는 표준 방법은 T5ForConditionalGenerationT5Tokenizer로 요약·번역 같은 조건부 생성을 하는 거예요. text-to-text 원리에 맞춰, 입력 앞에 작업을 나타내는 prefix를 붙이는 것이 관례예요.

기본 생성 예시

from transformers import T5ForConditionalGeneration, T5Tokenizer

model = T5ForConditionalGeneration.from_pretrained("t5-small")
tokenizer = T5Tokenizer.from_pretrained("t5-small")

# T5은 작업 prefix를 입력에 붙여 사용해요
input_text = "summarize: The quick brown fox jumps over the lazy dog."
input_ids = tokenizer(input_text, return_tensors="pt").input_ids

outputs = model.generate(input_ids)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
  • summarize: — 요약 작업 프롬프트 (번역은 translate English to German:, 질의응답은 question: ... context: ...)
  • generate()로 조건부 생성
  • skip_special_tokens=True로 특수 토큰 제외

파인튜닝

첫 단계(라벨 없는 데이터 사전훈련)와 파인튜닝(작업 데이터)을 통해, 특정 도메인·언어에 맞춰 모델을 조정할 수 있어요. Seq2SeqTrainer 또는 일반 transformers Trainer를 쓰면 편해요.

파이프라인 활용

요약 파이프라인으로도 빠르게 쓸 수 있어요.

from transformers import pipeline

summarizer = pipeline("summarization", model="t5-small")
out = summarizer("Your long text here...")
print(out[0]["summary_text"])

더 알아보기