Estenda nn.Module: __init__ e forward
Lo scheletro standard di un modello PyTorch
Estenda nn.Module: __init__ e forward è una lezione Deep Learning Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Deep Learning Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Deep Learning Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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). 🎯
Domande Frequenti
La lezione «Estenda nn.Module: __init__ e forward» è gratuita?
Sì — il testo completo di «Estenda nn.Module: __init__ e forward» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Deep Learning Academy, passa a CoddyKit PRO. Il corso Deep Learning Academy include 4 lezioni in totale.
Cosa imparerò in «Estenda nn.Module: __init__ e forward»?
Lo scheletro standard di un modello PyTorch Eserciti Deep Learning Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Deep Learning Academy?
Non è richiesta alcuna esperienza precedente. Deep Learning Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.
Quanto tempo richiede la lezione «Estenda nn.Module: __init__ e forward»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Deep Learning Academy?
Sì. Ogni lezione Deep Learning Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Estenda nn.Module: __init__ e forward
- Sovrapporre layer lineari
- nn.Sequential per modelli rapidi
- Ispezionare parametri e forme dei layer