Learn Before
Code

AlexNet Model Code Implementation

The architecture of AlexNet can be implemented in modern deep learning frameworks like PyTorch by sequentially composing convolutional, pooling, and fully connected layers. The network starts with a large 11×1111 \times 11 convolution with a stride of 4, followed by ReLU activations and max-pooling operations. It then processes the feature maps through smaller 5×55 \times 5 and 3×33 \times 3 convolutions. Finally, the tensor is flattened and passed through two 4096-dimensional fully connected layers, where dropout (p=0.5p=0.5) is applied to prevent overfitting.

class AlexNet(d2l.Classifier): def __init__(self, lr=0.1, num_classes=10): super().__init__() self.save_hyperparameters() self.net = nn.Sequential( nn.LazyConv2d(96, kernel_size=11, stride=4, padding=1), nn.ReLU(), nn.MaxPool2d(kernel_size=3, stride=2), nn.LazyConv2d(256, kernel_size=5, padding=2), nn.ReLU(), nn.MaxPool2d(kernel_size=3, stride=2), nn.LazyConv2d(384, kernel_size=3, padding=1), nn.ReLU(), nn.LazyConv2d(384, kernel_size=3, padding=1), nn.ReLU(), nn.LazyConv2d(256, kernel_size=3, padding=1), nn.ReLU(), nn.MaxPool2d(kernel_size=3, stride=2), nn.Flatten(), nn.LazyLinear(4096), nn.ReLU(), nn.Dropout(p=0.5), nn.LazyLinear(4096), nn.ReLU(), nn.Dropout(p=0.5), nn.LazyLinear(num_classes)) self.net.apply(d2l.init_cnn)

0

1

Updated 2026-06-29

Tags

D2L

Dive into Deep Learning @ D2L