Implementation:Avhz RustQuant ActivationFunctions
| 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 theRustQuant_autodiffcrate, 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|) / 2for 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
- Repository: RustQuant
- File: crates/RustQuant_ml/src/activations.rs
- Lines: 1-174
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