pub struct ActivationFunction { /* private fields */ }Expand description
Neural network activation layer with forward pass, derivative, in-place application, and call statistics.
Implementations§
Source§impl ActivationFunction
impl ActivationFunction
Sourcepub fn new(config: ActivationConfig) -> Self
pub fn new(config: ActivationConfig) -> Self
Create a new activation layer with the given configuration.
Sourcepub fn forward(&mut self, input: &[f64]) -> Vec<f64>
pub fn forward(&mut self, input: &[f64]) -> Vec<f64>
Apply the configured activation to every element of input and return
a newly allocated Vec<f64>.
For Softmax the operation is applied across the entire slice.
Sourcepub fn derivative(&self, output: &[f64]) -> Vec<f64>
pub fn derivative(&self, output: &[f64]) -> Vec<f64>
Compute the element-wise derivative of the activation with respect to the pre-activation input.
§Argument semantics
The output slice is interpreted differently per activation:
- Sigmoid —
outputshould be the post-activation valueσ(x). The derivative isσ(x) * (1 − σ(x))which avoids re-computing the expensive exponential. - Tanh —
outputshould be the post-activation valuetanh(x). The derivative is1 − tanh(x)². - All others —
outputis treated as the pre-activation valuexand the derivative is computed from first principles.
Sourcepub fn apply_inplace(&mut self, data: &mut [f64])
pub fn apply_inplace(&mut self, data: &mut [f64])
Apply the configured activation in place, mutating data.
Statistics are updated identically to forward.
Sourcepub fn stats(&self) -> &ActivationStats
pub fn stats(&self) -> &ActivationStats
Return a reference to the accumulated statistics for this instance.
Sourcepub fn config(&self) -> &ActivationConfig
pub fn config(&self) -> &ActivationConfig
Return a reference to the current configuration.
Sourcepub fn leaky_relu(x: f64, slope: f64) -> f64
pub fn leaky_relu(x: f64, slope: f64) -> f64
Leaky ReLU: x if x >= 0, else slope * x.
Sourcepub fn elu(x: f64, alpha: f64) -> f64
pub fn elu(x: f64, alpha: f64) -> f64
Exponential Linear Unit: x if x >= 0, else alpha * (exp(x) - 1).
Sourcepub fn tanh_activation(x: f64) -> f64
pub fn tanh_activation(x: f64) -> f64
Hyperbolic tangent activation.
Sourcepub fn softmax(input: &[f64]) -> Vec<f64>
pub fn softmax(input: &[f64]) -> Vec<f64>
Numerically stable softmax over the entire input slice.
Subtracts max(input) before exponentiating to prevent overflow.
Sourcepub fn gelu(x: f64) -> f64
pub fn gelu(x: f64) -> f64
GELU activation (tanh approximation).
0.5 * x * (1 + tanh(sqrt(2/π) * (x + 0.044715 * x³)))
Sourcepub fn mish(x: f64) -> f64
pub fn mish(x: f64) -> f64
Mish: x * tanh(softplus(x)) where softplus(x) = ln(1 + exp(x)).
Uses the numerically stable form ln(1 + exp(x)):
for large positive x, softplus(x) ≈ x; for large negative x,
softplus(x) ≈ exp(x).
Sourcepub fn hard_swish(x: f64) -> f64
pub fn hard_swish(x: f64) -> f64
Hard Swish: x * relu6(x + 3) / 6 where relu6(t) = min(max(t, 0), 6).
Auto Trait Implementations§
impl Freeze for ActivationFunction
impl RefUnwindSafe for ActivationFunction
impl Send for ActivationFunction
impl Sync for ActivationFunction
impl Unpin for ActivationFunction
impl UnsafeUnpin for ActivationFunction
impl UnwindSafe for ActivationFunction
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
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>
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 more