تدريب مصنّف نصوص
ضمّن النص، واجمعه، وتنبّأ بالمشاعر
تدريب مصنّف نصوص درس مجاني في Deep Learning Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Deep Learning Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The Goal: Predict a Label
A text classifier reads a sentence and predicts a label, like positive or negative sentiment for a movie review. 🎬
Step One: Encode the Text
Reuse your pipeline: tokenize each review and map tokens to ids, turning every sentence into a list of integers.
Step Two: Embed the Ids
Feed those ids into an embedding layer. Each review becomes a sequence of dense word vectors the model can process.
self.emb = nn.Embedding(vocab_size, 32)Step Three: Pool the Vectors
A sentence has many vectors but you need one. Pooling, often a mean over the words, collapses them into a single vector.
pooled = vecs.mean(dim=1) # average over the sequenceStep Four: A Linear Head
Send the pooled vector through a linear layer to produce one score per class. These raw scores are called logits.
self.fc = nn.Linear(32, num_classes)Assemble the Model
Stack embed, pool, and the linear head in a forward method. That is a complete, tiny text classifier.
def forward(self, ids):
x = self.emb(ids).mean(dim=1)
return self.fc(x)Pick the Loss
For multiclass labels use cross-entropy loss. It expects raw logits and the integer class index as the target.
loss_fn = nn.CrossEntropyLoss()Choose an Optimizer
An optimizer like Adam updates the embedding and linear weights together as it minimizes the loss.
opt = torch.optim.Adam(model.parameters(), lr=1e-3)The Training Loop
For each batch: forward, compute loss, backward, and step. Repeat over the data for several epochs.
logits = model(ids)
loss = loss_fn(logits, labels)
loss.backward()
opt.step()Make a Prediction
At inference, take the class with the highest logit using argmax to get the predicted label for new text.
pred = model(ids).argmax(dim=1)Measure Accuracy
Compare predictions to true labels to compute accuracy. Watch it climb as the embedding learns sentiment patterns.
Quick Check
In this classifier, what turns a sequence of word vectors into one vector?
Recap
You embed ids, pool them into one vector, classify with a linear head, and train with cross-entropy to label text. ✅
تعلم Python مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 30
- الدروس
- 120
الأسئلة الشائعة
هل درس «تدريب مصنّف نصوص» مجاني؟
نعم — نص درس «تدريب مصنّف نصوص» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Deep Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
ماذا ستتعلم في «تدريب مصنّف نصوص»؟
ضمّن النص، واجمعه، وتنبّأ بالمشاعر تتمرن على Deep Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Deep Learning Academy؟
لا تُشترط خبرة سابقة. Deep Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «تدريب مصنّف نصوص»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Deep Learning Academy هذا؟
نعم. كل درس في Deep Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تجزئة النص وبناء مفردات
- nn.Embedding: متجهات كلمات قابلة للتعلّم
- لماذا تلتقط Embeddings المعنى
- تدريب مصنّف نصوص