pub struct AdversarialTrainer { /* private fields */ }Expand description
Adversarial training wrapper for dual-network (generator/discriminator) setups.
Manages alternating training of two networks with configurable step ratios.
§Example
ⓘ
use axonml::adversarial::AdversarialTrainer;
let mut trainer = AdversarialTrainer::new(gen_optimizer, disc_optimizer);
trainer.set_disc_steps_per_gen(5); // Train D 5x per G step
for batch in data {
let (g_loss, d_loss) = trainer.step(&gen, &disc, &real, &noise, |pred, target| {
bce_loss(pred, target)
});
}Implementations§
Source§impl AdversarialTrainer
impl AdversarialTrainer
Sourcepub fn new(
gen_optimizer: Box<dyn Optimizer>,
disc_optimizer: Box<dyn Optimizer>,
) -> Self
pub fn new( gen_optimizer: Box<dyn Optimizer>, disc_optimizer: Box<dyn Optimizer>, ) -> Self
Creates a new adversarial trainer.
Sourcepub fn set_disc_steps_per_gen(&mut self, steps: usize)
pub fn set_disc_steps_per_gen(&mut self, steps: usize)
Sets how many discriminator steps per generator step.
Sourcepub fn step<G, D, F>(
&mut self,
generator: &G,
discriminator: &D,
real_data: &Variable,
noise: &Variable,
loss_fn: F,
) -> (f32, f32)
pub fn step<G, D, F>( &mut self, generator: &G, discriminator: &D, real_data: &Variable, noise: &Variable, loss_fn: F, ) -> (f32, f32)
Performs one adversarial training step.
§Arguments
generator- Generator modeldiscriminator- Discriminator modelreal_data- Real data batchnoise- Random noise input for generatorloss_fn- Loss function taking (predictions, targets) → scalar loss
§Returns
Tuple of (generator_loss, discriminator_loss)
Sourcepub fn optimizers(&self) -> (&dyn Optimizer, &dyn Optimizer)
pub fn optimizers(&self) -> (&dyn Optimizer, &dyn Optimizer)
Returns references to the optimizers.
Auto Trait Implementations§
impl Freeze for AdversarialTrainer
impl !RefUnwindSafe for AdversarialTrainer
impl !Send for AdversarialTrainer
impl !Sync for AdversarialTrainer
impl Unpin for AdversarialTrainer
impl UnsafeUnpin for AdversarialTrainer
impl !UnwindSafe for AdversarialTrainer
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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