pulsatrix
Loading...
Searching...
No Matches
pulsatrix::MnistConvNet Class Reference

Conv2D(1,8,5,5) -> ReLU -> Flatten -> Linear(4608,10), trained via CrossEntropyLoss + Adam, one real MNIST image at a time (this library has no batch dimension anywhere, same constraint XorNetwork already works under). More...

#include <mnist_classifier_example.hpp>

Public Member Functions

 MnistConvNet (DeviceBackend *backend, unsigned seed=42)
 Constructs the network with randomly initialized weights.
 
Tensor forward (const Tensor &image)
 Runs the network forward.
 
int64_t predict (const Tensor &image)
 forward() plus argmax – the predicted class index.
 
float train_step (const Tensor &image, int64_t target_class, AdamOptimizer &optimizer, MetricsSink &sink, int step)
 Runs one training step: forward, cross-entropy loss, backward through every layer, one Adam update per layer's parameters, and logs the loss.
 
const Tensor & classifier_weight () const
 Test/inspection accessor.
 
std::vector< Module * > modules ()
 The network's layers in forward order (Conv2D, ReLU, Flatten, Linear), for building an ExplainerContext over the trained model – so every explainer (Saliency, IntegratedGradients, GradCAM, LRP, ...) and build_circuit_graph() can run on exactly the weights train_step() learned. The pointers alias this object's members: they stay valid only while this MnistConvNet is alive.
 

Detailed Description

Conv2D(1,8,5,5) -> ReLU -> Flatten -> Linear(4608,10), trained via CrossEntropyLoss + Adam, one real MNIST image at a time (this library has no batch dimension anywhere, same constraint XorNetwork already works under).

Note
Reuses grad_cam_mnist_demo.cpp's exact network shape (kernel size 5, 8 output channels) deliberately – once this network is trained on real data, a Grad-CAM call against it produces a meaningful heatmap for the first time in this project, not just pipeline mechanics on random weights.
Random (not hand-picked) initial weights – unlike XorNetwork's 12-weight network, hand-picking ~4600+ asymmetric values isn't practical. A fixed RNG seed keeps this reproducible; see mission Recon for why zero-init specifically doesn't work (identical rationale to XorNetwork's own note).

Constructor & Destructor Documentation

◆ MnistConvNet()

pulsatrix::MnistConvNet::MnistConvNet ( DeviceBackend *  backend,
unsigned  seed = 42 
)
explicit

Constructs the network with randomly initialized weights.

Parameters
backendBackend to compute through. Not owned; must outlive this network.
seedRNG seed for weight initialization.

Member Function Documentation

◆ classifier_weight()

const Tensor & pulsatrix::MnistConvNet::classifier_weight ( ) const
inline

Test/inspection accessor.

◆ forward()

Tensor pulsatrix::MnistConvNet::forward ( const Tensor &  image)

Runs the network forward.

Parameters
imageShape (1, 28, 28), pixel values normalized to [0,1].
Returns
Shape (10,) raw logits (pre-softmax).

◆ modules()

std::vector< Module * > pulsatrix::MnistConvNet::modules ( )
inline

The network's layers in forward order (Conv2D, ReLU, Flatten, Linear), for building an ExplainerContext over the trained model – so every explainer (Saliency, IntegratedGradients, GradCAM, LRP, ...) and build_circuit_graph() can run on exactly the weights train_step() learned. The pointers alias this object's members: they stay valid only while this MnistConvNet is alive.

◆ predict()

int64_t pulsatrix::MnistConvNet::predict ( const Tensor &  image)

forward() plus argmax – the predicted class index.

Parameters
imageShape (1, 28, 28).
Returns
Predicted class, 0-9.

◆ train_step()

float pulsatrix::MnistConvNet::train_step ( const Tensor &  image,
int64_t  target_class,
AdamOptimizer &  optimizer,
MetricsSink &  sink,
int  step 
)

Runs one training step: forward, cross-entropy loss, backward through every layer, one Adam update per layer's parameters, and logs the loss.

Parameters
imageShape (1, 28, 28).
target_classGround-truth class, 0-9.
optimizerOptimizer to update this network's parameters with.
sinkWhere the loss value is logged (tag "loss").
stepTraining step number, passed through to sink.
Returns
The loss value for this example, before the update.

The documentation for this class was generated from the following file: