Skip to content

slise.utils

This script contains some utility functions.

SliseWarning

Bases: RuntimeWarning

Custom tag for warnings.

Source code in slise/utils.py
11
12
13
14
class SliseWarning(RuntimeWarning):
    """
    Custom tag for warnings.
    """

SliseException

Bases: Exception

Custom tag for exceptions.

Source code in slise/utils.py
17
18
19
20
class SliseException(Exception):
    """
    Custom tag for exceptions.
    """

limited_logit(p, stab=0.001)

Computes logits from probabilities.

Parameters:

Name Type Description Default
p Union[ndarray, float]

Probability vector or scalar.

required
stab float

Limit p to [stab, 1-stab] for numerical stability. Defaults to 0.001.

0.001

Returns:

Type Description
Union[ndarray, float]

Union[np.ndarray, float]: logit(clamp(p, stab, 1-stab)).

Source code in slise/utils.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def limited_logit(
    p: Union[np.ndarray, float], stab: float = 0.001
) -> Union[np.ndarray, float]:
    """Computes logits from probabilities.

    Args:
        p (Union[np.ndarray, float]): Probability vector or scalar.
        stab (float, optional): Limit p to [stab, 1-stab] for numerical stability. Defaults to 0.001.

    Returns:
        Union[np.ndarray, float]: `logit(clamp(p, stab, 1-stab))`.
    """
    p = np.minimum(1.0 - stab, np.maximum(stab, p))
    return np.log(p / (1.0 - p))

dsigmoid(x)

Derivative of the sigmoid function.

Parameters:

Name Type Description Default
x Union[ndarray, float]

Real vector or scalar.

required

Returns:

Type Description
Union[ndarray, float]

Union[np.ndarray, float]: Derivative of sigmoid(x).

Source code in slise/utils.py
39
40
41
42
43
44
45
46
47
48
49
def dsigmoid(x: Union[np.ndarray, float]) -> Union[np.ndarray, float]:
    """Derivative of the sigmoid function.

    Args:
        x (Union[np.ndarray, float]): Real vector or scalar.

    Returns:
        Union[np.ndarray, float]: Derivative of `sigmoid(x)`.
    """
    s = sigmoid(x)
    return s * (1 - s)

log_sigmoid(x)

Computes log(sigmoid(x)) in a numerically stable way.

Parameters:

Name Type Description Default
x Union[ndarray, float]

Real vector or scalar.

required

Returns:

Type Description
Union[ndarray, float]

Union[np.ndarray, float]: log(sigmoid(x))

Source code in slise/utils.py
52
53
54
55
56
57
58
59
60
61
62
def log_sigmoid(x: Union[np.ndarray, float]) -> Union[np.ndarray, float]:
    """Computes `log(sigmoid(x))` in a numerically stable way.

    Args:
        x (Union[np.ndarray, float]): Real vector or scalar.

    Returns:
        Union[np.ndarray, float]: `log(sigmoid(x))`
    """
    y = -np.sign(x)
    return (y * 0.5 + 0.5) * x - np.log1p(np.exp(y * x))

dlog_sigmoid(x)

Derivative of log(sigmoid(x)).

Parameters:

Name Type Description Default
x Union[ndarray, float]

Real vector or scalar.

required

Returns:

Type Description
Union[ndarray, float]

Union[np.ndarray, float]: Derivative of log(sigmoid(x))

Source code in slise/utils.py
65
66
67
68
69
70
71
72
73
74
def dlog_sigmoid(x: Union[np.ndarray, float]) -> Union[np.ndarray, float]:
    """Derivative of `log(sigmoid(x))`.

    Args:
        x (Union[np.ndarray, float]): Real vector or scalar.

    Returns:
        Union[np.ndarray, float]: Derivative of `log(sigmoid(x))`
    """
    return 1 - sigmoid(x)

sparsity(x, treshold=0)

Count the number of abs(x) > treshold.

Parameters:

Name Type Description Default
x Union[ndarray, float]

Real vector or scalar.

required
treshold float

Threshold non-inclusive. Defaults to 0.

0

Returns:

Name Type Description
int int

The number of abs(x) > treshold.

Source code in slise/utils.py
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def sparsity(x: Union[np.ndarray, float], treshold: float = 0) -> int:
    """Count the number of `abs(x) > treshold`.

    Args:
        x (Union[np.ndarray, float]): Real vector or scalar.
        treshold (float, optional): Threshold non-inclusive. Defaults to 0.

    Returns:
        int: The number of `abs(x) > treshold`.
    """
    if treshold > 0:
        return np.count_nonzero(np.abs(x) > treshold)
    else:
        return np.count_nonzero(x)

log_sum_exp(x)

Computes log(sum(exp(x))) in a numerically stable way.

Parameters:

Name Type Description Default
x ndarray

Real vector.

required

Returns:

Name Type Description
float float

log(sum(exp(x)))

Source code in slise/utils.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def log_sum_exp(x: np.ndarray) -> float:
    """Computes `log(sum(exp(x)))` in a numerically stable way.

    Args:
        x (np.ndarray): Real vector.

    Returns:
        float: `log(sum(exp(x)))`
    """
    xmax = np.max(x)
    return xmax + np.log(np.sum(np.exp(x - xmax)))

log_sum_special(x, y)

Computes log(sum(exp(x) * y)) (or log(sum(exp(x))) if all(y == 0)), in a numerically stable way.

Parameters:

Name Type Description Default
x ndarray

Real vector.

required
y ndarray

Real vector.

required

Returns:

Name Type Description
float float

log(sum(exp(x) * y)).

Source code in slise/utils.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def log_sum_special(x: np.ndarray, y: np.ndarray) -> float:
    """Computes `log(sum(exp(x) * y))` (or `log(sum(exp(x)))` if `all(y == 0)`), in a numerically stable way.

    Args:
        x (np.ndarray): Real vector.
        y (np.ndarray): Real vector.

    Returns:
        float: `log(sum(exp(x) * y))`.
    """
    xmax = np.max(x)
    xexp = np.exp(x - xmax)
    xsum = np.sum(xexp * y)
    if xsum == 0:
        xsum = np.sum(xexp)
    return xmax + np.log(xsum)

mat_mul_inter(X, alpha)

Matrix multiplication, but check and handle potential intercepts in alpha.

Parameters:

Name Type Description Default
X ndarray

Real matrix or vector.

required
alpha ndarray

Real vector.

required

Returns:

Type Description
ndarray

np.ndarray: X @ alpha or X @ alpha[1:] + alpha[0].

Source code in slise/utils.py
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def mat_mul_inter(X: np.ndarray, alpha: np.ndarray) -> np.ndarray:
    """Matrix multiplication, but check and handle potential intercepts in `alpha`.

    Args:
        X (np.ndarray): Real matrix or vector.
        alpha (np.ndarray): Real vector.

    Returns:
        np.ndarray: `X @ alpha` or `X @ alpha[1:] + alpha[0]`.
    """
    alpha = np.atleast_1d(alpha)
    if len(X.shape) == 1:
        if len(alpha) == X.size:
            return np.sum(alpha[1:] * X)
        if len(alpha) == X.size + 1:
            return alpha[0] + np.sum(alpha[1:] * X)
        else:
            X = np.reshape(X, X.shape + (1,))
    if len(alpha) == X.shape[1] + 1:
        return X @ alpha[1:] + alpha[0]
    else:
        return X @ alpha