콘텐츠로 이동

트랜스포머 파이프라인 기초

필요한 모듈 설치하기

!pip install transformers --quiet
!pip install torch --quiet

가장 기본적인 트랜스포머 활용 예시 (감정 분류)

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
classifier("I've been waiting for a HuggingFace course my whole life.")

모델을 직접 명시하는 경우

classifier = pipeline("sentiment-analysis", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")

다른 예제

classifier("I am hungry")

여러 문장을 전달하는 예제

classifier(
    ["I've been waiting for a HuggingFace course my whole life.", "I hate this so much!"]
)

실행결과)

[{'label': 'POSITIVE', 'score': 0.9598047137260437},
 {'label': 'NEGATIVE', 'score': 0.9994558095932007}]

사용 가능한 파이프라인 종류

  • feature-extraction (get the vector representation of a text)
  • fill-mask
  • ner (named entity recognition)
  • question-answering
  • sentiment-analysis
  • summarization
  • text-generation
  • translation
  • zero-shot-classification