TorchLean API

NN.Examples.Interop.PyTorch.CNN.Export

CNN PyTorch Fixture Export #

PyTorch exporter for the small 2-block ConvNet round-trip fixture.

This exporter is meant to mirror the "classic demo CNN" shape that shows up in many TorchLean examples: two Conv2d + ReLU + MaxPool2d blocks, then Flatten, then a single Linear head.

Instead of taking a long positional list of naturals, we use small configuration records so call sites stay readable and it's easy to extend the shape later.

Configuration for a PyTorch nn.Conv2d layer.

  • inChannels :

    Input channels (in_channels).

  • outChannels :

    Output channels (out_channels).

  • kernelH :

    Kernel height.

  • kernelW :

    Kernel width.

  • stride :

    Stride (applied to both spatial dims).

  • padding :

    Zero-padding (applied to both spatial dims).

Instances For

    Configuration for a PyTorch nn.MaxPool2d layer.

    • kernelH :

      Pool kernel height.

    • kernelW :

      Pool kernel width.

    • stride :

      Pool stride (applied to both spatial dims).

    Instances For

      Configuration for the 2-block CNN exporter.

      Instances For

        Render the 2-block CNN as a Python nn.Module class definition.

        Instances For
          def Export.CNNPyTorch.generateCNNPyTorchClass (inC outC inH inW kH kW stride1 padding1 stride2 padding2 poolKH poolKW poolStride1 poolStride2 flatSize : ) (className : String := "CNN") :

          Backwards-compatible wrapper for generateCnn2PyTorchClass.

          Call sites that pass a long positional list of naturals can use this adapter.

          Instances For
            def Export.CNNPyTorch.generateCNNWithWeights (convW1 convB1 convW2 convB2 linearW linearB : String) (inC outC inH inW kH kW stride1 padding1 stride2 padding2 poolKH poolKW poolstride1 poolstride2 flatSize : ) (className : String := "CNN") :

            Generate a Python CNN module plus a helper that loads explicit weights from string literals.

            This is mainly used for demos: you can paste JSON/Lean-rendered weight arrays into Python and run the model without writing an extra serializer.

            Instances For