Skip to main content

hpt_traits/ops/
regularization.rs

1use hpt_common::error::base::TensorError;
2use hpt_types::{into_scalar::Cast, type_promote::NormalOut};
3
4/// A trait contains regularization operations
5pub trait RegularizationOps {
6    /// The type of the output tensor
7    type Output;
8    /// The type of the output meta
9    type OutputMeta;
10
11    /// Randomly zeroes some of the elements of the input tensor with probability rate using samples from a Bernoulli distribution. Each element is zeroed independently.
12    ///
13    /// ## Parameters:
14    /// `rate`: Probability of an element to be zeroed. The value must be between 0 and 1.
15    ///
16    /// ## Example:
17    /// ```rust
18    /// let x = Tensor::<f32>::ones(&[3, 4])?;
19    /// let dropped = x.dropout(0.5)?;
20    /// ```
21    fn dropout(&self, rate: f64) -> Result<Self::Output, TensorError>
22    where
23        f64: Cast<Self::OutputMeta>,
24        bool: Cast<Self::OutputMeta>,
25        Self::OutputMeta: NormalOut<bool, Output = Self::OutputMeta>;
26
27    /// Applies the shrinkage function to the input tensor. The shrinkage function is a soft thresholding operator commonly used in signal processing and optimization algorithms, defined as:
28    /// `sign(x - bias) * max(abs(x - bias) - lambda, 0)`
29    ///
30    /// ## Parameters:
31    /// `bias`: Bias value to subtract from each element before applying shrinkage.
32    ///
33    /// `lambda`: Threshold parameter controlling the amount of shrinkage.
34    ///
35    /// ## Example:
36    /// ```rust
37    /// let x = Tensor::<f32>::new(&[[-3.0, -1.0, 0.0, 2.0, 5.0]]);
38    /// let result = x.shrinkage(0.0, 1.5)?; // [[-1.5, 0.0, 0.0, 0.5, 3.5]]
39    /// ```
40    fn shrinkage(
41        &self,
42        bias: Self::OutputMeta,
43        lambda: Self::OutputMeta,
44    ) -> Result<Self::Output, TensorError>;
45}