Sentiment Analysis CNN मॉडल का परीक्षण
अब जबकि मॉडल ट्रेन हो चुका है, PyBooks कुछ नई बुक रिव्यूज़ पर इसकी परफॉर्मेंस जाँचना चाहता है.
आपको जाँचना है कि किसी रिव्यू का सेंटिमेंट positive है या negative.
आपके लिए ये पैकेज इम्पोर्ट कर दिए गए हैं:
torch, torch.nn as nn, torch.nn.functional as F, torch.optim as optim.
vocab_size और embed_dim आर्ग्युमेंट्स के साथ TextClassificationCNN() का एक इंस्टेंस लोड कर के model नाम से सेव किया गया है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
PyTorch के साथ टेक्स्ट के लिए डीप लर्निंग
अभ्यास निर्देश
book_reviewsसूची पर इटरेट करें और हर रिव्यू के शब्दों को टेन्सर में कन्वर्ट करें.- हर
input_tensorके लिए मॉडल का आउटपुट प्राप्त करें. outputs.dataसे सबसे संभावित सेंटिमेंट कैटेगरी का इंडेक्स निकालें.predicted_labelआइटम को निकालकर उसे सेंटिमेंट स्ट्रिंग में कन्वर्ट करें, जहाँ1का अर्थ "Positive" लेबल है.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
book_reviews = [
"I love this book".split(),
"I do not like this book".split()
]
for review in book_reviews:
# Convert the review words into tensor form
input_tensor = ____.____([word_to_ix[w] for w in review], dtype=torch.long).unsqueeze(0)
# Get the model's output
outputs = model(____)
# Find the index of the most likely sentiment category
_, predicted_label = ____.____(outputs.data, 1)
# Convert the predicted label into a sentiment string
sentiment = "Positive" if ____ else "Negative"
print(f"Book Review: {' '.join(review)}")
print(f"Sentiment: {sentiment}\n")