ภาพรวมของการจำแนกข้อความ

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

Shubham Jain

Instructor

นิยามของการจำแนกข้อความ

  • การกำหนดป้ายกำกับให้กับข้อความ
  • การให้ความหมายแก่คำและประโยค

 

 

ประเภทของการจำแนกใน Machine Learning

  • จัดระเบียบและเพิ่มโครงสร้างให้กับข้อมูลที่ไม่มีโครงสร้าง
  • การประยุกต์ใช้:

    • วิเคราะห์ความรู้สึกของลูกค้าจากรีวิว
    • ตรวจจับสแปมในอีเมล
    • แท็กบทความข่าวด้วยหัวข้อที่เกี่ยวข้อง
  • ประเภท: binary, multi-class, multi-label

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

Binary classification

  • แบ่งออกเป็นสองหมวดหมู่
  • ตัวอย่าง: การตรวจจับสแปมในอีเมล
  • อีเมลจะถูกจำแนกเป็น 'spam' หรือ 'not spam'

Binary Classification

1 https://storage.googleapis.com/gweb-cloudblog-publish/images/image4_v2LFcq0.max-1200x1200.png
Deep Learning สำหรับข้อความด้วย PyTorch

Multi-class classification

การจำแนกข่าว

  • แบ่งออกเป็นหลายหมวดหมู่
  • ตัวอย่าง: บทความข่าว สามารถแบ่งได้หลายหมวดหมู่ เช่น
    1. การเมือง
    2. กีฬา
    3. เทคโนโลยี
Deep Learning สำหรับข้อความด้วย PyTorch

Multi-label classification

  • ข้อความหนึ่งชิ้นสามารถกำหนดได้หลายป้ายกำกับ
  • ตัวอย่าง: หนังสือ หนึ่งเล่มอาจอยู่ในหลายแนว
    • แอ็กชัน
    • ผจญภัย
    • แฟนตาซี
Deep Learning สำหรับข้อความด้วย PyTorch

Word embedding คืออะไร

ไปป์ไลน์ Word Embedding

ตัวอย่าง Word Embedding

  • เทคนิค encoding แบบเดิมเป็นจุดเริ่มต้นที่ดี
    • แต่มักสร้าง feature มากเกินไปและไม่สามารถระบุคำที่คล้ายกันได้
  • Word embedding แมปคำไปยังเวกเตอร์ตัวเลข
  • ตัวอย่างความสัมพันธ์เชิงความหมาย:
    • King และ queen
    • Man และ woman
Deep Learning สำหรับข้อความด้วย PyTorch

การแมปคำไปยัง index

  • ตัวอย่าง:
    • "King" -> 1
    • "Queen" -> 2
  • กระชับและประมวลผลได้อย่างมีประสิทธิภาพ
  • อยู่ถัดจากขั้นตอน tokenization ในไปป์ไลน์
Deep Learning สำหรับข้อความด้วย PyTorch

Word embedding ใน PyTorch

  • torch.nn.Embedding:
    • สร้างเวกเตอร์คำจาก index

 

  • Input: Index ของ ['The', 'cat', 'sat', 'on', 'the', 'mat']
Embedding for 'the': tensor([-0.4689,  0.3164, -0.2971, -0.1291,  0.4064])
Embedding for 'cat': tensor([-0.0978, -0.4764,  0.0476,  0.1044, -0.3976])
Embedding for 'sat': tensor([ 0.2731,  0.4431,  0.1275,  0.1434, -0.4721])
Deep Learning สำหรับข้อความด้วย PyTorch

การใช้ torch.nn.Embedding

import torch
from torch import nn

words = ["The", "cat", "sat", "on", "the", "mat"] word_to_idx = {word: i for i, word in enumerate(words)}
inputs = torch.LongTensor([word_to_idx[w] for w in words])
embedding = nn.Embedding(num_embeddings=len(words), embedding_dim=10)
output = embedding(inputs)
print(output)
tensor([[ 1.0624,  0.6792,  0.0459,  ... -1.0828, -0.4475,  0.4868],
         ...
         [1.5766,  0.0106,  0.1161,  ...,,  -0.0859, 1.3160,  1.3621])
Deep Learning สำหรับข้อความด้วย PyTorch

การใช้ embedding ในไปป์ไลน์

def preprocess_sentences(text):
  # Tokenization
  # Stemming
  ...

# Word to index mapping
class TextDataset(Dataset): def __init__(self, encoded_sentences): self.data = encoded_sentences def __len__(self): return len(self.data) def __getitem__(self, index): return self.data[index]
def text_processing_pipeline(text):
    tokens = preprocess_sentences(text)
    dataset = TextDataset(tokens)
    dataloader = DataLoader(dataset, batch_size=2, 
                            shuffle=True)
    return dataloader, vectorizer

text = "Your sample text here." dataloader, vectorizer = text_processing_pipeline(text)
embedding = nn.Embedding(num_embeddings=10, embedding_dim=50) for batch in dataloader: output = embedding(batch) print(output)
Deep Learning สำหรับข้อความด้วย PyTorch

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

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

Preparing Video For Download...