Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Class

nn.Module

torch.nn.Module is the base class for PyTorch neural-network modules. Subclass it to define models or reusable components, compose child modules, and manage registered parameters and module-wide conversions.

How it works

Subclass nn.Module to define a callable neural-network component. Initialize child modules and state in __init__, implement forward for the computation, and assign child modules as attributes so PyTorch registers them for parameter discovery and operations such as device or dtype conversion.

Syntax

class torch.nn.Module(*args, **kwargs)

Parameters

NameTypeDescription
*argsobjectPositional arguments accepted by the base constructor.
**kwargsobjectKeyword arguments accepted by the base constructor.

Returns

A Module instance; subclasses provide the model behavior through forward.

Key points

  • nn.Module is the foundational class for PyTorch neural-network components and models.
  • Child modules can be attached with ordinary attribute assignment.
  • Registered descendants participate when module-wide conversions such as to() are invoked.
  • A module may compose other modules into a nested architecture.

Common mistakes

  • Assign child modules as attributes; otherwise they are not registered through the documented attribute-assignment mechanism.
  • Custom models should inherit from nn.Module rather than being unrelated callable Python classes.
  • Nested modules form a hierarchy, so composition should use Module instances when building reusable model components.

Example: A small registered neural network

import torch
from torch import nn

class MLP(nn.Module):
    def __init__(self, width=8):
        super().__init__()
        self.layers = nn.Sequential(
            nn.Linear(4, width),
            nn.ReLU(),
            nn.Linear(width, 2),
        )

    def forward(self, x):
        return self.layers(x)

model = MLP()
print(model(torch.randn(3, 4)).shape)

Subclass nn.Module, register layers as attributes, and implement forward so the instance can be called with input tensors.

When to use it

Custom models

Define a trainable neural network with layers and custom computation.

Reusable components

Package a reusable operation together with its parameters and submodules.

Composed model trees

Build hierarchical architectures from smaller neural-network modules.

Interview questions

QWhat is nn.Module used for?

nn.Module is PyTorch’s base class for neural-network modules. A custom model subclasses it, initializes child modules, and defines forward for the computation.

Follow-up: How does composition work? A Module can contain other Modules, producing a nested module tree.

QWhat happens when a child module is assigned as an attribute?

Assigning child modules as attributes registers them with the parent. This lets PyTorch discover the submodules and apply operations such as to() to their parameters.

Follow-up: Why prefer nn.Sequential or ModuleList over a plain Python list? Registered containers ensure contained modules participate in the module hierarchy.

QHow is a custom Module structured?

The documented class declaration is torch.nn.Module(*args, **kwargs). In practice, subclasses call super().__init__() before assigning their child modules.

Follow-up: What belongs in forward? The input-to-output computation; initialization and layer construction belong in __init__.

  • torch.nn.Parameter
  • torch.nn.Sequential
  • torch.nn.ModuleList
  • torch.nn.ModuleDict

Source: Module — PyTorch main documentation. Checked 2026-10-15.