TorchLean API

NN.Examples.Interop.PyTorch.MLP.Export

MLP PyTorch Fixture Export #

PyTorch code generator for the small MLP round-trip fixture.

The generated Python mirrors the common nn.Linear → ReLU → nn.Linear pattern. We also support embedding explicit weights into a state_dict-shaped dictionary, which is handy for round-trips and small regression tests.

How to name state_dict keys when exporting weights.

  • linear : WeightKeyStyle

    Keys like fc1.weight / fc2.bias (matches PyTorch nn.Linear modules).

  • sequential : WeightKeyStyle

    Keys like layers.0.weight / layers.2.bias (common when exporting nn.Sequential).

Instances For

    Key name for the first layer's weight tensor in a PyTorch state_dict.

    Instances For

      Key name for the first layer's bias tensor in a PyTorch state_dict.

      Instances For

        Key name for the second layer's weight tensor in a PyTorch state_dict.

        Instances For

          Key name for the second layer's bias tensor in a PyTorch state_dict.

          Instances For
            structure Export.MLPPyTorch.MLPExportMetadata (α : Type) (inDim hidDim outDim : ) :

            Metadata for an exported MLP.

            This is a small “record of facts” about the generated Python: shapes, dimensions, and optionally embedded weights for round-trip tests.

            Instances For
              def Export.MLPPyTorch.generateMLPPyTorchClassLines (inDim hidDim outDim : ) (className : String) :

              Emit the Python class body for a basic Linear → ReLU → Linear MLP.

              This returns lines (not a single string) so callers can splice it into larger scripts.

              Instances For
                def Export.MLPPyTorch.generateMLPPyTorchClass (inDim hidDim outDim : ) (className : String := "MLP") :

                Generate a standalone Python file containing an nn.Module MLP class.

                Instances For

                  Generate Python code for an MLP plus helper functions that embed concrete weights.

                  The output contains a get_mlp_state_dict function that returns a PyTorch-shaped dictionary (state_dict) and a load_mlp_weights helper that calls model.load_state_dict(...).

                  Instances For
                    def Export.MLPPyTorch.generateMLPWithSoftmax {inDim hidDim outDim : } (className : String := "MLPWithSoftmax") :

                    Generate a Python MLP class that ends with a Softmax (classification convenience).

                    This mirrors common compact PyTorch model code; full training pipelines usually use logits + a combined loss (e.g. CrossEntropyLoss) instead of an explicit softmax.

                    Instances For
                      def Export.MLPPyTorch.generateMLPWithSoftmaxLines {inDim hidDim outDim : } (className : String) :

                      Line-based version of generateMLPWithSoftmax for script composition.

                      Instances For
                        def Export.MLPPyTorch.exportMLPFromSpecChain {α : Type} {inDim hidDim outDim : } (_chain : ModSpec.SpecChain α (Spec.Shape.dim inDim Spec.Shape.scalar) (Spec.Shape.dim outDim Spec.Shape.scalar)) (className : String := "ExportedMLP") :
                        MLPExportMetadata α inDim hidDim outDim

                        Export metadata for an MLP described as a SpecChain.

                        The chain is accepted to keep the API aligned with SpecChain, while this exporter produces metadata from the explicit dimensions supplied by the type parameters.

                        Instances For

                          Like exportMLPFromSpecChain, but include explicit weights in the metadata record.

                          Instances For
                            def Export.MLPPyTorch.generateCompleteMLPExport {inDim hidDim outDim : } (className : String := "MLP") :

                            Generate a complete Python script for MLP demos.

                            This includes:

                            Instances For