Customs related to optimization
Custom loss functions
SoftmaxFocalCrossEntropy
Bases: Loss
Categorical Focal Loss for multiclass classification. Inherited from tf.keras.losses.Loss
Focal loss applies a modulating term to the cross entropy loss to focus learning on hard misclassified examples. This results in both better performance in tasks with imbalanced data and a better calibrated outputs.
.. math:: FocalLoss(p_t) = \alpha_t * (1-p_t)^\gamma * log(p_t)
Where
.. math:: p_t = p; \hspace{1.2} a_t = a when y=1, and
.. math:: p_t = 1-p; \hspace{1.2} a_t = 1-a otherwise
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gamma |
float
|
modulating factor |
2.0
|
alpha |
float
|
weighting factor |
0.75
|
Source code in conftrainer/optimization/losses.py
call(y_true, y_pred, from_logits=False)
Compute the loss for given inputs
Returns:
| Name | Type | Description |
|---|---|---|
out |
tf.Tensor
|
Categorical focal loss |
Source code in conftrainer/optimization/losses.py
BYOLLoss
Bases: Loss
Implementation of BYOL loss from the BYOL paper.
Normalizes both online and target networks outputs and returns their cosine distance.
Inherits from tf.keras.losses.Loss
Parameters:
name: str name of the instance. Used during logging and in callbacks Methods:
get_serialized_dict: returns a dict with name of the loss as key and the serialized config as value
Source code in conftrainer/optimization/losses.py
call(y_true, y_pred)
staticmethod
BYOL loss This is a simple cosine similarity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true |
tf.Tensor
|
Tensors of equal shape to calculate cosine similarity of |
required |
y_pred |
tf.Tensor
|
Tensors of equal shape to calculate cosine similarity of |
required |
Source code in conftrainer/optimization/losses.py
SigmoidFocalCrossEntropy
Bases: Loss
Implements the focal loss function.
Focal loss was first introduced in the RetinaNet paper (https://arxiv.org/pdf/1708.02002.pdf). Focal loss is extremely useful for classification when you have highly imbalanced classes. It down-weights well-classified examples and focuses on hard examples. The loss value is much higher for a sample which is misclassified by the classifier as compared to the loss value corresponding to a well-classified example. One of the best use-cases of focal loss is its usage in object detection where the imbalance between the background class and other classes is extremely high.
Usage:
fl = tfa.losses.SigmoidFocalCrossEntropy() loss = fl( ... y_true = [[1.0], [1.0], [0.0]],y_pred = [[0.97], [0.91], [0.03]]) loss
Usage with tf.keras API:
model = tf.keras.Model() model.compile('sgd', loss=tfa.losses.SigmoidFocalCrossEntropy())
Args: alpha: balancing factor, default value is 0.25. gamma: modulating factor, default value is 2.0.
Returns:
Weighted loss float Tensor. If reduction is NONE, this has the same
shape as y_true; otherwise, it is scalar.
Raises:
ValueError: If the shape of sample_weight is invalid or value of
gamma is less than zero.
Source code in conftrainer/optimization/losses.py
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 152 153 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 | |
call(y_true, y_pred, from_logits=False)
Implements the focal loss function.
Focal loss was first introduced in the RetinaNet paper (https://arxiv.org/pdf/1708.02002.pdf). Focal loss is extremely useful for classification when you have highly imbalanced classes. It down-weights well-classified examples and focuses on hard examples. The loss value is much higher for a sample which is misclassified by the classifier as compared to the loss value corresponding to a well-classified example. One of the best use-cases of focal loss is its usage in object detection where the imbalance between the background class and other classes is extremely high.
Args: y_true: true targets tensor. y_pred: predictions tensor. alpha: balancing factor. gamma: modulating factor.
Returns:
Weighted loss float Tensor. If reduction is NONE,this has the
same shape as y_true; otherwise, it is scalar.
Source code in conftrainer/optimization/losses.py
Optimization related utils
get_metrics(metric_config_list)
Import and initialize metrics from tensorflow and tensorflow addons
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_config_list |
List[Dict[str, dict]]
|
dicts with names and arguments of metrics to import |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
Dict[str, tf.keras.metrics.Metric]
|
names of metrics as keys and initialized metrics |