torch.nn.Module is the base class for defining PyTorch neural network models. A model subclasses it, creates its layers in __init__(), and defines how inputs move through those layers in forward(). Calling the model, such as nn(X), invokes that forward pass. section 8.5 section 10.6.1
A Module commonly combines:
- Learnable layers such as Linear or Conv2d, instantiated during initialization. section 8.5 section 10.6.1
- Nonlinear operations such as Sigmoid, arranged with other layers in torch.nn.Sequential. section 8.5
- A forward computation that passes input tensors through the assembled architecture and returns predictions or intermediate outputs. section 8.5 section 10.6.1
Two-layer regression network
A demand-prediction model subclasses torch.nn.Module and stores a Sequential pipeline: Linear(input_size, 10), Sigmoid, Linear(10, 5), Sigmoid, and Linear(5, 1). Its forward() method returns self.model(X). The resulting module can be called during training, passed to MSELoss, and supplied to an SGD optimizer through nn.parameters(). section 8.5
class TwoLayeredNN(torch.nn.Module):
def __init__(self, input_size, hidden1_size, hidden2_size, output_size):
super(TwoLayeredNN, self).__init__()
self.model = torch.nn.Sequential( #1
torch.nn.Linear(input_size, hidden1_size), #2
torch.nn.Sigmoid(),
torch.nn.Linear(hidden1_size, hidden2_size), #3
torch.nn.Sigmoid(),
torch.nn.Linear(hidden2_size, output_size) #4
)
def forward(self, X): #5
return self.model(X)
nn = TwoLayeredNN(input_size=X.shape[-1], hidden1_size=10,
hidden2_size=5, output_size=1)
Inspect the complete Module definition showing initialization, Sequential composition, layer sizes, forward(), and model construction. section 8.5





