pub struct WeightInitializer { /* private fields */ }Expand description
Weight initializer that generates tensors according to a chosen strategy.
Implementations§
Source§impl WeightInitializer
impl WeightInitializer
Sourcepub fn new(config: WeightInitConfig) -> Self
pub fn new(config: WeightInitConfig) -> Self
Create a new initializer from the given configuration.
Sourcepub fn initialize(&mut self, shape: &TensorShape) -> Vec<f64>
pub fn initialize(&mut self, shape: &TensorShape) -> Vec<f64>
Generate weights for the given tensor shape.
Sourcepub fn compute_fan(shape: &TensorShape) -> (usize, usize)
pub fn compute_fan(shape: &TensorShape) -> (usize, usize)
Compute (fan_in, fan_out) from a tensor shape.
- 1D:
(dims[0], dims[0]) - 2D:
(dims[1], dims[0]) - 3D+:
(dims[1] * receptive, dims[0] * receptive)wherereceptive = product of dims[2..]
Sourcepub fn xavier_bound(fan_in: usize, fan_out: usize, gain: f64) -> f64
pub fn xavier_bound(fan_in: usize, fan_out: usize, gain: f64) -> f64
Compute the Xavier/Glorot bound: gain * sqrt(6 / (fan_in + fan_out)).
Sourcepub fn he_bound(fan: usize, gain: f64) -> f64
pub fn he_bound(fan: usize, gain: f64) -> f64
Compute the He/Kaiming bound: gain * sqrt(3 / fan).
Sourcepub fn next_uniform(&mut self, low: f64, high: f64) -> f64
pub fn next_uniform(&mut self, low: f64, high: f64) -> f64
Generate a uniform random value in [low, high) using xorshift64.
Sourcepub fn next_normal(&mut self, mean: f64, std: f64) -> f64
pub fn next_normal(&mut self, mean: f64, std: f64) -> f64
Generate a normally distributed value via the Box-Muller transform.
Sourcepub fn compute_stats(weights: &[f64]) -> InitStats
pub fn compute_stats(weights: &[f64]) -> InitStats
Compute basic statistics for a slice of weights.
Sourcepub fn reset_seed(&mut self, seed: u64)
pub fn reset_seed(&mut self, seed: u64)
Reset the PRNG seed.
Sourcepub fn initialized_count(&self) -> u64
pub fn initialized_count(&self) -> u64
Return the total number of weights initialised so far.
Auto Trait Implementations§
impl Freeze for WeightInitializer
impl RefUnwindSafe for WeightInitializer
impl Send for WeightInitializer
impl Sync for WeightInitializer
impl Unpin for WeightInitializer
impl UnsafeUnpin for WeightInitializer
impl UnwindSafe for WeightInitializer
Blanket Implementations§
impl<T> Allocation for T
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> 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