Diverse useful functions in TensorFlow
Diverse Tensorflow operations used in the ChoiceLearn library.
CustomCategoricalCrossEntropy
Bases: Loss
Custom Categorical Cross Entropy Loss. Handles all options in one place.
Follows structure of tf.keras.losses.CategoricalCrossentropy and its different possibilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_logits |
bool
|
Whether to compute the softmax from logits or probabilities, by default False |
False
|
sparse |
bool
|
Whether the choice labels are integers(True) or one-hot encoded(False), by default False |
False
|
label_smoothing |
float
|
Value of smoothing to apply to labels, by default 0.0 Smoothing applied is 1.0 - label_smoothing for chosen item and label_smoothing / (num_items - 1) for all other items |
0.0
|
axis |
int
|
Axis on which to compute the softmax. Used only if from_logits is True, by default -1 |
-1
|
epsilon |
float
|
Value to apply to avoid computation issues in log, by default 1e-10 |
1e-10
|
Source code in choice_learn/tf_ops.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
__init__(from_logits=False, sparse=False, label_smoothing=0.0, axis=-1, epsilon=1e-10, name='eps_categorical_crossentropy', reduction='sum_over_batch_size')
Initialize function.
Follows structure of tf.keras.losses.CategoricalCrossentropy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_logits |
bool
|
Whether to compute the softmax from logits or probabilities, by default False |
False
|
sparse |
bool
|
Whether the choice labels are integers(True) or one-hot encoded(False), by default False |
False
|
label_smoothing |
float
|
Value of smoothing to apply to labels, by default 0.0 Smoothing applied is 1.0 - label_smoothing for chosen item and label_smoothing / (num_items - 1) for all other items |
0.0
|
axis |
int
|
Axis on which to compute the softmax. Used only if from_logits is True, by default -1 |
-1
|
epsilon |
float
|
Value to apply to avoid computation issues in log, by default 1e-10 |
1e-10
|
name |
Name of the loss function - here to follow tf.keras.losses.Loss signature |
'eps_categorical_crossentropy'
|
|
reduction |
Reduction function - here to follow tf.keras.losses.Loss signature |
'sum_over_batch_size'
|
Source code in choice_learn/tf_ops.py
call(y_true, y_pred)
Compute the cross-entropy loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true |
ndarray | Tenosr
|
Ground truth labels |
required |
y_pred |
ndarray | Tenosr
|
Predicted labels |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Average Cross-Entropy loss |
Source code in choice_learn/tf_ops.py
ExactCategoricalCrossEntropy
Bases: Loss
Custom Categorical Cross Entropy Loss. Handles all options in one place.
Follows structure of tf.keras.losses.CategoricalCrossentropy and its different possibilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_logits |
bool
|
Whether to compute the softmax from logits or probabilities, by default False |
False
|
sparse |
bool
|
Whether the choice labels are integers(True) or one-hot encoded(False), by default False |
False
|
label_smoothing |
float
|
Value of smoothing to apply to labels, by default 0.0 Smoothing applied is 1.0 - label_smoothing for chosen item and label_smoothing / (num_items - 1)for all other items |
required |
axis |
int
|
Axis on which to compute the softmax. Used only if from_logits is True, by default -1 |
-1
|
epsilon |
float
|
Value to apply to avoid computation issues in log, by default 1e-10 |
1e-35
|
Source code in choice_learn/tf_ops.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
__init__(from_logits=False, sparse=False, axis=-1, epsilon=1e-35, name='exact_categorical_crossentropy', reduction='sum_over_batch_size')
Initialize function.
Follows structure of tf.keras.losses.CategoricalCrossentropy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_logits |
bool
|
Whether to compute the softmax from logits or probabilities, by default False |
False
|
sparse |
bool
|
Whether the choice labels are integers(True) or one-hot encoded(False), by default False |
False
|
axis |
int
|
Axis on which to compute the softmax. Used only if from_logits is True, by default -1 |
-1
|
epsilon |
float
|
Value to apply to avoid computation issues in log, by default 1e-10 |
1e-35
|
name |
Name of the loss function - here to follow tf.keras.losses.Loss signature |
'exact_categorical_crossentropy'
|
|
reduction |
Reduction function - here to follow tf.keras.losses.Loss signature |
'sum_over_batch_size'
|
Source code in choice_learn/tf_ops.py
call(y_true, y_pred)
Compute the cross-entropy loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true |
ndarray | Tensor
|
Ground truth labels |
required |
y_pred |
ndarray | Tensor
|
Predicted labels |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Average Cross-Entropy loss |
Source code in choice_learn/tf_ops.py
NoiseConstrastiveEstimation
Bases: Loss
Noise Contrastive Estimation Loss.
Source code in choice_learn/tf_ops.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
__call__(logit_true, logit_negative, freq_true, freq_negative, sample_weight=None)
Overide of the tf.keras call function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logit_true |
ndarray | Tensor
|
Ground truth samplelogits |
required |
logit_negative |
ndarray | Tensor
|
Negative samples logits |
required |
freq_true |
ndarray | Tensor
|
Frequency / probabilty in data of true sample |
required |
freq_negative |
ndarray | Tensor
|
Frequency / probabilty in data of negative samples |
required |
sample_weight |
ndarray | Tensor
|
samplewise weights |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Average Cross-Entropy loss |
Source code in choice_learn/tf_ops.py
__init__(epsilon=1e-10, name='noise_contrastive_estimation', reduction='sum_over_batch_size')
Initialize function.
Follows structure of tf.keras.losses.CategoricalCrossentropy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epsilon |
safety net for logarithm computation |
1e-10
|
|
name |
Name of the loss function - here to follow tf.keras.losses.Loss signature |
'noise_contrastive_estimation'
|
|
reduction |
Reduction function - here to follow tf.keras.losses.Loss signature |
'sum_over_batch_size'
|
Source code in choice_learn/tf_ops.py
call(logit_true, logit_negative, freq_true, freq_negative)
Compute NCE loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logit_true |
ndarray | Tensor
|
Ground truth samples logit |
required |
logit_negative |
ndarray | Tensor
|
Negative samples logit |
required |
freq_true |
ndarray | Tensor
|
Frequency / probabilty in data of true sample |
required |
freq_negative |
ndarray | Tensor
|
Frequency / probabilty in data of negative samples |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Average Cross-Entropy loss |
Source code in choice_learn/tf_ops.py
softmax_with_availabilities(items_logit_by_choice, available_items_by_choice, axis=-1, normalize_exit=False, eps=1e-05)
Compute softmax probabilities from utilities.
Takes into account availabilities (1 if the product is available, 0 otherwise) to set probabilities to 0 for unavailable products and to renormalize the probabilities of available products.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items_logit_by_choice |
ndarray(n_choices, n_items)
|
Utilities / Logits on which to compute the softmax |
required |
available_items_by_choice |
ndarray(n_choices, n_items)
|
Matrix indicating the availability (1) or not (0) of the products |
required |
axis |
int
|
Axis of items_logit_by_choice on which to apply the softmax, by default -1 |
-1
|
normalize_exit |
bool
|
Whether to normalize the probabilities of available products with an exit choice of utility 1, by default False |
False
|
eps |
float
|
Value to avoid division by 0 when a product with probability almost 1 is unavailable, by default 1e-5 |
1e-05
|
Returns:
| Type | Description |
|---|---|
Tensor(n_choices, n_items)
|
Probabilities of each product for each choice computed from Logits |