AutoClass 활용하기
이제 토크나이저가 어떻게 동작하는지, 그리고 모델을 위한 텍스트 준비에서 어떤 역할을 하는지 살펴보셨습니다. 이번에는 한 단계 더 나아가 pipeline() 함수와 AutoModel, AutoTokenizer를 함께 사용해 보겠습니다. 제어성과 편의성의 균형을 잘 맞추는 방법입니다.
감정 분석 과제를 이어서, AutoClass와 pipeline 모듈을 함께 사용해 보세요.
transformers 라이브러리의 AutoModelForSequenceClassification, AutoTokenizer, pipeline은 이미 임포트되어 있습니다.
이 연습은 강의의 일부입니다
Hugging Face 활용하기
연습 안내
- 모델과 토크나이저를 다운로드하여 각각
my_model,my_tokenizer로 저장하세요. - 파이프라인을 생성하여
my_pipeline으로 저장하세요. my_pipeline을 사용해 예측을 수행하고 결과를output으로 저장하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Download the model and tokenizer
my_model = ____.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
my_tokenizer = ____.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
# Create the pipeline
my_pipeline = pipeline(task="sentiment-analysis", ____=____, ____=____)
# Predict the sentiment
output = ____("This course is pretty good, I guess.")
print(f"Sentiment using AutoClasses: {output[0]['label']}")