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

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

Shubham Jain

Data Scientist

RNN สำหรับข้อความ

  • รองรับลำดับข้อมูลที่มีความยาวแตกต่างกัน
  • มีหน่วยความจำระยะสั้นภายใน
  • CNN ตรวจจับรูปแบบในกลุ่มข้อมูล
  • RNN จดจำคำก่อนหน้าเพื่อเข้าใจความหมายได้ดีขึ้น
Deep Learning สำหรับข้อความด้วย PyTorch

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

ภาพการประชดประชัน

เหตุใด?

  • RNN อ่านประโยคทีละคำเหมือนมนุษย์
  • เข้าใจบริบทและลำดับของคำ

ตัวอย่าง: ตรวจจับการประชดในทวีต

"I just love getting stuck in traffic."

  • ประชดประชัน
Deep Learning สำหรับข้อความด้วย PyTorch

ทบทวน: การสร้าง Dataset และ DataLoader

# Import libraries
from torch.utils.data import Dataset, DataLoader

# Create a class class TextDataset(Dataset):
def __init__(self, text): self.text = text
def __len__(self): return len(self.text)
def __getitem__(self, idx): return self.text[idx]
Deep Learning สำหรับข้อความด้วย PyTorch

การสร้าง RNN

sample_tweet = "This movie had a great plot and amazing acting."
# Preprocess the review and convert it to a tensor (not shown for brevity)
# ...
sentiment_prediction = model(sample_tweet_tensor)
  • ฝึก RNN เพื่อจำแนกทวีตว่าเชิงบวกหรือเชิงลบ
  • ผลลัพธ์: "Positive"
Deep Learning สำหรับข้อความด้วย PyTorch

RNN แบบอื่น: LSTM

ภาพการวิเคราะห์ความรู้สึก

ทวีต:

  "Loved the cinematography, 
  hated the dialogue. 
  The acting was exceptional,
  but the plot fell flat."
  • Long Short Term Memory (LSTM) จัดการความซับซ้อนที่ RNN อาจรับมือได้ยาก
Deep Learning สำหรับข้อความด้วย PyTorch

LSTM

สถาปัตยกรรม LSTM: Input gate, forget gate และ output gate

class LSTMModel(nn.Module):

def __init__(self, input_size, hidden_size, output_size): super(LSTMModel, self).__init__() self.lstm = nn.LSTM(input_size, hidden_size, batch_first=True) self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x): _, (hidden, _) = self.lstm(x) output = self.fc(hidden.squeeze(0)) return output
Deep Learning สำหรับข้อความด้วย PyTorch

RNN แบบอื่น: GRU

  • หัวเรื่องอีเมล:

       "Congratulations!
        You've won a free trip 
        to Hawaii!"
    

 

  • Gated Recurrent Unit (GRU) จดจำรูปแบบสแปมได้รวดเร็วโดยไม่ต้องอาศัยบริบทเต็ม

อีเมลสแปม

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

GRU

class GRUModel(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(GRUModel, self).__init__()
        self.gru = nn.GRU(input_size, hidden_size, batch_first=True)
        self.fc = nn.Linear(hidden_size, output_size)

def forward(self, x): _, hidden = self.gru(x) output = self.fc(hidden.squeeze(0)) return output
Deep Learning สำหรับข้อความด้วย PyTorch

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

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

Preparing Video For Download...