pub struct ParallelTrainer<M, O, B: Backend> {
pub model: M,
pub optimizer: O,
/* private fields */
}Expand description
High-level training loop with gradient accumulation.
Splits a large effective batch into accumulation_steps micro-batches,
accumulates gradients across all of them, then performs a single
optimizer step. This simulates a larger batch size without requiring
more memory.
§Example
ⓘ
let model = Sequential::new(vec![...]);
let optimizer = Adam::new(model.parameters(), 1e-3);
let mut trainer = ParallelTrainer::new(model, optimizer, 4);
// Each call accumulates 1/4 of the gradient; every 4th call steps.
for (i, (x, y)) in data.iter().enumerate() {
if let Some(loss) = trainer.accumulate_step(&x, &y, mse_loss)? {
println!("step {}: loss = {:.4}", i, loss);
}
}Fields§
§model: MThe model.
optimizer: OThe optimizer.
Implementations§
Source§impl<M, O, B> ParallelTrainer<M, O, B>
impl<M, O, B> ParallelTrainer<M, O, B>
Sourcepub fn new(model: M, optimizer: O, accumulation_steps: usize) -> Self
pub fn new(model: M, optimizer: O, accumulation_steps: usize) -> Self
Create a new ParallelTrainer.
accumulation_steps: number of micro-batches before each optimizer step.
Auto Trait Implementations§
impl<M, O, B> Freeze for ParallelTrainer<M, O, B>
impl<M, O, B> RefUnwindSafe for ParallelTrainer<M, O, B>
impl<M, O, B> Send for ParallelTrainer<M, O, B>
impl<M, O, B> Sync for ParallelTrainer<M, O, B>
impl<M, O, B> Unpin for ParallelTrainer<M, O, B>
impl<M, O, B> UnwindSafe for ParallelTrainer<M, O, B>
Blanket Implementations§
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
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 more