Subclass nn.Module: __init__ and forward
The standard PyTorch model skeleton.
Subclass nn.Module: __init__ and forward is a free Deep Learning Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Deep Learning Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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). 🎯
Frequently asked questions
Is the “Subclass nn.Module: __init__ and forward” lesson free?
Yes — the full text of “Subclass nn.Module: __init__ and forward” is free to read here on the web, and the Deep Learning Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Deep Learning Academy course, upgrade to CoddyKit PRO.
What will I learn in “Subclass nn.Module: __init__ and forward”?
The standard PyTorch model skeleton. You practise Deep Learning Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Deep Learning Academy?
No prior experience is required. Deep Learning Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Subclass nn.Module: __init__ and forward” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Deep Learning Academy lesson?
Yes. Every Deep Learning Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Subclass nn.Module: __init__ and forward
- Stacking Linear Layers
- nn.Sequential for Quick Models
- Inspect Parameters and Layer Shapes