TorchLean API

NN.API.Samples

Synthetic Samples and Small Datasets #

This module provides small deterministic data generators used by examples and tests:

These helpers are simple and in-memory. They keep tutorial code focused on models and verification rather than data-loading infrastructure.

Tabular 2D #

@[reducible, inline]
abbrev NN.API.Samples.affine2 (w1 w2 b x1 x2 : Float) :

2D affine synthetic target function generator (re-exported from the runtime helper module).

Instances For

    A length-1 float vector tensor.

    Instances For

      A length-2 float vector tensor.

      Instances For

        Cartesian product of two float vectors (batched tensor of points).

        grid2 xs ys produces a tensor X : (m*n, 2) containing all pairs (x, y) with:

        • x taken from xs : (m,)
        • y taken from ys : (n,)

        Ordering is row-major: for each x in xs (outer loop), we sweep all y in ys (inner loop).

        PyTorch analogue: torch.cartesian_prod(xs, ys) (up to shape).

        Instances For

          Linearly spaced points including endpoints.

          linspace lo hi count returns a vector tensor of shape (count,):

          • empty if count = 0
          • [lo] if count = 1
          • otherwise count points from lo to hi (inclusive).

          PyTorch analogue: torch.linspace.

          Instances For
            def NN.API.Samples.grid2Rect (xLo xHi yLo yHi : Float) (xCount yCount : ) :

            Rectangular grid over [xLo, xHi] x [yLo, yHi].

            Instances For

              Square grid over [lo, hi] x [lo, hi].

              Instances For

                Compute 2D→1D regression targets for a batched grid.

                Input X has shape (n,2) and the output Y has shape (n,1).

                Instances For

                  Casted version of vec1Float under an arbitrary scalar semantics α.

                  Instances For
                    def NN.API.Samples.vec2 {α : Type} [Context α] (cast : Floatα) (x1 x2 : Float) :

                    Casted version of vec2Float under an arbitrary scalar semantics α.

                    Instances For

                      Image 2D (Synthetic CHW Pixels) #

                      Which axis a synthetic "band" varies along.

                      We use this to build small labeled image datasets without depending on any external image format.

                      Instances For
                        @[implicit_reducible]

                        Human-readable axis name (useful in example titles / sample names).

                        Instances For
                          def NN.API.Samples.Image2D.renderCHWFloat (c h w : ) (pixel : Fin cFin hFin wFloat) :

                          Render a CHW c h w image tensor from a pixel function.

                          The pixel function is indexed by Fin indices that are guaranteed to be in-bounds.

                          Instances For
                            def NN.API.Samples.Image2D.binaryCHWFloat (c h w : ) (on : Fin cFin hFin wBool) (onValue : Float := 1.0) (offValue : Float := 0.0) :

                            Binary image tensor renderer in CHW layout.

                            Pixels satisfying on c row col get onValue; the rest get offValue.

                            Instances For
                              def NN.API.Samples.Image2D.bandCHWFloat (h w : ) (axis : Axis) (offset : ) (thickness : := 2) (onValue : Float := 1.0) (offValue : Float := 0.0) :

                              Render a thick horizontal or vertical band into an h x w image tensor.

                              This is used to create small classification datasets where the label is "horizontal" vs "vertical".

                              Returns a single-channel CHW 1 h w image tensor (channel-first, PyTorch style).

                              Instances For

                                Label metadata for a class of synthetic band images.

                                Instances For
                                  def NN.API.Samples.Image2D.verticalClass (label : := 0) (name : String := "vertical") :

                                  Convenience constructor for a "vertical band" class.

                                  Instances For
                                    def NN.API.Samples.Image2D.horizontalClass (label : := 1) (name : String := "horizontal") :

                                    Convenience constructor for a "horizontal band" class.

                                    Instances For
                                      def NN.API.Samples.Image2D.bandDatasetCHWFloat (h w : ) (classes : List BandClass) (offsets : List ) (thickness : := 2) :

                                      Generate labeled band samples for a list of classes and offsets.

                                      This produces a list of (x, label) pairs where x is a typed CHW 1 h w tensor.

                                      Instances For

                                        Like bandDatasetCHWFloat, but keep a human-readable name for each sample.

                                        Each entry is (name, x, label) where x is a typed CHW 1 h w tensor.

                                        Instances For

                                          Labels and Packing #

                                          One-hot encode a label as a float vector of shape Vec classes.

                                          Instances For
                                            def NN.API.Samples.oneHot {α : Type} [Context α] (cast : Floatα) (classes label : ) :

                                            Casted version of oneHotFloat.

                                            Instances For
                                              def NN.API.Samples.classification {α : Type} [Context α] {σ : Spec.Shape} (cast : Floatα) (classes : ) (xs : List (Spec.Tensor Float σ × )) :

                                              Convert (x, label) pairs into (x, oneHot(label)) pairs.

                                              This is a pure preprocessing step that keeps the data in-memory.

                                              Instances For

                                                Pack (x, y) tensor pairs into TorchLean supervised TList samples.

                                                This is the common sample representation used by the training helpers.

                                                Instances For
                                                  def NN.API.Samples.labeled {α : Type} [Context α] {σ : Spec.Shape} (cast : Floatα) (classes : ) (xs : List (Spec.Tensor Float σ × )) :

                                                  Convert (x, label) pairs into TorchLean TList samples with one-hot targets.

                                                  Instances For

                                                    CHW Parsing #

                                                    Interpret a flat list of floats as an image tensor of shape CHW c h w.

                                                    Fails if xs.length ≠ c*h*w.

                                                    Instances For
                                                      def NN.API.Samples.imageCHW {α : Type} [Context α] (cast : Floatα) (c h w : ) (xs : List Float) :

                                                      Casted version of imageCHWFloat.

                                                      Instances For