Kodning av textdata

Djupinlärning för text med PyTorch

Shubham Jain

Data Scientist

Textkodning

Pytorch Processing Pipeline

  • Omvandlar text till maskinläsbara tal
  • Möjliggör analys och modellering

Bild på sekventiell data för att hitta insikter

Djupinlärning för text med PyTorch

Kodningstekniker

  • One-hot encoding: omvandlar ord till unika numeriska representationer
  • Bag-of-Words (BoW): fångar ordfrekvens, utan hänsyn till ordning
  • TF-IDF: balanserar unikhet och vikt
  • Embedding: konverterar ord till vektorer och fångar semantisk betydelse (kapitel 2)
Djupinlärning för text med PyTorch

One-hot encoding

  • Varje ord mappas till en unik vektor
  • Binär vektor:
    • 1 om ordet förekommer
    • 0 om ordet saknas
  • ['cat', 'dog', 'rabbit']
    • 'cat' [1, 0, 0]
    • 'dog' [0, 1, 0]
    • 'rabbit' [0, 0, 1]
Djupinlärning för text med PyTorch

One-hot encoding med 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.])}
Djupinlärning för text med PyTorch

Bag-of-words

  • Exempel: "The cat sat on the mat"
  • Bag-of-words:
    • {'the': 2, 'cat': 1, 'sat': 1, 'on': 1, 'mat': 1}
  • Varje dokument behandlas som en oordnad samling ord
  • Fokuserar på frekvens, inte ordning
Djupinlärning för text med 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']
Djupinlärning för text med PyTorch

TF-IDF

  • Term Frequency-Inverse Document Frequency
    • Poängsätter ordets vikt i ett dokument
    • Ovanliga ord får högre poäng
    • Vanliga ord får lägre poäng
    • Lyfter fram informativa ord
Djupinlärning för text med 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']
Djupinlärning för text med PyTorch

TfidfVectorizer

TFIDF-kod

Djupinlärning för text med PyTorch

Kodningstekniker

Tekniker: one-hot encoding, bag-of-words och TF-IDF

  • Gör det möjligt för modeller att förstå och bearbeta text
  • Välj en teknik för att undvika redundans
  • Det finns fler tekniker
Djupinlärning för text med PyTorch

Nu kör vi en övning!

Djupinlärning för text med PyTorch

Preparing Video For Download...