파이프라인 내부 동작 과정
from transformers import pipeline
classifier = pipeline("sentiment-analysis")
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}]
다음이 동작 하는 방법은 아래와 같다 .
1.Tokenizer 를 이용한 전처리
원시텍스트를 바로 처리 할수 없기 떄문에 pipeline 에도 첫번쨰 단계는 텍스트 입력을 모델이 이해할 수 있는 숫자로 변호나 하는것이 중요
TIP
tensor란 ?!
수학적인 개념으로 데이터의 배열 아래 배열의 정의를 보면 이해가 될수있다.
Transformers는 기본적으로 tensor 로 입력을 받는고 return 타입을 정할수가 있다 .
raw_inputs = [
"I've been waiting for a HuggingFace course my whole life.",
"I hate this so much!",
]
inputs = tokenizer(raw_inputs, padding=True, truncation=True, return_tensors="pt")
print(inputs)
return 을 PyTorch tensor 로 받은결과
{
'input_ids': tensor([
[ 101, 1045, 1005, 2310, 2042, 3403, 2005, 1037, 17662, 12172, 2607, 2026, 2878, 2166, 1012, 102],
[ 101, 1045, 5223, 2023, 2061, 2172, 999, 102, 0, 0, 0, 0, 0, 0, 0, 0]
]),
'attention_mask': tensor([
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
])
}
출력은 input_ids와 attention_mask 두 개의 키를 갖는 딕셔너리입니다
put_ids는 각 문장 내 토큰의 고유 식별자인 정수로 이루어진 2개의 행
Tokenizers ?! (0) | 2024.01.19 |
---|---|
Huggingface 모델 사용법!? (1) | 2024.01.19 |
Huggingface Model 구조 (2) | 2024.01.12 |
Hugging Face 튜토리얼 (트랜스포머 모델) (0) | 2024.01.08 |
Langchain 이란 무엇일까? (0) | 2023.12.29 |