Kódování textových dat

Deep Learning for Text with PyTorch

Shubham Jain

Data Scientist

Kódování textu

Zpracovací pipeline Pytorch

  • Převod textu na strojově čitelná čísla
  • Umožňuje analýzu a modelování

Obrázek sekvenčních dat pro získání poznatků

Deep Learning for Text with PyTorch

Techniky kódování

  • One-hot kódování: převádí slova na jedinečné číselné reprezentace
  • Bag-of-Words (BoW): zachycuje četnost slov bez ohledu na pořadí
  • TF-IDF: vyvažuje jedinečnost a důležitost
  • Embedding: převádí slova na vektory zachycující sémantický význam (kapitola 2)
Deep Learning for Text with PyTorch

One-hot kódování

  • Mapování každého slova na jedinečný vektor
  • Binární vektor:
    • 1 pro přítomnost slova
    • 0 pro nepřítomnost slova
  • ['cat', 'dog', 'rabbit']
    • 'cat' [1, 0, 0]
    • 'dog' [0, 1, 0]
    • 'rabbit' [0, 0, 1]
Deep Learning for Text with PyTorch

One-hot kódování v 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 for Text with PyTorch

Bag-of-words

  • Příklad: "The cat sat on the mat"
  • Bag-of-words:
    • {'the': 2, 'cat': 1, 'sat': 1, 'on': 1, 'mat': 1}
  • Každý dokument je neuspořádaná sbírka slov
  • Zaměřuje se na četnost, nikoli na pořadí
Deep Learning for Text with 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 for Text with PyTorch

TF-IDF

  • Term Frequency-Inverse Document Frequency
    • Hodnotí důležitost slov v dokumentu
    • Vzácná slova mají vyšší skóre
    • Běžná slova mají nižší skóre
    • Zvýrazňuje informativní slova
Deep Learning for Text with 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 for Text with PyTorch

TfidfVectorizer

Kód TF-IDF

Deep Learning for Text with PyTorch

Techniky kódování

Techniky: one-hot kódování, bag-of-words a TF-IDF

  • Umožňují modelům porozumět textu a zpracovat ho
  • Zvolte jednu techniku, aby nedocházelo k redundanci
  • Existují i další techniky
Deep Learning for Text with PyTorch

Pojďme si procvičit!

Deep Learning for Text with PyTorch

Preparing Video For Download...