Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • ba8c099
  • /
  • tensorly
  • /
  • tenalg
  • /
  • proximal.py
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:3d76adf26b3effe56f12fd2acc718595c10da095
directory badge
swh:1:dir:516ce30dec932f1afc8fb82197b995a3ed02f149

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
proximal.py
import numpy as np
from scipy.linalg import svd

# Author: Jean Kossaifi

# License: BSD 3 clause



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 theshold is applied per elements, 0 values are ignored

    Returns
    -------
    ndarray
        thresholded tensor on which the operator has been applied

    Examples
    --------
    Basic shrinkage

    >>> import numpy as np
    >>> from tensorly.tenalg.proximal import soft_thresholding
    >>> tensor = np.array([[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 = np.array([[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
    """
    signs = np.sign(tensor)
    values = (signs*tensor - threshold)
    return np.where(values > 0, signs*values, 0)


def inplace_soft_thresholding(tensor, threshold):
    """Inplace version of the shrinkage operator

    Parameters
    ----------
    tensor : ndarray
    threshold : float

    Returns
    -------
    ndarray
        tensor on which the operator has been applied inplace

    See also
    --------
    soft_thresholding : less memory-efficient but fast soft-thresholding operator
    svd_thresholding : SVD-thresholding operator
    """
    index_shrink = ((tensor <= threshold) & (tensor >= -threshold))
    index_more = (tensor > threshold)
    index_less = (tensor < -threshold)
    tensor[index_shrink] = 0
    tensor[index_more] -= threshold
    tensor[index_less] += threshold
    return tensor


def svd_thresholding(matrix, threshold):
    """Singular value thresholding operator

    Parameters
    ----------
    matrix : ndarray
    threshold : float

    Returns
    -------
    ndarray
        matrix on which the operator has been applied

    See also
    --------
    procrustes : procrustes operator
    """
    U, s, V = svd(matrix, full_matrices=False)
    return np.dot(U, soft_thresholding(s, threshold)[:, None]*V)


def procrustes(matrix):
    """Procrustes operator

    Parameters
    ----------
    matrix : ndarray

    Returns
    -------
    ndarray
        matrix on which the Procrustes operator has been applied
        has the same shape as the original tensor


    See also
    --------
    svd_thresholding : SVD-thresholding operator
    """
    U, _, V = svd(matrix, full_matrices=False)
    return np.dot(U, V)

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API