Học chuyển giao cho phân loại văn bản

Deep Learning cho Văn bản với PyTorch

Shubham Jain

Instructor

Học chuyển giao là gì?

 

Học chuyển giao

  • Tái sử dụng tri thức từ một nhiệm vụ cho nhiệm vụ liên quan

 

  • Tiết kiệm thời gian
  • Chia sẻ chuyên môn
  • Giảm nhu cầu dữ liệu lớn

 

  • Ví dụ: giáo viên tiếng Anh dạy Lịch sử
Deep Learning cho Văn bản với PyTorch

Cơ chế học chuyển giao

Học chuyển giao I

Deep Learning cho Văn bản với PyTorch

Cơ chế học chuyển giao

Học chuyển giao II

Deep Learning cho Văn bản với PyTorch

Cơ chế học chuyển giao

Học chuyển giao II

Deep Learning cho Văn bản với PyTorch

Cơ chế học chuyển giao

Học chuyển giao III

Deep Learning cho Văn bản với PyTorch

Mô hình tiền huấn luyện: BERT

  • Bidirectional Encoder Representations from Transformers

Bert Sentiment Analysis

  • Huấn luyện cho mô hình hóa ngôn ngữ
  • Nhiều lớp transformer
  • Tiền huấn luyện trên tập văn bản lớn
Deep Learning cho Văn bản với PyTorch

Thực hành: triển khai BERT

texts = ["I love this!", 
         "This is terrible.", 
         "Amazing experience!", 
         "Not my cup of tea."]
labels = [1, 0, 1, 0]

import torch from transformers import BertTokenizer, BertForSequenceClassification
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2)
inputs = tokenizer(texts, padding=True, truncation=True, return_tensors="pt", max_length=32) inputs["labels"] = torch.tensor(labels)
Deep Learning cho Văn bản với PyTorch

Tinh chỉnh BERT

optimizer = torch.optim.AdamW(model.parameters(), lr=0.00001)
model.train()

for epoch in range(1): outputs = model(**inputs)
loss = outputs.loss loss.backward()
optimizer.step() optimizer.zero_grad()
print(f"Epoch: {epoch+1}, Loss: {loss.item()}")
Epoch: 1, Loss: 0.7061821222305298
Deep Learning cho Văn bản với PyTorch

Đánh giá trên văn bản mới

text = "I had an awesome day!"
input_eval = tokenizer(text, return_tensors="pt", truncation=True, 
                       padding=True, max_length=128)

outputs_eval = model(**input_eval)
predictions = torch.nn.functional.softmax(outputs_eval.logits, dim=-1)
predicted_label = 'positive' if torch.argmax(predictions) > 0 else 'negative' print(f"Text: {text}\nSentiment: {predicted_label}")
Text: I had an awesome day!
Sentiment: positive
Deep Learning cho Văn bản với PyTorch

Ayo berlatih!

Deep Learning cho Văn bản với PyTorch

Preparing Video For Download...