Overview
The stepwise
crate is a zero-dependency helper library for writing iterative algorithms.
It supports both numeric techniques—such as gradient descent, fixed-point iteration, Newton–Raphson—and non-numeric stepwise processes, like line-by-line file parsing or tree search.
With stepwise
, you can:
- Use the
Driver
executor in a functional, iterator-like style to configure:- iteration limits, timeouts, convergence criteria,
- logging, and
- terminal progress bars.
- Control early stopping using
metrics
that compute moving averages, absolute/relative differences, invocation counts, or detect stalled solvers. - Use included mini
linear algebra functions
for computing norms and distances. - Collect structured data using
samplers
for ML training, convergence analysis, or plotting. - Benchmark your own solvers on a suite of
problems
, and compare them with the built-inalgorithms
.
Links
driver
|
algorithms
|
metrics
|
linear algebra functions
|
samplers
|
problems
Example 1
use ;
use GradientDescent;
let sphere_gradient = ;
let learning_rate = 0.1;
let initial_guess = vec!;
let algo = new;
let = fixed_iters.solve.expect;
assert_approx_eq!;
Example 2
use Duration;
use *;
let gd = new;
let mut avg_of_norm = with_window;
let = fail_after_iters
.converge_when
.show_progress_bar_after
.on_step
.solve
.expect;
assert_approx_eq!;
assert_eq!;
In Example 2 (above):
- Early stopping is used to terminate the algorithm
- An exponential moving average of gradient's l2-norm is used to test it is near zero.
- A progress bar is also printed in the terminal if algorithm runs for more than 20ms.
- The stepwise
prelude
is used to import both themetrics
and thelinear algebra trait
- A decaying learning rate is applied after each step.
Creating an Algorithm
Callers will typically interact with your algorithm through the Driver
trait.
However, to integrate with stepwise
, you must implement the core Algo
trait for your algorithm.
This involves defining a single method:
step()
: returns a tuple of:- a
ControlFlow
, indicating whether iteration should continue or stop - a
Result
, capturing any error that may occur during a step
- a
This design separates control flow from failure, supporting fallible solvers with clean early-stopping logic.
Note: accessor methods like x()
(for current state) are not part of the trait, but are expected in usable solvers, in order to retrieve the solution after or during iteration.
use ;
Links
Changelog
Motivation
- Related crate:
stochy