Skip to main content

ActivationFunction

Struct ActivationFunction 

Source
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

Source

pub fn new(config: ActivationConfig) -> Self

Create a new activation layer with the given configuration.

Source

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.

Source

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:

  • Sigmoidoutput should be the post-activation value σ(x). The derivative is σ(x) * (1 − σ(x)) which avoids re-computing the expensive exponential.
  • Tanhoutput should be the post-activation value tanh(x). The derivative is 1 − tanh(x)².
  • All othersoutput is treated as the pre-activation value x and the derivative is computed from first principles.
Source

pub fn apply_inplace(&mut self, data: &mut [f64])

Apply the configured activation in place, mutating data.

Statistics are updated identically to forward.

Source

pub fn stats(&self) -> &ActivationStats

Return a reference to the accumulated statistics for this instance.

Source

pub fn config(&self) -> &ActivationConfig

Return a reference to the current configuration.

Source

pub fn relu(x: f64) -> f64

Rectified Linear Unit: max(0, x).

Source

pub fn leaky_relu(x: f64, slope: f64) -> f64

Leaky ReLU: x if x >= 0, else slope * x.

Source

pub fn elu(x: f64, alpha: f64) -> f64

Exponential Linear Unit: x if x >= 0, else alpha * (exp(x) - 1).

Source

pub fn sigmoid(x: f64) -> f64

Logistic sigmoid: 1 / (1 + exp(-x)).

Source

pub fn tanh_activation(x: f64) -> f64

Hyperbolic tangent activation.

Source

pub fn softmax(input: &[f64]) -> Vec<f64>

Numerically stable softmax over the entire input slice.

Subtracts max(input) before exponentiating to prevent overflow.

Source

pub fn gelu(x: f64) -> f64

GELU activation (tanh approximation).

0.5 * x * (1 + tanh(sqrt(2/π) * (x + 0.044715 * x³)))

Source

pub fn swish(x: f64) -> f64

Swish / SiLU: x * sigmoid(x).

Source

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).

Source

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§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more