Macro with_params

Source
macro_rules! with_params {
    (
        set $($key:ident).+ = $val:expr;

        $($body:tt)*
    ) => { ... };
    (
        params $ps:expr;
        set $($key:ident).+ = $val:expr;

        $($body:tt)*
    ) => { ... };
    (
        params $ps:expr;
        params $nested:expr;

        $($body:tt)*
    ) => { ... };
    (
        get $name:ident = $($key:ident).+ or $default:expr;

        $($body:tt)*
    ) => { ... };
    (
        $(#[doc = $doc:expr])*
        get $name:ident = $($key:ident).+ or $default:expr;

        $($body:tt)*
    ) => { ... };
    (
        params $ps:expr;
        get $name:ident = $($key:ident).+ or $default:expr;

        $($body:tt)*
    ) => { ... };
    (
        params $ps:expr;

        $($body:tt)*
    ) => { ... };
    ($($body:tt)*) => { ... };
}
Expand description

Define or use hyperparameters in a code block.

Hyperparameters are named parameters whose values control the learning process of an ML model or the behaviors of an underlying machine learning system.

Hyperparameter is designed as user-friendly as global variables but overcomes two major drawbacks of global variables: non-thread safety and global scope.

ยงA quick example

use hyperparameter::*;

with_params! {   // with_params begins a new parameter scope
    set a.b = 1; // set the value of named parameter `a.b`
    set a.b.c = 2.0; // `a.b.c` is another parameter.

    assert_eq!(1, get_param!(a.b, 0));

    with_params! {   // start a new parameter scope that inherits parameters from the previous scope
        set a.b = 2; // override parameter `a.b`

        let a_b = get_param!(a.b, 0); // read parameter `a.b`, return the default value (0) if not defined
        assert_eq!(2, a_b);
    }
}