Phase 23 of 25 · Topic 23.3

Building Neural Networks with torch.nn.Module

1Concept

Custom neural architectures subclass `torch.nn.Module`. Layers are defined in `__init__()`, and computation is defined in `forward(x)`. PyTorch tracks all trainable parameters automatically.

2Architecture Diagram

Input (dim=128) ---> [ Linear(128, 64) ] ---> [ ReLU ] ---> [ Linear(64, 1) ] ---> Output

3Code Example

Python 3.12
nn_module_code = '''
import torch
import torch.nn as nn

class EnterpriseClassifier(nn.Module):
    def __init__(self, in_features: int, hidden: int, classes: int):
        super().__init__()
        self.network = nn.Sequential(
            nn.Linear(in_features, hidden),
            nn.ReLU(),
            nn.Dropout(0.2),
            nn.Linear(hidden, classes)
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.network(x)

model = EnterpriseClassifier(in_features=10, hidden=32, classes=2)
print(f"Model Architecture:\n{model}")
'''
print("=== Deep Neural Network Architecture ===")
print(nn_module_code.strip())

4Expected Output

=== Deep Neural Network Architecture ===
import torch
import torch.nn as nn

class EnterpriseClassifier(nn.Module):
    def __init__(self, in_features: int, hidden: int, classes: int):
        super().__init__()
        self.network = nn.Sequential(
            nn.Linear(in_features, hidden),
            nn.ReLU(),
            nn.Dropout(0.2),
            nn.Linear(hidden, classes)
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.network(x)

model = EnterpriseClassifier(in_features=10, hidden=32, classes=2)
print(f"Model Architecture:\n{model}")

5Key Takeaways

  • Never call `model.forward(x)` directly; invoke `model(x)` so PyTorch hooks execute.
  • Call `model.eval()` before inference to disable Dropout and freeze BatchNorm layers.
  • `model.parameters()` provides parameter iterators to optimizers.