Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Avhz RustQuant ActivationFunctions

From Leeroopedia
Revision as of 14:30, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Avhz_RustQuant_ActivationFunctions.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Machine_Learning, Neural_Networks, Quantitative_Finance
Last Updated 2026-02-07 19:00 GMT

Overview

Concrete tool for activation functions used in machine learning models provided by the RustQuant library.

Description

The ActivationFunction trait defines a suite of common activation functions used in neural networks and other ML models. The trait is implemented for three types, enabling use with scalar values, vectors, and automatic differentiation variables:

  • f64 -- Scalar floating-point evaluation.
  • DVector<f64> -- Element-wise application over nalgebra dynamic vectors.
  • Variable<'_> -- Automatic differentiation compatible via the RustQuant_autodiff crate, enabling gradient computation through activation functions.

The following activation functions are provided:

  • sigmoid / logistic -- 1 / (1 + exp(-x)). Both names are provided for convenience; they compute the same function.
  • identity -- Returns the input unchanged.
  • relu -- Rectified Linear Unit: max(0, x). Implemented as (x + |x|) / 2 for the autodiff variant.
  • gelu -- Gaussian Error Linear Unit: 0.5 * x * (1 + erf(x / sqrt(2))).
  • tanh -- Hyperbolic tangent.
  • softplus -- ln(1 + exp(x)). A smooth approximation to ReLU.
  • gaussian -- exp(-x^2). A radial basis function.

Usage

Use these activation functions as building blocks in neural network layers, logistic regression models, or any ML pipeline that requires non-linear transformations. The Variable implementation enables automatic differentiation through activation functions, which is essential for gradient-based optimization (backpropagation).

Code Reference

Source Location

Signature

pub trait ActivationFunction {
    fn sigmoid(&self) -> Self;
    fn identity(&self) -> Self;
    fn logistic(&self) -> Self;
    fn relu(&self) -> Self;
    fn gelu(&self) -> Self;
    fn tanh(&self) -> Self;
    fn softplus(&self) -> Self;
    fn gaussian(&self) -> Self;
}

impl ActivationFunction for f64 { /* ... */ }
impl ActivationFunction for DVector<f64> { /* ... */ }
impl ActivationFunction for Variable<'_> { /* ... */ }

Import

use RustQuant::ml::ActivationFunction;

I/O Contract

Inputs

Name Type Required Description
self f64, DVector<f64>, or Variable<'_> Yes The input value, vector, or autodiff variable to which the activation function is applied.

Outputs

Name Type Description
result Self The transformed value after applying the activation function. Same type as input.

Usage Examples

use RustQuant::ml::ActivationFunction;
use nalgebra::DVector;

// Scalar usage
let x: f64 = 0.5;
let sig = x.sigmoid();       // 1 / (1 + exp(-0.5)) ~ 0.6225
let r = x.relu();            // 0.5
let sp = x.softplus();       // ln(1 + exp(0.5)) ~ 0.9741

// Vector usage
let v = DVector::from_vec(vec![-1.0, 0.0, 1.0, 2.0]);
let activated = v.relu();    // [0.0, 0.0, 1.0, 2.0]
let logistic = v.logistic(); // element-wise sigmoid

// Autodiff usage (for gradient computation)
use RustQuant::autodiff::Graph;
let graph = Graph::new();
let var = graph.var(0.5);
let output = var.sigmoid();
// Compute gradient via backpropagation

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment