When writing custom deep learning architectures in PyTorch, you typically define your model as a Python class. Which base class must your model inherit from to integrate with PyTorch's layer management and autograd systems?
Select an answer to reveal the explanation.
Short Explanation and Infographic
If you're writing code in PyTorch, torch.nn.Module is your absolute starting point. Think of it like the chassis of a car. Everything else you build—whether it's a single convolutional layer, a pooling layer, or the entire neural network model—bolts onto this chassis. When your class inherits from torch.nn.Module, PyTorch automatically tracks all your weights, handles backpropagation, and lets you move the whole model to a GPU with a single command. The optimizers, datasets, and loss functions have their own separate homes. Write this down: torch.nn.Module is the grandfather of all PyTorch models!
Full explanation below image
Full Explanation
In the PyTorch framework, torch.nn.Module is the fundamental base class for all neural network building blocks. When designing a neural network model, the developer creates a custom class that inherits from torch.nn.Module. This inheritance provides several critical functionalities out of the box: 1. Parameter Tracking: It automatically registers and tracks all weights and biases (instances of torch.nn.Parameter) defined within the model, allowing optimizers to easily access them. 2. Submodule Management: It recursively tracks child modules (such as linear or convolutional layers) instantiated within the model. 3. Execution Hook: It provides the forward pass interface, ensuring that calling the model object executes the forward method while managing PyTorch's internal autograd state. 4. Device Casting: It allows the entire network's parameters to be moved to a GPU (e.g., using .to('cuda')) or cast to different precisions in a single operation. The distractors refer to other parts of the PyTorch API: torch.optim.Optimizer is the base class for optimization algorithms like SGD and Adam; torch.utils.data.Dataset is used for data loading and batching; torch.nn.functional contains stateless activation and loss functions rather than serving as the object-oriented base class for model structure.