def soft_sparsity_prox(tensor, threshold): """ Projects the input tensor on the set of tensors with l1 norm smaller than threshold, using Soft Thresholding. Parameters ---------- tensor : ndarray threshold : Returns ------- ndarray References ---------- .. [1]: Schenker, C., Cohen, J. E., & Acar, E. (2020). A Flexible Optimization Framework for Regularized Matrix-Tensor Factorizations with Linear Couplings. IEEE Journal of Selected Topics in Signal Processing. Notes ----- .. math:: \\begin{equation} \\lambda: prox_\\lambda (||tensor||_1) \\leq parameter \\end{equation} """ return simplex_prox(tl.abs(tensor), threshold) * tl.sign(tensor)
def soft_thresholding(tensor, threshold): """Soft-thresholding operator sign(tensor) * max[abs(tensor) - threshold, 0] Parameters ---------- tensor : ndarray threshold : float or ndarray with shape tensor.shape * If float the threshold is applied to the whole tensor * If ndarray, one threshold is applied per elements, 0 values are ignored Returns ------- ndarray thresholded tensor on which the operator has been applied Examples -------- Basic shrinkage >>> import tensorly.backend as T >>> from tensorly.tenalg.proximal import soft_thresholding >>> tensor = tl.tensor([[1, -2, 1.5], [-4, 3, -0.5]]) >>> soft_thresholding(tensor, 1.1) array([[ 0. , -0.9, 0.4], [-2.9, 1.9, 0. ]]) Example with missing values >>> mask = tl.tensor([[0, 0, 1], [1, 0, 1]]) >>> soft_thresholding(tensor, mask*1.1) array([[ 1. , -2. , 0.4], [-2.9, 3. , 0. ]]) See also -------- inplace_soft_thresholding : Inplace version of the soft-thresholding operator svd_thresholding : SVD-thresholding operator """ return tl.sign(tensor) * tl.clip(tl.abs(tensor) - threshold, a_min=0)