Mã hóa văn bản

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

Shubham Jain

Data Scientist

Mã hóa văn bản

Quy trình xử lý Pytorch

  • Chuyển văn bản thành số máy hiểu được
  • Phục vụ phân tích và mô hình hóa

Dữ liệu tuần tự để tìm insight

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

Kỹ thuật mã hóa

  • One-hot encoding: biến từ thành mã số duy nhất
  • Bag-of-Words (BoW): ghi nhận tần suất, bỏ qua thứ tự
  • TF-IDF: cân bằng tính độc nhất và mức quan trọng
  • Embedding: chuyển từ thành vector, giữ nghĩa ngữ nghĩa (Chương 2)
Deep Learning cho Văn bản với PyTorch

One-hot encoding

  • Ánh xạ mỗi từ thành một vector riêng
  • Vector nhị phân:
    • 1 nếu từ xuất hiện
    • 0 nếu từ không xuất hiện
  • ['cat', 'dog', 'rabbit']
    • 'cat' [1, 0, 0]
    • 'dog' [0, 1, 0]
    • 'rabbit' [0, 0, 1]
Deep Learning cho Văn bản với PyTorch

One-hot encoding với 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 cho Văn bản với PyTorch

Bag-of-Words

  • Ví dụ: "The cat sat on the mat"
  • Túi từ (Bag-of-Words):
    • {'the': 2, 'cat': 1, 'sat': 1, 'on': 1, 'mat': 1}
  • Xem mỗi tài liệu như một tập từ không có thứ tự
  • Tập trung vào tần suất, không phải thứ tự
Deep Learning cho Văn bản với 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 cho Văn bản với PyTorch

TF-IDF

  • Term Frequency–Inverse Document Frequency
    • Chấm điểm mức quan trọng của từ trong tài liệu
    • Từ hiếm có điểm cao hơn
    • Từ phổ biến có điểm thấp hơn
    • Nhấn mạnh các từ giàu thông tin
Deep Learning cho Văn bản với 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 cho Văn bản với PyTorch

TfidfVectorizer

Mã Tfidf

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

Kỹ thuật mã hóa

Kỹ thuật: One-hot encoding, Bag-of-Words, và TF-IDF

  • Giúp mô hình hiểu và xử lý văn bản
  • Chọn một kỹ thuật để tránh dư thừa
  • Còn nhiều kỹ thuật khác
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...