Skip to content

math

sorix.utils.math

abs

abs(X)

Computes the absolute value of each element in input.

Source code in sorix/utils/math.py
def abs(X):
    """Computes the absolute value of each element in input.
    """
    if isinstance(X, Tensor):
        xp = get_xp(X)
        if not is_grad_enabled() or not X.requires_grad:
            return Tensor(xp.abs(X.data), device=X.device, requires_grad=False)

        out = Tensor(xp.abs(X.data), (X,), 'abs', device=X.device, requires_grad=True)

        def _backward():
            if out.grad is None:
                return
            if X.requires_grad:
                g = out.grad.data if isinstance(out.grad, Tensor) else out.grad
                X._accumulate_grad(g * xp.sign(X.data))

        out._backward = _backward
        return out
    else:
        xp = cp if (cp is not None and isinstance(X, cp.ndarray)) else np
        return xp.abs(X)

sign

sign(X)

Returns a new tensor with the signs of the elements of input.

Source code in sorix/utils/math.py
def sign(X):
    """Returns a new tensor with the signs of the elements of input.
    """
    if isinstance(X, Tensor):
        xp = get_xp(X)
        return Tensor(xp.sign(X.data), device=X.device, requires_grad=False)
    else:
        xp = cp if (cp is not None and isinstance(X, cp.ndarray)) else np
        return xp.sign(X)

round

round(X)

Rounds elements of input to the nearest integer.

Source code in sorix/utils/math.py
def round(X):
    """Rounds elements of input to the nearest integer.
    """
    if isinstance(X, Tensor):
        xp = get_xp(X)
        return Tensor(xp.round(X.data), device=X.device, requires_grad=False)
    else:
        xp = cp if (cp is not None and isinstance(X, cp.ndarray)) else np
        return xp.round(X)

floor

floor(X)

Returns a new tensor with the floor of the elements of input.

Source code in sorix/utils/math.py
def floor(X):
    """Returns a new tensor with the floor of the elements of input.
    """
    if isinstance(X, Tensor):
        xp = get_xp(X)
        return Tensor(xp.floor(X.data), device=X.device, requires_grad=False)
    else:
        xp = cp if (cp is not None and isinstance(X, cp.ndarray)) else np
        return xp.floor(X)

ceil

ceil(X)

Returns a new tensor with the ceil of the elements of input.

Source code in sorix/utils/math.py
def ceil(X):
    """Returns a new tensor with the ceil of the elements of input.
    """
    if isinstance(X, Tensor):
        xp = get_xp(X)
        return Tensor(xp.ceil(X.data), device=X.device, requires_grad=False)
    else:
        xp = cp if (cp is not None and isinstance(X, cp.ndarray)) else np
        return xp.ceil(X)

mean

mean(X, axis=None, keepdims=False)

Computes the mean value of all elements in the input tensor.

Source code in sorix/utils/math.py
def mean(X, axis=None, keepdims=False):
    """Computes the mean value of all elements in the input tensor.
    """
    if isinstance(X, Tensor):
        return X.mean(axis=axis, keepdims=keepdims)
    else:
        xp = cp if (cp is not None and isinstance(X, cp.ndarray)) else np
        return xp.mean(X, axis=axis, keepdims=keepdims)

sum

sum(X, axis=None, keepdims=False)

Computes the sum of all elements in the input tensor.

Source code in sorix/utils/math.py
def sum(X, axis=None, keepdims=False):
    """Computes the sum of all elements in the input tensor.
    """
    if isinstance(X, Tensor):
        return X.sum(axis=axis, keepdims=keepdims)
    else:
        xp = cp if (cp is not None and isinstance(X, cp.ndarray)) else np
        return xp.sum(X, axis=axis, keepdims=keepdims)