pub struct AdamW { /* private fields */ }Expand description
AdamW, as MLX’s optimizers.AdamW: Adam with decoupled weight decay (MLX’s default
0.01).
Implementations§
Source§impl AdamW
impl AdamW
Sourcepub fn new(learning_rate: impl Into<LearningRate>) -> Self
pub fn new(learning_rate: impl Into<LearningRate>) -> Self
AdamW with MLX’s defaults: betas (0.9, 0.999), eps = 1e-8, weight decay 0.01.
Sourcepub fn weight_decay(self, weight_decay: f64) -> Self
pub fn weight_decay(self, weight_decay: f64) -> Self
Sets the decoupled weight decay.
Sourcepub fn bias_correction(self) -> Self
pub fn bias_correction(self) -> Self
Enables bias correction.
Trait Implementations§
Source§impl Optimizer for AdamW
impl Optimizer for AdamW
Source§fn apply(
&mut self,
params: &[(&str, &Array)],
grads: &[&Array],
) -> Result<Vec<Array>>
fn apply( &mut self, params: &[(&str, &Array)], grads: &[&Array], ) -> Result<Vec<Array>>
One step over named parameters (the path keys each parameter’s state) and their
gradients: the updated parameters, in order. Increments the step once.
Source§fn init_params(&mut self, params: &[(&str, &Array)])
fn init_params(&mut self, params: &[(&str, &Array)])
Creates zero state for
params that have none (paths with state are kept).Source§fn visit_state(&self, v: &mut Visit<'_>)
fn visit_state(&self, v: &mut Visit<'_>)
Visits the state (
step, learning_rate, slots) and the hyperparameters.Source§fn visit_state_mut(&mut self, v: &mut VisitMut<'_>)
fn visit_state_mut(&mut self, v: &mut VisitMut<'_>)
Visits the state mutably.
Source§fn learning_rate(&self) -> Array
fn learning_rate(&self) -> Array
The learning rate: the 0-D float32 array in the state.
Source§fn set_learning_rate(&mut self, lr: f32)
fn set_learning_rate(&mut self, lr: f32)
Sets the learning rate array (in a compiled step it is an input: no retrace).
Source§impl Tree for AdamWwhere
Self: Clone,
impl Tree for AdamWwhere
Self: Clone,
Source§fn visit_mut(&mut self, __v: &mut VisitMut<'_>)
fn visit_mut(&mut self, __v: &mut VisitMut<'_>)
Reports every leaf (and child tree and mode flag) to
v mutably, in the order of
Leaves::visit.Source§fn map(&self, f: impl FnMut(&str, &Array) -> Array) -> Self
fn map(&self, f: impl FnMut(&str, &Array) -> Array) -> Self
The tree with every leaf replaced by
f(path, leaf) (same structure; parameters keep
their frozen flag).Source§fn flatten(&self) -> Vec<(String, Array)>
fn flatten(&self) -> Vec<(String, Array)>
The leaves with their paths, in order (MLX’s
tree_flatten).Source§fn unflatten(
&self,
leaves: impl IntoIterator<Item = Array>,
) -> Result<Self, Error>
fn unflatten( &self, leaves: impl IntoIterator<Item = Array>, ) -> Result<Self, Error>
A tree of this structure (
self is the template) with leaves in order: exactly
Tree::len of them, else an error naming both counts.Source§fn load_flat(
&mut self,
named: impl IntoIterator<Item = (String, Array)>,
mode: LoadMode,
) -> Result<(), Error>
fn load_flat( &mut self, named: impl IntoIterator<Item = (String, Array)>, mode: LoadMode, ) -> Result<(), Error>
Replaces leaves by path.
LoadMode::Strict requires exactly the tree’s paths with
matching shapes; LoadMode::Partial assigns the matching names and ignores
unknown ones (shapes must still match). Loaded dtypes are kept. On any problem the
error lists every one (missing, unknown and mismatched names, sorted) and the tree is
unchanged. Duplicate paths (in the tree or in named) are an error.Auto Trait Implementations§
impl !RefUnwindSafe for AdamW
impl !UnwindSafe for AdamW
impl Freeze for AdamW
impl Send for AdamW
impl Sync for AdamW
impl Unpin for AdamW
impl UnsafeUnpin for AdamW
Blanket Implementations§
impl<T> AutoreleaseSafe for Twhere
T: ?Sized,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoResult for Twhere
T: Tree,
impl<T> IntoResult for Twhere
T: Tree,
Source§impl<T> Module for Twhere
T: Tree,
impl<T> Module for Twhere
T: Tree,
Source§fn parameters(&self) -> Vec<(String, Array)>
fn parameters(&self) -> Vec<(String, Array)>
Every leaf (parameters, buffers and plain arrays) with its path, in order.
Source§fn trainable_parameters(&self) -> Vec<(String, Array)>
fn trainable_parameters(&self) -> Vec<(String, Array)>
The trainable leaves with their paths, in order.
Source§fn num_params(&self) -> usize
fn num_params(&self) -> usize
The number of elements over every leaf (MLX’s
tree_flatten total).Source§fn num_trainable(&self) -> usize
fn num_trainable(&self) -> usize
The number of elements over the trainable leaves.
Source§fn summary(&self) -> Summary
fn summary(&self) -> Summary
A table of the leaves (path, shape, dtype, trainable, bytes) with totals.
Source§fn display(&self) -> ModuleDisplay<'_, Self>
fn display(&self) -> ModuleDisplay<'_, Self>
The nested modules with their parameter counts, for printing:
println!("{}", model.display()).Source§fn freeze(&mut self, filter: impl Fn(&str) -> bool) -> Result<usize>
fn freeze(&mut self, filter: impl Fn(&str) -> bool) -> Result<usize>
Freezes the parameters whose path matches
filter, returning how many changed.
A matching plain Array leaf cannot be frozen (only Params can): that is an
error naming the paths, and nothing changes. Buffers are never trainable anyway.Source§fn unfreeze(&mut self, filter: impl Fn(&str) -> bool) -> Result<usize>
fn unfreeze(&mut self, filter: impl Fn(&str) -> bool) -> Result<usize>
Unfreezes the parameters whose path matches
filter, returning how many changed.
Buffers (quantized weights) and integer parameters stay non-trainable.Source§fn train(&mut self, on: bool)
fn train(&mut self, on: bool)
Sets every mode flag of the tree: training (
true, the default) or evaluation
(false, where dropout is the identity), as MLX’s Module.train(mode). (There is
no eval(): it would read like x.eval(), which evaluates.)Source§fn training(&self) -> bool
fn training(&self) -> bool
Whether the tree is in training mode (its first mode flag;
true without any).Source§fn load_weights(&mut self, src: impl WeightSource) -> Result<()>
fn load_weights(&mut self, src: impl WeightSource) -> Result<()>
Replaces leaves by name, strictly (MLX’s
load_weights(strict=True)): every leaf
must be given with its shape and nothing else (loaded dtypes are kept). On any
problem the error lists every missing, unknown and mismatched name, and the model
is unchanged. src is a safetensors path or named arrays.Source§fn load_weights_partial(&mut self, src: impl WeightSource) -> Result<()>
fn load_weights_partial(&mut self, src: impl WeightSource) -> Result<()>
Replaces the leaves given by name (MLX’s
load_weights(strict=False)): unknown
names are ignored, missing leaves keep their values, shapes must match.Source§fn save_weights(&self, path: impl AsRef<Path>) -> Result<()>
fn save_weights(&self, path: impl AsRef<Path>) -> Result<()>
Writes every leaf as safetensors with MLX’s names and dtypes (MLX’s
save_weights), evaluating them. Unloaded placeholders are an error naming them.Source§fn save_trainable(&self, path: impl AsRef<Path>) -> Result<()>
fn save_trainable(&self, path: impl AsRef<Path>) -> Result<()>
Writes only the trainable leaves (an adapter’s weights, say).
Source§fn map_modules<T: Tree>(
&mut self,
filter: impl Fn(&str) -> bool,
f: impl FnMut(&str, &mut T) -> Result<()>,
) -> Result<usize>
fn map_modules<T: Tree>( &mut self, filter: impl Fn(&str) -> bool, f: impl FnMut(&str, &mut T) -> Result<()>, ) -> Result<usize>
Calls
f(path, submodule) on every submodule of type T whose path matches
filter (f may replace it, as when applying LoRA), returning how many. It stops at
the first error from f; submodules changed before it keep their change. Matching
submodules are not searched for further matches.