TorchLean API

NN.Data.SampleStream

Finite Sample Streams #

This module provides the lower-level data source used by the trainer API. A SampleStream α records a finite number of samples and computes a sample only when its index is requested. Array- backed data is wrapped without copying, while generated data and tensor slices remain lazy.

Application code normally constructs TorchLean.Trainer.Dataset; this module is useful for manual training loops and data-source implementations that already know their runtime scalar type.

A finite, lazily indexed source of samples.

  • size : Nat

    Number of samples available from the stream.

  • get : Fin self.sizeα

    Compute the sample at an in-bounds index.

Instances For

    Named halves of a stream split.

    • selected : SampleStream α

      Samples before the split point.

    • remaining : SampleStream α

      Samples at and after the split point.

    Instances For

      State and stream produced by deterministic shuffling.

      • nextSeed : Nat

        Seed to use for the next deterministic shuffle.

      • stream : SampleStream α

        Stream in shuffled index order.

      Instances For

        State and named partitions produced by a shuffled split.

        • nextSeed : Nat

          Seed to use for the next deterministic shuffle.

        • selected : SampleStream α

          Samples selected before the split point.

        • remaining : SampleStream α

          Samples selected at and after the split point.

        Instances For
          def TorchLean.Data.SampleStream.fromFunction {α : Type} (size : Nat) (get : Fin sizeα) :

          Construct a finite stream from an index function.

          Instances For

            Wrap an array as a finite stream without copying it.

            Instances For

              Materialize the samples in index order.

              Instances For
                @[simp]

                A stream built from an array reports that array's length.

                Return true exactly when the stream contains no samples.

                Instances For
                  def TorchLean.Data.SampleStream.get? {α : Type} (stream : SampleStream α) (i : Nat) :

                  Safely request a sample by a natural-number index.

                  Instances For
                    def TorchLean.Data.SampleStream.map {α β : Type} (f : αβ) (stream : SampleStream α) :

                    Transform samples when they are requested.

                    Instances For

                      Append two finite streams in index order.

                      Instances For

                        Split a stream into its first n samples and the remaining suffix.

                        Instances For

                          Shuffle the indices deterministically, returning the next seed and a stream with the new order.

                          Only the index permutation is stored; requesting a shuffled sample still evaluates the original stream at that index.

                          Instances For

                            Deterministically shuffle a stream and discard the next pseudo-random seed.

                            Instances For

                              Shuffle a stream once and split the result at n.

                              Instances For
                                def TorchLean.Data.SampleStream.batches {α : Type} (name : String) (batchSize : Nat) (stream : SampleStream α) :

                                Split a nonempty stream into consecutive nonempty batches of size at most batchSize.

                                Instances For
                                  def TorchLean.Data.SampleStream.cycle {α : Type} (stream : SampleStream α) (h : 0 < stream.size) (i : Nat) :
                                  α

                                  Cycle through a nonempty stream indefinitely.

                                  Instances For
                                    def TorchLean.Data.SampleStream.cycleOrError {α : Type} (stream : SampleStream α) (error : String := "empty sample stream") :
                                    Except String (Natα)

                                    Build a cycling sample function, rejecting an empty stream once at construction time.

                                    Instances For

                                      Configuration and deterministic shuffle state for epoch traversal.

                                      • samples : SampleStream α

                                        Samples traversed by each epoch.

                                      • batchSize : Nat

                                        Maximum number of samples in each batch.

                                      • shuffle : Bool

                                        Whether to shuffle before each epoch.

                                      • seed : Nat

                                        Seed threaded through deterministic shuffles.

                                      • dropLast : Bool

                                        Whether to discard a final batch shorter than batchSize.

                                      Instances For
                                        structure TorchLean.Data.Epoch (Loader Batch : Type) :

                                        Batches produced for one epoch together with the loader state for the next epoch.

                                        • nextLoader : Loader

                                          Loader state to use for the next epoch.

                                        • batches : Array Batch

                                          Batches produced by this epoch.

                                        Instances For
                                          def TorchLean.Data.EpochLoader.fromStream {α : Type} (samples : SampleStream α) (batchSize : Nat) (shuffle : Bool := false) (seed : Nat := 0) (dropLast : Bool := false) :

                                          Construct an epoch loader for a finite stream.

                                          Instances For

                                            Produce one epoch of batches and the loader state for the next epoch. Each shuffle applies the next seed to the original source. Keeping that source avoids retaining an additional permutation closure for every completed epoch.

                                            Instances For
                                              def TorchLean.Data.EpochLoader.mapNextEpoch {α β : Type} (name : String) (loader : EpochLoader α) (collate : Array αExcept String β) :

                                              Produce one epoch and map each raw batch through collate.

                                              Instances For