Skip to main content

Optimizer

Trait Optimizer 

Source
pub trait Optimizer {
    // Required methods
    fn step(&mut self);
    fn zero_grad(&mut self);
    fn lr(&self) -> f32;
    fn set_lr(&mut self, lr: f32);
}
Expand description

Common trait for all optimizers.

Required Methods§

Source

fn step(&mut self)

Marks the optimizer as having stepped. This does NOT update parameters.

Gradients live in a global autograd graph keyed by tensor id, so an optimizer (which holds only parameter ids + state) cannot reach the parameter tensors from here. To actually apply updates, call step_with_params(&mut params) (e.g. sgd.step_with_params(&mut model.parameters_mut())). Canonical training loop: clear_graph(); let loss = ...; loss.backward(); opt.step_with_params(&mut model.parameters_mut());

Source

fn zero_grad(&mut self)

Zero all parameter gradients.

Source

fn lr(&self) -> f32

Get current learning rate.

Source

fn set_lr(&mut self, lr: f32)

Set learning rate (for schedulers).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§