Attention is All You Need(2017)에서 시퀀스 모델링을 위한 새로운 신경망 아키텍처를 제안함
순환신경망(RNN) 대비 기계 번역 학습의 품질과 훈련 비용 좋음
효율적인 전이학습 방법(ULMFiT)으로 매우 크고 다양한 말뭉치(corpus)에서 장단기메모리(LSTM) 신경망을 훈련해 적은 양의 레이블링된 데이터로도 높은 성능
가장 유명한 트랜스포머 모델
1) GPT(Generative Pretrained Transformer)
2) BERT(Bidirectional Encoder Representations from Transformers)
다양한 트랜스포머 모델 time line
[참조] LLM time line
1) RNN : 입력 받은 데이터를 네트워크에 통과시키고 hidden state라고 불리는 벡터를 출력함과 동시에 모델은 피드백 루프를 통해 일부 정보를 다시 자신에게 전달여 다음 단계에서 해당 정보를 사용하는 순환 신경망
2) Sequence-to-Sequence : 입력과 출력의 데이터 길이가 다른 데이터 분석에 용이하며, 인코더는 입력 시퀀스의 정보를 숫자로 표현하고, 마지막에 hidden state에 정보를 답고 디코더에 전달하여 출력 시퀀스를 생성함.
1) attention
즉, attention 메커니즘은 디코더가 출력 시퀀스의 각 위치마다 어떤 입력 상태를 주로 주의해야 할지 결정
2) self-attention
1) 기본 개념
기본에 학습된 모델을 다른 새로운 작업에 적용하거나 세밀하게 조정함
2) ULMFiT - NLP 전이학습 예시
사전 학습된 LSTM 모델을 다양한 작업에 적용하기 위한 프레임워크 개발 연구 프로젝트 결과
1) GPT (Generative Pre-trained Transformer)
2) BERT (Bidirectional Encoder Representations from Transformers)
1) 라이브러리 : 토크나이저, 트랜스포머스, 데이터셋, 액셀러레이트 제공
1-1) 허깅페이스 토크나이저 : 로스트 백엔드로 빠른 텍스트 토큰화 가능, 모델 출력을 적절한 포맷으로 변환하는 전처리, 사후처리 단계 처리 등
1-2) 데이터셋 : 수천개의 데이터셋 제공, 메모리 매핑이라는 특별한 메커니즘을 활용해 램 부족 회피
1-3) 엑셀러레이트 : 훈련 루프를 미세하게 제어할 때 필요, 훈련에 필요한 인프라 전환을 단순화해 워크플로우를 가속화 함
2) 허깅페이스허브 : 사정 훈련된 모델 가중치, 데이터셋, 평가 지표를 위한 스크립트, 문서(모델 카드, 데이터셋 카드) 등
[주의] 예제 코드에는 없지만, 현재 구제적인 모델명 등을 넣어서 분석하는 것이 권장됨
import pandas as pd
text = """Dear Amazon, last week I ordered an Optimus Prime action figure \
from your online store in Germany. Unfortunately, when I opened the package, \
I discovered to my horror that I had been sent an action figure of Megatron \
instead! As a lifelong enemy of the Decepticons, I hope you can understand my \
dilemma. To resolve the issue, I demand an exchange of Megatron for the \
Optimus Prime figure I ordered. Enclosed are copies of my records concerning \
this purchase. I expect to hear from you soon. Sincerely, Bumblebee."""
from transformers import pipeline
classifier = pipeline("text-classification")
outputs = classifier(text)
pd.DataFrame(outputs)
ner_tagger = pipeline("ner", aggregation_strategy="simple")
outputs = ner_tagger(text)
pd.DataFrame(outputs)
reader = pipeline("question-answering")
question = "What does the customer want?"
outputs = reader(question=question, context=text)
pd.DataFrame([outputs])
summarizer = pipeline("summarization")
outputs = summarizer(text, max_length=45, clean_up_tokenization_spaces=True)
print(outputs[0]['summary_text'])
translator = pipeline("translation_en_to_de",
model="Helsinki-NLP/opus-mt-en-de")
outputs = translator(text, clean_up_tokenization_spaces=True, min_length=100)
print(outputs[0]['translation_text'])
#hide
from transformers import set_seed
set_seed(42) # Set the seed to get reproducible results
generator = pipeline("text-generation")
response = "Dear Bumblebee, I am sorry to hear that your order was mixed up."
prompt = text + "\n\nCustomer service response:\n" + response
outputs = generator(prompt, max_length=200)
print(outputs[0]['generated_text'])