SmallCNN
A compact convolutional neural network designed for edge device deployment. Optimized for 28x28 grayscale images with configurable channel dimensions.Constructor
Number of output channels for the first convolutional layer. Controls the model’s feature extraction capacity in the initial layer.
Number of output channels for the second convolutional layer. Determines the depth of features in the second layer.
Number of output classes for classification. The final linear layer outputs this many logits.
Architecture Details
The SmallCNN architecture consists of:- Layer 1: Conv2d(1 → conv1_channels, kernel=3, padding=1) → ReLU → MaxPool2d(2)
- Layer 2: Conv2d(conv1_channels → conv2_channels, kernel=3, padding=1) → ReLU → MaxPool2d(2)
- Classifier: Flatten → Linear(conv2_channels × 7 × 7 → num_classes)
The model expects input tensors of shape
(batch_size, 1, 28, 28). After two pooling operations, the spatial dimensions reduce from 28×28 to 7×7.forward
Performs forward pass through the network.Input tensor of shape
(batch_size, 1, 28, 28) representing grayscale images.Classification logits of shape
(batch_size, num_classes).Usage Example
set_deterministic
Configures all random number generators for deterministic behavior across PyTorch, NumPy, and Python’s random module. Essential for reproducible experiments on edge devices.Random seed value to set across all libraries. Use the same seed to reproduce results.
What It Does
This function sets:- Python random:
random.seed(seed) - NumPy:
np.random.seed(seed) - PyTorch CPU:
torch.manual_seed(seed) - PyTorch CUDA:
torch.cuda.manual_seed_all(seed) - cuDNN: Sets
deterministic=Trueandbenchmark=False - PyTorch algorithms: Enables
use_deterministic_algorithms(True)
Enabling deterministic mode may reduce performance. Use for reproducibility during experiments, but consider disabling for production deployments.