การเข้ารหัสข้อมูลข้อความ

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

Shubham Jain

Data Scientist

การเข้ารหัสข้อความ

Pytorch Processing Pipeline

  • แปลงข้อความให้เป็นตัวเลขที่เครื่องอ่านได้
  • เพื่อนำไปวิเคราะห์และสร้างโมเดล

ภาพข้อมูลลำดับสำหรับค้นหาข้อมูลเชิงลึก

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

เทคนิคการเข้ารหัส

  • One-hot encoding: แปลงคำให้เป็นตัวแทนตัวเลขที่ไม่ซ้ำกัน
  • Bag-of-Words (BoW): นับความถี่ของคำ โดยไม่คำนึงถึงลำดับ
  • TF-IDF: สมดุลระหว่างความหายากและความสำคัญของคำ
  • Embedding: แปลงคำให้เป็นเวกเตอร์ที่จับความหมาย (บทที่ 2)
Deep Learning สำหรับข้อความด้วย PyTorch

One-hot encoding

  • จับคู่แต่ละคำกับเวกเตอร์ที่ไม่ซ้ำกัน
  • เวกเตอร์ไบนารี:
    • 1 หมายถึงคำนั้นปรากฏอยู่
    • 0 หมายถึงคำนั้นไม่ปรากฏ
  • ['cat', 'dog', 'rabbit']
    • 'cat' [1, 0, 0]
    • 'dog' [0, 1, 0]
    • 'rabbit' [0, 0, 1]
Deep Learning สำหรับข้อความด้วย PyTorch

One-hot encoding ด้วย PyTorch

import torch
vocab = ['cat', 'dog', 'rabbit']

vocab_size = len(vocab)
one_hot_vectors = torch.eye(vocab_size)
one_hot_dict = {word: one_hot_vectors[i] for i, word in enumerate(vocab)}
print(one_hot_dict)
{'cat': tensor([1., 0., 0.]),
  'dog': tensor([0., 1., 0.]),
  'rabbit': tensor([0., 0., 1.])}
Deep Learning สำหรับข้อความด้วย PyTorch

Bag-of-words

  • ตัวอย่าง: "The cat sat on the mat"
  • Bag-of-words:
    • {'the': 2, 'cat': 1, 'sat': 1, 'on': 1, 'mat': 1}
  • มองแต่ละเอกสารเป็นกลุ่มคำที่ไม่มีลำดับ
  • เน้นที่ ความถี่ ไม่ใช่ลำดับ
Deep Learning สำหรับข้อความด้วย PyTorch

CountVectorizer

from sklearn.feature_extraction.text import CountVectorizer

vectorizer = CountVectorizer()
corpus = ['This is the first document.', 'This document is the second document.', 'And this is the third one.', 'Is this the first document?']
X = vectorizer.fit_transform(corpus)
print(X.toarray())
print(vectorizer.get_feature_names_out())
[[0 1 1 1 0 0 1 0 1]
 [0 2 0 1 0 1 1 0 1]
 [1 0 0 1 1 0 1 1 1]
 [0 1 1 1 0 0 1 0 1]]

['and' 'document' 'first' 'is' 'one' 'second' 'the' 'third' 'this']
Deep Learning สำหรับข้อความด้วย PyTorch

TF-IDF

  • Term Frequency-Inverse Document Frequency
    • ให้คะแนนความสำคัญของคำในเอกสาร
    • คำที่พบน้อยจะได้คะแนนสูงกว่า
    • คำที่พบบ่อยจะได้คะแนนต่ำกว่า
    • เน้นคำที่ให้ข้อมูลที่มีความหมาย
Deep Learning สำหรับข้อความด้วย PyTorch

TfidfVectorizer

from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer()

corpus = ['This is the first document.','This document is the second document.', 'And this is the third one.','Is this the first document?']
X = vectorizer.fit_transform(corpus)
print(X.toarray())
print(vectorizer.get_feature_names_out())
[[0.         0.         0.68091856 0.51785612 0.51785612 0.        ]
 [0.         0.         0.          0.51785612 0.51785612 0.68091856]
 [0.85151335 0.42575668 0.         0.32274454 0.32274454 0.        ]
 [0.         0.         0.68091856 0.51785612 0.51785612 0.        ]]

['and' 'document' 'first' 'is' 'one' 'second']
Deep Learning สำหรับข้อความด้วย PyTorch

TfidfVectorizer

โค้ด TFIDF

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

เทคนิคการเข้ารหัส

เทคนิค: One-hot encoding, Bag-of-words และ TF-IDF

  • ช่วยให้โมเดลเข้าใจและประมวลผลข้อความได้
  • เลือกเพียงเทคนิคเดียวเพื่อหลีกเลี่ยงความซ้ำซ้อน
  • ยังมีเทคนิคอื่น ๆ อีก
Deep Learning สำหรับข้อความด้วย PyTorch

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

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

Preparing Video For Download...