pub struct TensorRegularizer { /* private fields */ }Expand description
L1/L2/ElasticNet regularization for tensor weight vectors.
§Examples
use ipfrs_tensorlogic::regularizer::{TensorRegularizer, RegularizerConfig, RegularizerType};
let config = RegularizerConfig {
reg_type: RegularizerType::L1,
lambda: 0.1,
elastic_alpha: 0.5,
};
let mut reg = TensorRegularizer::new(config);
let weights = vec![1.0, -2.0, 3.0];
let penalty = reg.penalty(&weights);
assert!((penalty - 0.6).abs() < 1e-12); // 0.1 * (1 + 2 + 3)Implementations§
Source§impl TensorRegularizer
impl TensorRegularizer
Sourcepub fn new(config: RegularizerConfig) -> Self
pub fn new(config: RegularizerConfig) -> Self
Create a new regularizer from the given configuration.
Sourcepub fn penalty(&mut self, weights: &[f64]) -> f64
pub fn penalty(&mut self, weights: &[f64]) -> f64
Compute the regularization penalty for the given weight vector.
Sourcepub fn gradient(&mut self, weights: &[f64]) -> Vec<f64>
pub fn gradient(&mut self, weights: &[f64]) -> Vec<f64>
Compute the regularization gradient for the given weight vector.
Sourcepub fn l1_penalty(&self, weights: &[f64]) -> f64
pub fn l1_penalty(&self, weights: &[f64]) -> f64
L1 penalty: lambda * sum(|w_i|).
Sourcepub fn l1_gradient(&self, weights: &[f64]) -> Vec<f64>
pub fn l1_gradient(&self, weights: &[f64]) -> Vec<f64>
L1 gradient: lambda * sign(w_i).
The sub-gradient at zero is defined as 0.0.
Sourcepub fn l2_penalty(&self, weights: &[f64]) -> f64
pub fn l2_penalty(&self, weights: &[f64]) -> f64
L2 penalty: lambda * sum(w_i^2).
Sourcepub fn l2_gradient(&self, weights: &[f64]) -> Vec<f64>
pub fn l2_gradient(&self, weights: &[f64]) -> Vec<f64>
L2 gradient: 2 * lambda * w_i.
Sourcepub fn elastic_penalty(&self, weights: &[f64]) -> f64
pub fn elastic_penalty(&self, weights: &[f64]) -> f64
ElasticNet penalty: alpha * L1(w) + (1 - alpha) * L2(w).
Sourcepub fn elastic_gradient(&self, weights: &[f64]) -> Vec<f64>
pub fn elastic_gradient(&self, weights: &[f64]) -> Vec<f64>
ElasticNet gradient: alpha * L1_grad(w) + (1 - alpha) * L2_grad(w).
Sourcepub fn stats(&self) -> RegularizerStats
pub fn stats(&self) -> RegularizerStats
Return runtime statistics.
Auto Trait Implementations§
impl Freeze for TensorRegularizer
impl RefUnwindSafe for TensorRegularizer
impl Send for TensorRegularizer
impl Sync for TensorRegularizer
impl Unpin for TensorRegularizer
impl UnsafeUnpin for TensorRegularizer
impl UnwindSafe for TensorRegularizer
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