توريث nn.Module: __init__ وforward
الهيكل القياسي لنموذج PyTorch
توريث nn.Module: __init__ وforward درس مجاني في Deep Learning Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Deep Learning Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Models Are Just Classes
In PyTorch, every model is a Python class. You build yours by subclassing nn.Module, the base that powers all networks.
Two Methods Run the Show
A model needs only two methods to work: __init__ sets up the layers, and forward describes how data flows through them.
Always Call super().__init__
The very first line inside __init__ must call super().__init__(). This wires your model into PyTorch and lets it track everything. 🔌
class Net(nn.Module):
def __init__(self):
super().__init__()Define Layers in __init__
Inside __init__ you create your layers and store them as attributes. Saving them on self lets PyTorch register them automatically.
self.fc1 = nn.Linear(4, 8)
self.fc2 = nn.Linear(8, 2)forward Is the Recipe
The forward method takes an input tensor and returns an output. It spells out the exact order your layers process the data.
def forward(self, x):
x = self.fc1(x)
return self.fc2(x)Never Call forward Directly
You call the model like a function, not model.forward(x). Using model(x) runs hooks and bookkeeping that forward alone skips.
out = model(x) # preferred
# not: out = model.forward(x)Layers Become Parameters
Because layers live on self, their weights are collected as the model's parameters. The optimizer later updates exactly these.
Instantiate Then Use
Create the model once, then feed it tensors many times. Each call flows through the same learned weights.
model = Net()
prediction = model(sample_input)Shapes Must Line Up
Each layer's output size must match the next layer's input size. Plan these dimensions as data travels through forward.
Why This Pattern Wins
Subclassing keeps setup and flow cleanly apart. The same simple skeleton scales from a tiny net to a giant one.
Flexible by Design
Inside forward you can branch, reshape, or reuse layers freely. This freedom is why custom nn.Module classes are so powerful. 💪
Quick Check
Think about which method defines how data moves through the network.
Recap
You build a model by subclassing nn.Module, defining layers in __init__ and the data flow in forward. Then just call model(x). 🎯
الأسئلة الشائعة
هل درس «توريث nn.Module: __init__ وforward» مجاني؟
نعم — نص درس «توريث nn.Module: __init__ وforward» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Deep Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
ماذا ستتعلم في «توريث nn.Module: __init__ وforward»؟
الهيكل القياسي لنموذج PyTorch تتمرن على Deep Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Deep Learning Academy؟
لا تُشترط خبرة سابقة. Deep Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «توريث nn.Module: __init__ وforward»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Deep Learning Academy هذا؟
نعم. كل درس في Deep Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- توريث nn.Module: __init__ وforward
- تكديس الطبقات الخطية
- nn.Sequential للنماذج السريعة
- فحص المعاملات وأشكال الطبقات