텍스트 처리에 트랜스포머 사용하기

PyTorch로 배우는 텍스트 딥러닝

Shubham Jain

Instructor

왜 텍스트 처리에 트랜스포머를 쓰나요?

트랜스포머 로고

  • 속도
  • 단어 간 관계 파악(거리와 무관)
  • 사람과 유사한 응답
PyTorch로 배우는 텍스트 딥러닝

트랜스포머 구성 요소

  • 인코더: 입력 처리

 

  • 디코더: 출력 생성

 

  • 피드포워드 신경망: 의미 정제

 

  • 위치 인코딩: 순서 반영

 

  • 멀티헤드 어텐션: 다중 관점/감정 포착
PyTorch로 배우는 텍스트 딥러닝

데이터 준비: 학습/테스트 분할

sentences = ["I love this product", "This is terrible", 
             "Could be better", "This is the best"]
labels = [1, 0, 0, 1]

train_sentences = sentences[:3] train_labels = labels[:3] test_sentences = sentences[3:] test_labels = labels[3:]
PyTorch로 배우는 텍스트 딥러닝

트랜스포머 모델 구축

class TransformerEncoder(nn.Module):

def __init__(self, embed_size, heads, num_layers, dropout): super(TransformerEncoder, self).__init__()
self.encoder = nn.TransformerEncoder( nn.TransformerEncoderLayer(d_model=embed_size, nhead=heads), num_layers=num_layers)
self.fc = nn.Linear(embed_size, 2)
def forward(self, x):
x = self.encoder(x)
x = x.mean(dim=1)
return self.fc(x)
model = TransformerEncoder(embed_size=512, heads=8, num_layers=3, dropout=0.5)
optimizer = optim.Adam(model.parameters(), lr=0.001) criterion = nn.CrossEntropyLoss()
PyTorch로 배우는 텍스트 딥러닝

트랜스포머 학습시키기

for epoch in range(5):

for sentence, label in zip(train_sentences, train_labels): tokens = sentence.split()
data = torch.stack([token_embeddings[token] for token in tokens], dim=1)
output = model(data)
loss = criterion(output, torch.tensor([label]))
optimizer.zero_grad() loss.backward() optimizer.step() print(f"Epoch {epoch}, Loss: {loss.item()}")
Epoch 0, Loss: 13.788233757019043
Epoch 1, Loss: 3.9480819702148438
Epoch 2, Loss: 2.4790847301483154
Epoch 3, Loss: 1.3020926713943481
Epoch 4, Loss: 0.4660853147506714
PyTorch로 배우는 텍스트 딥러닝

트랜스포머로 예측하기

def predict(sentence):
    model.eval()

with torch.no_grad():
tokens = sentence.split() data = torch.stack([token_embeddings.get(token, torch.rand((1, 512))) for token in tokens], dim=1)
output = model(data)
predicted = torch.argmax(output, dim=1)
return "Positive" if predicted.item() == 1 else "Negative"
PyTorch로 배우는 텍스트 딥러닝

새 텍스트로 예측하기

sample_sentence = "This product can be better"
print(f"'{sample_sentence}' is {predict(sample_sentence)}")
'This product can be better' is Negative
PyTorch로 배우는 텍스트 딥러닝

연습해 봅시다!

PyTorch로 배우는 텍스트 딥러닝

Preparing Video For Download...