Deep Learning für Text mit PyTorch
Shubham Jain
Data Scientist
"
"
{{3}}"
"- One-hot-Encoding: wandelt Wörter in eindeutige numerische Repräsentationen um
"- Abbildung jedes Wortes auf einen eindeutigen Vektor
"- ['Katze', 'Hund', 'Kaninchen']
"`py
import torch
vocab = ['cat', 'dog', 'rabbit']
----CODE_GLUE---- ```py 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)
out
{'cat': tensor([1., 0., 0.]),
'dog': tensor([0., 1., 0.]),
'rabbit': tensor([0., 0., 1.])}{{5}}"
"- Beispiel: \"Die Katze saß auf der Matte\"
"- Jedes Dokument wird als Sammlung von Wörtern betrachtet, ohne dass die Reihenfolge eine Rolle spielt.
from sklearn.feature_extraction.text import CountVectorizervectorizer = 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())
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]] DNT_CURLY_TAG_5 out ['and' 'document' 'first' 'is' 'one' 'second' 'the' 'third' 'this']
"- Termfrequenz-Inverse Dokumentfrequenz
from sklearn.feature_extraction.text import TfidfVectorizer vectorizer = TfidfVectorizer()corpus = ['Dies ist das erste Dokument.','Dieses Dokument ist das zweite Dokument.', 'Und dies ist das dritte.','Ist dies das erste Dokument?']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. ]] ```
"
"
"Techniken: One-hot-Encoding, Bag-of-Words und TF-IDF
Deep Learning für Text mit PyTorch