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 |
|
SliseException
Bases: Exception
Custom tag for exceptions.
Source code in slise/utils.py
17 18 19 20 |
|
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]: |
Source code in slise/utils.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
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 |
Source code in slise/utils.py
39 40 41 42 43 44 45 46 47 48 49 |
|
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]: |
Source code in slise/utils.py
52 53 54 55 56 57 58 59 60 61 62 |
|
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 |
Source code in slise/utils.py
65 66 67 68 69 70 71 72 73 74 |
|
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 |
Source code in slise/utils.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
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
|
|
Source code in slise/utils.py
93 94 95 96 97 98 99 100 101 102 103 |
|
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
|
|
Source code in slise/utils.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
|
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: |
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 |
|