Metin verisini kodlama

PyTorch ile Metin için Deep Learning

Shubham Jain

Data Scientist

Metin kodlama

Pytorch İşleme Hattı

  • Metni makinenin okuyacağı sayılara dönüştürme
  • Analiz ve modellemeyi sağlar

Kestirim için Sıralı Veri Görseli

PyTorch ile Metin için Deep Learning

Kodlama teknikleri

  • One-hot encoding: sözcükleri benzersiz sayısal temsillere dönüştürür
  • Bag-of-Words (BoW): kelime sıklığını yakalar, sıralamayı önemsemez
  • TF-IDF: özgünlük ile önemi dengeler
  • Gömme (Embedding): sözcükleri anlamsal vektörlere çevirir (Bölüm 2)
PyTorch ile Metin için Deep Learning

One-hot encoding

  • Her kelimeyi ayrı bir vektöre eşleme
  • İkili vektör:
    • 1: kelime var
    • 0: kelime yok
  • ['cat', 'dog', 'rabbit']
    • 'cat' [1, 0, 0]
    • 'dog' [0, 1, 0]
    • 'rabbit' [0, 0, 1]
PyTorch ile Metin için Deep Learning

PyTorch ile one-hot encoding

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.])}
PyTorch ile Metin için Deep Learning

Bag-of-words

  • Örnek: "The cat sat on the mat"
  • Kelime torbası (bag-of-words):
    • {'the': 2, 'cat': 1, 'sat': 1, 'on': 1, 'mat': 1}
  • Her belgeyi sırasız kelime kümesi olarak ele alma
  • Sıklığa odaklanır, sıraya değil
PyTorch ile Metin için Deep Learning

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']
PyTorch ile Metin için Deep Learning

TF-IDF

  • Terim Frekansı-Ters Belge Frekansı
    • Bir belgedeki kelimelerin önemini puanlar
    • Nadir kelimelerin puanı daha yüksektir
    • Yaygın olanların puanı daha düşüktür
    • Bilgi verici kelimeleri vurgular
PyTorch ile Metin için Deep Learning

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']
PyTorch ile Metin için Deep Learning

TfidfVectorizer

TFIDF Kodu

PyTorch ile Metin için Deep Learning

Kodlama teknikleri

Teknikler: One-hot encoding, bag-of-words ve TF-IDF

  • Modellerin metni anlamasını ve işlemesini sağlar
  • Tekniği tek seçin, yinelenmeden kaçının
  • Başka teknikler de vardır
PyTorch ile Metin için Deep Learning

Hadi pratik yapalım!

PyTorch ile Metin için Deep Learning

Preparing Video For Download...