Overview
Thetorch.nn module provides the building blocks for creating neural networks in PyTorch. It contains layers, activation functions, loss functions, and utilities for building and training deep learning models.
Key Components
Module Base Class
All neural network modules inherit fromtorch.nn.Module, which provides:
- Parameter management
- Forward pass computation
- Training/evaluation mode switching
- Device and dtype management
Main Submodules
Modules
Neural network layers and containers (Linear, Conv2d, Sequential, etc.)
Functional
Functional API for operations without learnable parameters
Initialization
Weight initialization methods for neural networks
Loss Functions
Training objectives like CrossEntropyLoss, MSELoss
Core Classes
Parameter
Tensor data for the parameter
Whether the parameter requires gradient computation
Buffer
Usage Example
Factory Kwargs
Dictionary that may contain device, dtype, memory_format, or factory_kwargs keys
Canonicalized dictionary with factory kwargs
Common Patterns
Model Definition
Training Mode
Device Management
Best Practices
Always call super().__init__()
Always call super().__init__()
When creating custom modules, always call the parent class constructor:
Use nn.Parameter for learnable weights
Use nn.Parameter for learnable weights
Tensors assigned as attributes won’t be registered as parameters unless wrapped:
Implement forward() method
Implement forward() method
The forward method defines the computation performed at every call:
See Also
- Neural Network Modules - Detailed module documentation
- Functional API - Stateless operations
- Weight Initialization - Initialize network weights