टेक्स्ट क्लासिफिकेशन के लिए Convolutional Neural Networks

PyTorch के साथ टेक्स्ट के लिए डीप लर्निंग

Shubham Jain

Instructor

टेक्स्ट क्लासिफिकेशन के लिए CNNs

  • ट्वीट्स को {{1}} के रूप में क्लासिफाई करना
    • Positive
    • Negative
    • Neutral
PyTorch के साथ टेक्स्ट के लिए डीप लर्निंग

Convolution ऑपरेशन

Convolution ऑपरेशन

  • Convolution ऑपरेशन
    • इनपुट डेटा पर एक फ़िल्टर (kernel) स्लाइड करना
    • फ़िल्टर की हर पोज़ीशन पर एलिमेंट-वाइज़ कैलकुलेशन करना

 

  • टेक्स्ट के लिए: शब्दों की संरचना और मतलब सीखता है
1 Animation from Vincent Dumoulin, Francesco Visin
PyTorch के साथ टेक्स्ट के लिए डीप लर्निंग

CNNs में फ़िल्टर और स्ट्राइड

  • फ़िल्टर:
    • छोटी मैट्रिक्स जिसे हम इनपुट पर स्लाइड करते हैं

 

  • स्ट्राइड:
    • फ़िल्टर जितनी पोज़ीशंस आगे बढ़ता है

फ़िल्टर और स्ट्राइड

1 Animation from Vincent Dumoulin, Francesco Visin
PyTorch के साथ टेक्स्ट के लिए डीप लर्निंग

टेक्स्ट के लिए CNN आर्किटेक्चर

  • Convolutional लेयर: इनपुट डेटा पर फ़िल्टर अप्लाई करती है
  • Pooling लेयर: अहम जानकारी रखते हुए डेटा साइज घटाती है
  • Fully connected लेयर: पिछली लेयर के आउटपुट पर अंतिम प्रेडिक्शन करती है
PyTorch के साथ टेक्स्ट के लिए डीप लर्निंग

CNN से टेक्स्ट क्लासिफिकेशन मॉडल इम्प्लीमेंट करना

class SentimentAnalysisCNN(nn.Module):

def __init__(self, vocab_size, embed_dim):
super().__init__()
self.embedding = nn.Embedding(vocab_size, embed_dim)
self.conv = nn.Conv1d(embed_dim, embed_dim, kernel_size=3, stride=1, padding=1)
self.fc = nn.Linear(embed_dim, 2) ...
  • __init__ मेथड आर्किटेक्चर कॉन्फ़िगर करता है
  • super() बेस क्लास nn.Module इनिशियलाइज़ करता है
  • nn.Embedding डेंस वर्ड वेक्टर बनाता है
  • nn.Conv1d one-dimensional डेटा के लिए
PyTorch के साथ टेक्स्ट के लिए डीप लर्निंग

CNN से टेक्स्ट क्लासिफिकेशन मॉडल इम्प्लीमेंट करना

    ...
    def forward(self, text):
        embedded = self.embedding(text).permute(0, 2, 1)

conved = F.relu(self.conv(embedded))
conved = conved.mean(dim=2)
return self.fc(conved)
  • Embedding लेयर टेक्स्ट को एम्बेडिंग में बदलती है
  • टेन्सर को convolution लेयर की अपेक्षित इनपुट से मैच करें
  • ReLU से अहम फीचर्स निकालें
  • अतिरिक्त लेयर और डायमेंशन हटाएँ
PyTorch के साथ टेक्स्ट के लिए डीप लर्निंग

Sentiment analysis मॉडल के लिए डेटा तैयार करना

vocab = ["i", "love", "this", "book", "do", "not", "like"]
word_to_idx = {word: i for i, word in enumerate(vocab)}

vocab_size = len(word_to_ix)
embed_dim = 10
book_samples = [ ("The story was captivating and kept me hooked until the end.".split(),1), ("I found the characters shallow and the plot predictable.".split(),0) ]
model = SentimentAnalysisCNN(vocab_size, embed_dim) criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=0.1)
PyTorch के साथ टेक्स्ट के लिए डीप लर्निंग

मॉडल ट्रेन करना

for epoch in range(10):  
    for sentence, label in data:

model.zero_grad()
sentence = torch.LongTensor([word_to_idx.get(w, 0) for w in sentence]).unsqueeze(0)
outputs = model(sentence) label = torch.LongTensor([int(label)])
loss = criterion(outputs, label) loss.backward()
optimizer.step()
PyTorch के साथ टेक्स्ट के लिए डीप लर्निंग

Sentiment Analysis मॉडल चलाना

for sample in book_samples:

input_tensor = torch.tensor([word_to_idx[w] for w in sample], dtype=torch.long).unsqueeze(0)
outputs = model(input_tensor)
_, predicted_label = torch.max(outputs.data, 1)
sentiment = "Positive" if predicted_label.item() == 1 else "Negative"
print(f"Book Review: {' '.join(sample)}") print(f"Sentiment: {sentiment}\n")
Book Review: The story was captivating and kept me hooked until the end
Sentiment: Positive
Book Review: I found the characters shallow and the plot predictable
Sentiment: Negative
PyTorch के साथ टेक्स्ट के लिए डीप लर्निंग

अभ्यास करते हैं!

PyTorch के साथ टेक्स्ट के लिए डीप लर्निंग

Preparing Video For Download...