pub struct Dropout<A: Float + Debug> { /* private fields */ }Expand description
Dropout regularization for gradients
This type implements the Regularizer trait, so it operates on the
gradient array handed to Regularizer::apply, not on layer activations.
While in training mode it zeroes each gradient entry independently with
probability rate and rescales the survivors by 1 / (1 - rate) (inverted
dropout), which keeps the expected gradient unchanged. In evaluation mode —
or when rate is zero — gradients pass through untouched.
Applying the same idea to activations requires a forward/backward pass that
this crate’s optimizer-side Regularizer interface does not model; use
crate::regularizers::SpatialDropout or a neural-network layer for that.
A fresh mask is drawn on every Regularizer::apply call — masks are never
cached or reused across calls, so consecutive calls with identical gradients
generally produce different results.
§Examples
use scirs2_core::ndarray::Array1;
use optirs_core::regularizers::Dropout;
use scirs2_core::random::SeedableRng;
use scirs2_core::random::rngs::SmallRng;
// Create a dropout regularizer with 0.5 dropout rate
let seed = [0u8; 32];
let mut rng = SmallRng::from_seed(seed);
let mut dropout = Dropout::new(0.5f64, &mut rng);
// Set to training mode
dropout.train();
// Check the dropout rate
assert_eq!(dropout.rate(), 0.5);
// Set to evaluation mode
dropout.eval();
assert!(!dropout.is_training());Implementations§
Source§impl<A: Float + Debug + Send + Sync> Dropout<A>
impl<A: Float + Debug + Send + Sync> Dropout<A>
Sourcepub fn new<R: Rng>(rate: A, rng: &mut R) -> Self
pub fn new<R: Rng>(rate: A, rng: &mut R) -> Self
Create a new dropout regularizer
§Arguments
rate- Dropout rate (0.0 to 1.0, fraction of entries that are dropped)rng- Random number generator used to seed this regularizer’s own RNG
Sourcepub fn set_rate(&mut self, rate: A) -> &mut Self
pub fn set_rate(&mut self, rate: A) -> &mut Self
Set the dropout rate
§Arguments
rate- Dropout rate (0.0 to 1.0, fraction of entries that are dropped)
Sourcepub fn is_training(&self) -> bool
pub fn is_training(&self) -> bool
Get the training mode
Trait Implementations§
Source§impl<A, D> Regularizer<A, D> for Dropout<A>
impl<A, D> Regularizer<A, D> for Dropout<A>
Source§fn apply(&self, _params: &Array<A, D>, gradients: &mut Array<A, D>) -> Result<A>
fn apply(&self, _params: &Array<A, D>, gradients: &mut Array<A, D>) -> Result<A>
Mask the gradients in place with a freshly drawn dropout mask.
params is ignored: this regularizer perturbs the gradient signal, not
the parameters. Always returns a zero penalty because dropout adds no
term to the loss.
Auto Trait Implementations§
impl<A> !Freeze for Dropout<A>
impl<A> !RefUnwindSafe for Dropout<A>
impl<A> !Sync for Dropout<A>
impl<A> Send for Dropout<A>where
A: Send,
impl<A> Unpin for Dropout<A>where
A: Unpin,
impl<A> UnsafeUnpin for Dropout<A>where
A: UnsafeUnpin,
impl<A> UnwindSafe for Dropout<A>where
A: UnwindSafe,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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> ⓘ
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> ⓘ
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> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.