Course contentsShow
← Machine Learning and Deep Learning
Lesson 802 of 3,53819. PyTorch FundamentalsFree lesson

Creating Your First Custom Module

Build a simple custom module by subclassing nn.Module and implementing the required __init__ and forward methods.

Creating Your First Custom Module

What you'll learn: How to build your own neural network layer or model in PyTorch by subclassing nn.Module.

The Core Idea

In PyTorch, every neural network component—from a single layer to an entire model—is built by creating a class that inherits from nn.Module. This is the standard way to define custom architectures. You only need to implement two methods:

  1. __init__: Define the components (layers, parameters) your module will use
  2. forward: Specify how data flows through those components

Think of nn.Module as a blueprint for building blocks. The __init__ method is where you gather your materials (layers, weights), and the forward method is the instruction manual for assembling them.

Building Your First Module

import torch
import torch.nn as nn

class SimpleNet(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super().__init__()  # Always call the parent constructor
        self.fc1 = nn.Linear(input_size, hidden_size)
        self.fc2 = nn.Linear(hidden_size, output_size)
    
    def forward(self, x):
        x = self.fc1(x)
        x = torch.relu(x)
        x = self.fc2(x)
        return x

# Using your custom module
model = SimpleNet(input_size=10, hidden_size=20, output_size=2)
input_data = torch.randn(5, 10)  # batch of 5 samples
output = model(input_data)  # forward pass happens automatically

Note that you call model(input_data), not model.forward(input_data). PyTorch's nn.Module handles this for you, ensuring gradients are tracked properly.

Key Takeaway: Creating a custom module requires subclassing nn.Module, calling super().__init__(), defining your components in __init__, and specifying the data flow in forward. This pattern is the foundation of all PyTorch models.