LeNetForImageClassification
PretrainedModelClassificationHeadMixinLeNetForImageClassification(config: LeNetConfig)LeNet-5 with F6 fully-connected layer and final classifier.
Combines a LeNet convolutional trunk with the original
paper's two-layer fully-connected head: a hidden layer F6 mapping
120 → 84 followed by the output layer mapping 84 →
config.num_classes. When labels are supplied to
forward, a cross-entropy loss is returned alongside the
logits; otherwise loss is None. Used as a teaching baseline
on MNIST / Fashion-MNIST and as a parity reference for LeNet-style
architectures.
Parameters
configLeNetConfiglenet_5_cls for the paper-cited
configuration (10-way classifier, tanh, average pooling); pass a
custom config to retarget num_classes or to switch the
activation/pooling family.Attributes
configLeNetConfigfeaturesnn.Sequentialact_f6nn.Modulelucid.nn.Tanh in the
paper, lucid.nn.ReLU if config.activation="relu".classifiernn.Modulenum_classes, built by
ClassificationHeadMixin._build_classifier.Notes
From LeCun et al., "Gradient-Based Learning Applied to Document Recognition", Proc. IEEE 86(11):2278–2324, 1998, Figure 2. The original output layer was a Gaussian-RBF unit comparing F6 activations against fixed digit templates; this implementation uses the modern linear + cross-entropy head, which gave essentially the same accuracy in subsequent reimplementations. Loss is the standard categorical cross-entropy
computed only when labels is not None.
Examples
Run inference on a small MNIST-style batch:
>>> import lucid
>>> from lucid.models.vision.lenet import lenet_5_cls
>>> model = lenet_5_cls()
>>> x = lucid.randn(4, 1, 32, 32)
>>> out = model(x)
>>> out.logits.shape
(4, 10)
>>> out.loss is None
True
Compute a training loss given labels:
>>> labels = lucid.tensor([0, 1, 2, 3], dtype=lucid.int64)
>>> out = model(x, labels=labels)
>>> out.loss.shape
()Used by 2
Constructors
1Instance methods
1forward(x: Tensor, labels: Tensor | None = None)