Convolutional Neural Networks สำหรับการจำแนกข้อความ

Deep Learning สำหรับข้อความด้วย PyTorch

Shubham Jain

Instructor

CNN สำหรับการจำแนกข้อความ

  • จำแนกทวีตเป็น
    • เชิงบวก
    • เชิงลบ
    • เป็นกลาง
Deep Learning สำหรับข้อความด้วย PyTorch

การดำเนินการ Convolution

การดำเนินการ Convolution

  • การดำเนินการ Convolution
    • เลื่อน Filter (Kernel) ไปบนข้อมูล Input
    • คำนวณแบบ Element-wise ในแต่ละตำแหน่งของ Filter

 

  • สำหรับข้อความ: เรียนรู้โครงสร้างและความหมายของคำ
1 Animation from Vincent Dumoulin, Francesco Visin
Deep Learning สำหรับข้อความด้วย PyTorch

Filter และ Stride ใน CNN

  • Filter:
    • Matrix ขนาดเล็กที่เลื่อนไปบน Input

 

  • Stride:
    • จำนวนตำแหน่งที่ Filter เลื่อนในแต่ละครั้ง

Filter และ Stride

1 Animation from Vincent Dumoulin, Francesco Visin
Deep Learning สำหรับข้อความด้วย PyTorch

สถาปัตยกรรม CNN สำหรับข้อความ

  • Convolutional Layer: ใช้ Filter กับข้อมูล Input
  • Pooling Layer: ลดขนาดข้อมูลโดยคงข้อมูลสำคัญไว้
  • Fully Connected Layer: ทำนายผลลัพธ์สุดท้ายจาก Output ของเลเยอร์ก่อนหน้า
Deep Learning สำหรับข้อความด้วย PyTorch

การสร้างโมเดลจำแนกข้อความด้วย CNN

class SentimentAnalysisCNN(nn.Module):

def __init__(self, vocab_size, embed_dim):
super().__init__()
self.embedding = nn.Embedding(vocab_size, embed_dim)
self.conv = nn.Conv1d(embed_dim, embed_dim, kernel_size=3, stride=1, padding=1)
self.fc = nn.Linear(embed_dim, 2) ...
  • เมธอด __init__ กำหนดสถาปัตยกรรมของโมเดล
  • super() เริ่มต้น Base Class nn.Module
  • nn.Embedding สร้าง Dense Word Vector
  • nn.Conv1d สำหรับข้อมูลแบบ 1 มิติ
Deep Learning สำหรับข้อความด้วย PyTorch

การสร้างโมเดลจำแนกข้อความด้วย CNN

    ...
    def forward(self, text):
        embedded = self.embedding(text).permute(0, 2, 1)

conved = F.relu(self.conv(embedded))
conved = conved.mean(dim=2)
return self.fc(conved)
  • Embedding Layer แปลงข้อความเป็น Embedding
  • ปรับ Tensor ให้ตรงกับ Input ที่ Convolution Layer คาดหวัง
  • ดึง Feature สำคัญด้วย ReLU
  • ลบเลเยอร์และมิติที่ไม่จำเป็นออก
Deep Learning สำหรับข้อความด้วย PyTorch

เตรียมข้อมูลสำหรับโมเดลวิเคราะห์ความรู้สึก

vocab = ["i", "love", "this", "book", "do", "not", "like"]
word_to_idx = {word: i for i, word in enumerate(vocab)}

vocab_size = len(word_to_ix)
embed_dim = 10
book_samples = [ ("The story was captivating and kept me hooked until the end.".split(),1), ("I found the characters shallow and the plot predictable.".split(),0) ]
model = SentimentAnalysisCNN(vocab_size, embed_dim) criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=0.1)
Deep Learning สำหรับข้อความด้วย PyTorch

เทรนโมเดล

for epoch in range(10):  
    for sentence, label in data:

model.zero_grad()
sentence = torch.LongTensor([word_to_idx.get(w, 0) for w in sentence]).unsqueeze(0)
outputs = model(sentence) label = torch.LongTensor([int(label)])
loss = criterion(outputs, label) loss.backward()
optimizer.step()
Deep Learning สำหรับข้อความด้วย PyTorch

รันโมเดลวิเคราะห์ความรู้สึก

for sample in book_samples:

input_tensor = torch.tensor([word_to_idx[w] for w in sample], dtype=torch.long).unsqueeze(0)
outputs = model(input_tensor)
_, predicted_label = torch.max(outputs.data, 1)
sentiment = "Positive" if predicted_label.item() == 1 else "Negative"
print(f"Book Review: {' '.join(sample)}") print(f"Sentiment: {sentiment}\n")
Book Review: The story was captivating and kept me hooked until the end
Sentiment: Positive
Book Review: I found the characters shallow and the plot predictable
Sentiment: Negative
Deep Learning สำหรับข้อความด้วย PyTorch

มาฝึกกันเถอะ!

Deep Learning สำหรับข้อความด้วย PyTorch

Preparing Video For Download...