nn module
nn module provides a high-level interface for building neural networks. It encapsulates layers, loss functions, and optimizers, making it easier to construct and train models.
Neural network components
Layers: Define the computational operations performed on input data. Loss functions: Measure the difference between predicted and actual values, guiding the optimization process. Containers: Organize layers and other components into a cohesive model structure. Initializers: Set initial values for model parameters, crucial for effective training.
nn.Sequential
nn.Sequential is a container that allows you to build a neural network by stacking layers in a sequential manner. It simplifies the model definition process by automatically managing the flow of data through the layers.
import torch.nn as nn
model = nn.Sequential(
nn.Linear(input_size, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, output_size),
nn.Softmax(dim=1)
)
-
Linear Layer Also known as a fully connected layer, it applies a linear transformation to the input data.
-
Convolutional Layer Applies convolution operations, commonly used in image processing tasks to extract features.
-
Pooling Layer Reduces the spatial dimensions of the input, retaining essential features while reducing computational load.
-
Recurrent Layer Processes sequential data, maintaining a hidden state to capture temporal dependencies.
-
Dropout Layer Randomly drops out a fraction of neurons during training, preventing overfitting.