Utils deep keras
AttentionAverage
Bases: Layer
Source code in template_nlp/models_training/models_tensorflow/utils_deep_keras.py
AttentionWithContext
Bases: Layer
Attention operation, with a context/query vector, for temporal data. Supports Masking. Follows the work of Yang et al. [https://www.cs.cmu.edu/~diyiy/docs/naacl16.pdf] "Hierarchical Attention Networks for Document Classification" by using a context vector to assist the attention
Input shape
3D tensor with shape: `(samples, steps, features)`.
Output shape
2D tensor with shape: `(samples, features)`.
:param kwargs: Just put it on top of an RNN Layer (GRU/LSTM/SimpleRNN) with return_sequences=True. The dimensions are inferred based on the output shape of the RNN. Example: model.add(LSTM(64, return_sequences=True)) model.add(AttentionWithContext())
Source code in template_nlp/models_training/models_tensorflow/utils_deep_keras.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
|
compare_keras_models(model1, model2)
Checks if all weights of each keras model layer are the same
Source code in template_nlp/models_training/models_tensorflow/utils_deep_keras.py
f1(y_true, y_pred)
f1 score, to use as custom metrics
- /! To use with a big batch size /! -
From
https://www.kaggle.com/rejpalcz/best-loss-function-for-f1-score-metric https://stackoverflow.com/questions/59963911/how-to-write-a-custom-f1-loss-function-with-weighted-average-for-keras
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true |
Ground truth values |
required | |
y_pred |
The predicted values |
required |
Returns: float: metric
Source code in template_nlp/models_training/models_tensorflow/utils_deep_keras.py
f1_loss(y_true, y_pred)
f1 loss, to use as custom loss
- /! To use with a big batch size /! -
From
https://www.kaggle.com/rejpalcz/best-loss-function-for-f1-score-metric https://stackoverflow.com/questions/59963911/how-to-write-a-custom-f1-loss-function-with-weighted-average-for-keras
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true |
Ground truth values |
required | |
y_pred |
The predicted values |
required |
Returns: float: metric
Source code in template_nlp/models_training/models_tensorflow/utils_deep_keras.py
fb_loss(b, y_true, y_pred)
fB loss, to use as custom loss
- /! To use with a big batch size /! -
From
https://www.kaggle.com/rejpalcz/best-loss-function-for-f1-score-metric https://stackoverflow.com/questions/59963911/how-to-write-a-custom-f1-loss-function-with-weighted-average-for-keras
Parameters:
Name | Type | Description | Default |
---|---|---|---|
b |
float
|
importance recall in the calculation of the fB score |
required |
y_true |
Ground truth values |
required | |
y_pred |
The predicted values |
required |
Returns: float: metric
Source code in template_nlp/models_training/models_tensorflow/utils_deep_keras.py
get_fb_loss(b=2.0)
Gets a fB-score loss
Parameters:
Name | Type | Description | Default |
---|---|---|---|
b |
float
|
importance recall in the calculation of the fB score |
2.0
|
Returns: Callable: fb_loss
Source code in template_nlp/models_training/models_tensorflow/utils_deep_keras.py
get_weighted_binary_crossentropy(pos_weight=10.0)
Gets a "weighted binary crossentropy" loss From https://stats.stackexchange.com/questions/261128/neural-network-for-multi-label-classification-with-large-number-of-classes-outpu TO BE ADDED IN custom_objects : 'weighted_binary_crossentropy': utils_deep_keras.get_weighted_binary_crossentropy(pos_weight=...)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos_weight |
float
|
Weight of the positive class, to be tuned |
10.0
|
Returns: Callable: Weighted binary crossentropy loss
Source code in template_nlp/models_training/models_tensorflow/utils_deep_keras.py
precision(y_true, y_pred)
Precision, to use as custom metrics
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true |
Ground truth values |
required | |
y_pred |
The predicted values |
required |
Returns: float: metric
Source code in template_nlp/models_training/models_tensorflow/utils_deep_keras.py
recall(y_true, y_pred)
Recall to use as a custom metrics
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true |
Ground truth values |
required | |
y_pred |
The predicted values |
required |
Returns: float: metric
Source code in template_nlp/models_training/models_tensorflow/utils_deep_keras.py
weighted_binary_crossentropy(pos_weight, target, output)
Weighted binary crossentropy between an output tensor and a target tensor. pos_weight is used as a multiplier for the positive targets.
Combination of the following functions: * keras.losses.binary_crossentropy * keras.backend.tensorflow_backend.binary_crossentropy * tf.nn.weighted_cross_entropy_with_logits
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos_weight |
float
|
poid classe positive, to be tuned |
required |
target |
Target tensor |
required | |
output |
Output tensor |
required |
Returns: float: metric