computes the mean of squares of errors between labels and predictions formula python loss meansquareytrue ypred args reduction type of reduction to apply to the loss in almost all cases this should be sumoverbatchsize supported options are sum sumoverbatchsize or none name optional name for the loss instance computes the mean of absolute difference between labels and predictions formula python loss meanabsytrue ypred args reduction type of reduction to apply to the loss in almost all cases this should be sumoverbatchsize supported options are sum sumoverbatchsize or none name optional name for the loss instance computes the mean absolute percentage error between ytrue ypred formula python loss 100 meanabsytrue ypred ytrue args reduction type of reduction to apply to the loss in almost all cases this should be sumoverbatchsize supported options are sum sumoverbatchsize or none name optional name for the loss instance computes the mean squared logarithmic error between ytrue ypred formula python loss meansquarelogytrue 1 logypred 1 args reduction type of reduction to apply to the loss in almost all cases this should be sumoverbatchsize supported options are sum sumoverbatchsize or none name optional name for the loss instance computes the cosine similarity between ytrue ypred note that it is a number between 1 and 1 when it is a negative number between 1 and 0 0 indicates orthogonality and values closer to 1 indicate greater similarity this makes it usable as a loss function in a setting where you try to maximize the proximity between predictions and targets if either ytrue or ypred is a zero vector cosine similarity will be 0 regardless of the proximity between predictions and targets formula python loss suml2normytrue l2normypred args axis the axis along which the cosine similarity is computed the features axis defaults to 1 reduction type of reduction to apply to the loss in almost all cases this should be sumoverbatchsize supported options are sum sumoverbatchsize or none name optional name for the loss instance computes the huber loss between ytrue ypred formula python for x in error if absx delta loss append0 5 x2 elif absx delta loss appenddelta absx 0 5 delta2 loss meanloss axis1 see huber losshttps en wikipedia orgwikihuberloss args delta a float the point where the huber loss function changes from a quadratic to linear reduction type of reduction to apply to loss options are sum sumoverbatchsize or none defaults to sumoverbatchsize name optional name for the instance computes the logarithm of the hyperbolic cosine of the prediction error formula python error ypred ytrue logcosh meanlogexperror experror2 axis1 where x is the error ypred ytrue args reduction type of reduction to apply to loss options are sum sumoverbatchsize or none defaults to sumoverbatchsize name optional name for the instance computes the hinge loss between ytrue ypred formula python loss maximum1 ytrue ypred 0 ytrue values are expected to be 1 or 1 if binary 0 or 1 labels are provided we will convert them to 1 or 1 args reduction type of reduction to apply to the loss in almost all cases this should be sumoverbatchsize supported options are sum sumoverbatchsize or none name optional name for the loss instance computes the squared hinge loss between ytrue ypred formula python loss squaremaximum1 ytrue ypred 0 ytrue values are expected to be 1 or 1 if binary 0 or 1 labels are provided we will convert them to 1 or 1 args reduction type of reduction to apply to the loss in almost all cases this should be sumoverbatchsize supported options are sum sumoverbatchsize or none name optional name for the loss instance computes the categorical hinge loss between ytrue ypred formula python loss maximumneg pos 1 0 where negmaximum1ytrueypred and possumytrueypred args reduction type of reduction to apply to the loss in almost all cases this should be sumoverbatchsize supported options are sum sumoverbatchsize or none name optional name for the loss instance computes kullbackleibler divergence loss between ytrue ypred formula python loss ytrue logytrue ypred args reduction type of reduction to apply to the loss in almost all cases this should be sumoverbatchsize supported options are sum sumoverbatchsize or none name optional name for the loss instance computes the poisson loss between ytrue ypred formula python loss ypred ytrue logypred args reduction type of reduction to apply to the loss in almost all cases this should be sumoverbatchsize supported options are sum sumoverbatchsize or none name optional name for the loss instance computes the crossentropy loss between true labels and predicted labels use this crossentropy loss for binary 0 or 1 classification applications the loss function requires the following inputs ytrue true label this is either 0 or 1 ypred predicted value this is the model s prediction i e a single floatingpoint value which either represents a logithttps en wikipedia orgwikilogit i e value in inf inf when fromlogitstrue or a probability i e value in 0 1 when fromlogitsfalse args fromlogits whether to interpret ypred as a tensor of logithttps en wikipedia orgwikilogit values by default we assume that ypred is probabilities i e values in 0 1 labelsmoothing float in range 0 1 when 0 no smoothing occurs when 0 we compute the loss between the predicted labels and a smoothed version of the true labels where the smoothing squeezes the labels towards 0 5 larger values of labelsmoothing correspond to heavier smoothing axis the axis along which to compute crossentropy the features axis defaults to 1 reduction type of reduction to apply to the loss in almost all cases this should be sumoverbatchsize supported options are sum sumoverbatchsize or none name optional name for the loss instance examples recommended usage set fromlogitstrue with compile api python model compile losskeras losses binarycrossentropyfromlogitstrue as a standalone function example 1 batchsize 1 number of samples 4 ytrue 0 1 0 0 ypred 18 6 0 51 2 94 12 8 bce keras losses binarycrossentropyfromlogitstrue bceytrue ypred 0 865 example 2 batchsize 2 number of samples 4 ytrue 0 1 0 0 ypred 18 6 0 51 2 94 12 8 using default auto sumoverbatchsize reduction type bce keras losses binarycrossentropyfromlogitstrue bceytrue ypred 0 865 using sampleweight attribute bceytrue ypred sampleweight0 8 0 2 0 243 using sum reduction type bce keras losses binarycrossentropyfromlogitstrue reductionsum bceytrue ypred 1 730 using none reduction type bce keras losses binarycrossentropyfromlogitstrue reductionnone bceytrue ypred array0 235 1 496 dtypefloat32 default usage set fromlogitsfalse make the following updates to the above recommended usage section 1 set fromlogitsfalse keras losses binarycrossentropy or fromlogitsfalse 2 update ypred to use probabilities instead of logits ypred 0 6 0 3 0 2 0 8 or 0 6 0 3 0 2 0 8 computes focal crossentropy loss between true labels and predictions binary crossentropy loss is often used for binary 0 or 1 classification tasks the loss function requires the following inputs ytrue true label this is either 0 or 1 ypred predicted value this is the model s prediction i e a single floatingpoint value which either represents a logithttps en wikipedia orgwikilogit i e value in inf inf when fromlogitstrue or a probability i e value in 0 1 when fromlogitsfalse according to lin et al 2018https arxiv orgpdf1708 02002 pdf it helps to apply a focal factor to downweight easy examples and focus more on hard examples by default the focal tensor is computed as follows focalfactor 1 output gamma for class 1 focalfactor output gamma for class 0 where gamma is a focusing parameter when gamma0 this function is equivalent to the binary crossentropy loss args applyclassbalancing a bool whether to apply weight balancing on the binary classes 0 and 1 alpha a weight balancing factor for class 1 default is 0 25 as mentioned in reference lin et al 2018 https arxiv orgpdf1708 02002 pdf the weight for class 0 is 1 0 alpha gamma a focusing parameter used to compute the focal factor default is 2 0 as mentioned in the reference lin et al 2018https arxiv orgpdf1708 02002 pdf fromlogits whether to interpret ypred as a tensor of logithttps en wikipedia orgwikilogit values by default we assume that ypred are probabilities i e values in 0 1 labelsmoothing float in 0 1 when 0 no smoothing occurs when 0 we compute the loss between the predicted labels and a smoothed version of the true labels where the smoothing squeezes the labels towards 0 5 larger values of labelsmoothing correspond to heavier smoothing axis the axis along which to compute crossentropy the features axis defaults to 1 reduction type of reduction to apply to the loss in almost all cases this should be sumoverbatchsize supported options are sum sumoverbatchsize or none name optional name for the loss instance examples with the compile api python model compile losskeras losses binaryfocalcrossentropy gamma2 0 fromlogitstrue as a standalone function example 1 batchsize 1 number of samples 4 ytrue 0 1 0 0 ypred 18 6 0 51 2 94 12 8 loss keras losses binaryfocalcrossentropy gamma2 fromlogitstrue lossytrue ypred 0 691 apply class weight loss keras losses binaryfocalcrossentropy applyclassbalancingtrue gamma2 fromlogitstrue lossytrue ypred 0 51 example 2 batchsize 2 number of samples 4 ytrue 0 1 0 0 ypred 18 6 0 51 2 94 12 8 using default auto sumoverbatchsize reduction type loss keras losses binaryfocalcrossentropy gamma3 fromlogitstrue lossytrue ypred 0 647 apply class weight loss keras losses binaryfocalcrossentropy applyclassbalancingtrue gamma3 fromlogitstrue lossytrue ypred 0 482 using sampleweight attribute with focal effect loss keras losses binaryfocalcrossentropy gamma3 fromlogitstrue lossytrue ypred sampleweight0 8 0 2 0 133 apply class weight loss keras losses binaryfocalcrossentropy applyclassbalancingtrue gamma3 fromlogitstrue lossytrue ypred sampleweight0 8 0 2 0 097 using sum reduction type loss keras losses binaryfocalcrossentropy gamma4 fromlogitstrue reductionsum lossytrue ypred 1 222 apply class weight loss keras losses binaryfocalcrossentropy applyclassbalancingtrue gamma4 fromlogitstrue reductionsum lossytrue ypred 0 914 using none reduction type loss keras losses binaryfocalcrossentropy gamma5 fromlogitstrue reductionnone lossytrue ypred array0 0017 1 1561 dtypefloat32 apply class weight loss keras losses binaryfocalcrossentropy applyclassbalancingtrue gamma5 fromlogitstrue reductionnone lossytrue ypred array0 0004 0 8670 dtypefloat32 computes the crossentropy loss between the labels and predictions use this crossentropy loss function when there are two or more label classes we expect labels to be provided in a onehot representation if you want to provide labels as integers please use sparsecategoricalcrossentropy loss there should be numclasses floating point values per feature i e the shape of both ypred and ytrue are batchsize numclasses args fromlogits whether ypred is expected to be a logits tensor by default we assume that ypred encodes a probability distribution labelsmoothing float in 0 1 when 0 label values are smoothed meaning the confidence on label values are relaxed for example if 0 1 use 0 1 numclasses for nontarget labels and 0 9 0 1 numclasses for target labels axis the axis along which to compute crossentropy the features axis defaults to 1 reduction type of reduction to apply to the loss in almost all cases this should be sumoverbatchsize supported options are sum sumoverbatchsize or none name optional name for the loss instance examples standalone usage ytrue 0 1 0 0 0 1 ypred 0 05 0 95 0 0 1 0 8 0 1 using auto sumoverbatchsize reduction type cce keras losses categoricalcrossentropy cceytrue ypred 1 177 calling with sampleweight cceytrue ypred sampleweightnp array0 3 0 7 0 814 using sum reduction type cce keras losses categoricalcrossentropy reductionsum cceytrue ypred 2 354 using none reduction type cce keras losses categoricalcrossentropy reductionnone cceytrue ypred array0 0513 2 303 dtypefloat32 usage with the compile api python model compileoptimizer sgd losskeras losses categoricalcrossentropy computes the alpha balanced focal crossentropy loss use this crossentropy loss function when there are two or more label classes and if you want to handle class imbalance without using classweights we expect labels to be provided in a onehot representation according to lin et al 2018https arxiv orgpdf1708 02002 pdf it helps to apply a focal factor to downweight easy examples and focus more on hard examples the general formula for the focal loss fl is as follows flpt 1 pt gamma logpt where pt is defined as follows pt output if ytrue 1 else 1 output 1 pt gamma is the modulatingfactor where gamma is a focusing parameter when gamma 0 there is no focal effect on the cross entropy gamma reduces the importance given to simple examples in a smooth manner the s use alphabalanced variant of focal loss fl in the paper flpt alpha 1 pt gamma logpt where alpha is the weight factor for the classes if alpha 1 the loss won t be able to handle class imbalance properly as all classes will have the same weight this can be a constant or a list of constants if alpha is a list it must have the same length as the number of classes the formula above can be generalized to flpt alpha 1 pt gamma crossentropyytrue ypred where minus comes from crossentropyytrue ypred ce extending this to multiclass case is straightforward flpt alpha 1 pt gamma categoricalceytrue ypred in the snippet below there is numclasses floating pointing values per example the shape of both ypred and ytrue are batchsize numclasses args alpha a weight balancing factor for all classes default is 0 25 as mentioned in the reference it can be a list of floats or a scalar in the multiclass case alpha may be set by inverse class frequency by using computeclassweight from sklearn utils gamma a focusing parameter default is 2 0 as mentioned in the reference it helps to gradually reduce the importance given to simple easy examples in a smooth manner fromlogits whether output is expected to be a logits tensor by default we consider that output encodes a probability distribution labelsmoothing float in 0 1 when 0 label values are smoothed meaning the confidence on label values are relaxed for example if 0 1 use 0 1 numclasses for nontarget labels and 0 9 0 1 numclasses for target labels axis the axis along which to compute crossentropy the features axis defaults to 1 reduction type of reduction to apply to the loss in almost all cases this should be sumoverbatchsize supported options are sum sumoverbatchsize or none name optional name for the loss instance examples standalone usage ytrue 0 1 0 0 0 1 ypred 0 05 0 95 0 0 1 0 8 0 1 using auto sumoverbatchsize reduction type cce keras losses categoricalfocalcrossentropy cceytrue ypred 0 23315276 calling with sampleweight cceytrue ypred sampleweightnp array0 3 0 7 0 1632 using sum reduction type cce keras losses categoricalfocalcrossentropy reductionsum cceytrue ypred 0 46631 using none reduction type cce keras losses categoricalfocalcrossentropy reductionnone cceytrue ypred array3 2058331e05 4 6627346e01 dtypefloat32 usage with the compile api python model compileoptimizer adam losskeras losses categoricalfocalcrossentropy initializes categoricalfocalcrossentropy instance super init categoricalfocalcrossentropy alphaalpha gammagamma namename reductionreduction fromlogitsfromlogits labelsmoothinglabelsmoothing axisaxis self fromlogits fromlogits self labelsmoothing labelsmoothing self axis axis self alpha alpha self gamma gamma def getconfigself return name self name reduction self reduction fromlogits self fromlogits labelsmoothing self labelsmoothing axis self axis alpha self alpha gamma self gamma kerasexportkeras losses sparsecategoricalcrossentropy class sparsecategoricalcrossentropylossfunctionwrapper def init self fromlogitsfalse ignoreclassnone reductionsumoverbatchsize namesparsecategoricalcrossentropy super init sparsecategoricalcrossentropy namename reductionreduction fromlogitsfromlogits ignoreclassignoreclass self fromlogits fromlogits self ignoreclass ignoreclass def getconfigself return name self name reduction self reduction fromlogits self fromlogits ignoreclass self ignoreclass def convertbinarylabelstohingeytrue convert the binary labels to 1 or 1 returns the labels unchanged if they are nonbinary computes the hinge loss between ytrue ypred formula python loss meanmaximum1 ytrue ypred 0 axis1 args ytrue the ground truth values ytrue values are expected to be 1 or 1 if binary 0 or 1 labels are provided they will be converted to 1 or 1 with shape batchsize d0 dn ypred the predicted values with shape batchsize d0 dn returns hinge loss values with shape batchsize d0 dn1 example ytrue np random choice1 1 size2 3 ypred np random randomsize2 3 loss keras losses hingeytrue ypred computes the squared hinge loss between ytrue ypred formula python loss meansquaremaximum1 ytrue ypred 0 axis1 args ytrue the ground truth values ytrue values are expected to be 1 or 1 if binary 0 or 1 labels are provided we will convert them to 1 or 1 with shape batchsize d0 dn ypred the predicted values with shape batchsize d0 dn returns squared hinge loss values with shape batchsize d0 dn1 example ytrue np random choice1 1 size2 3 ypred np random randomsize2 3 loss keras losses squaredhingeytrue ypred computes the categorical hinge loss between ytrue ypred formula python loss maximumneg pos 1 0 where negmaximum1ytrueypred and possumytrueypred args ytrue the ground truth values ytrue values are expected to be either 1 1 or 0 1 i e a onehotencoded tensor with shape batchsize d0 dn ypred the predicted values with shape batchsize d0 dn returns categorical hinge loss values with shape batchsize d0 dn1 example ytrue np random randint0 3 size2 ytrue np eyenp maxytrue 1ytrue ypred np random randomsize2 3 loss keras losses categoricalhingeytrue ypred legacy aliases computes the mean squared error between labels and predictions formula python loss meansquareytrue ypred axis1 example ytrue np random randint0 2 size2 3 ypred np random randomsize2 3 loss keras losses meansquarederrorytrue ypred args ytrue ground truth values with shape batchsize d0 dn ypred the predicted values with shape batchsize d0 dn returns mean squared error values with shape batchsize d0 dn1 legacy aliases computes the mean absolute error between labels and predictions python loss meanabsytrue ypred axis1 args ytrue ground truth values with shape batchsize d0 dn ypred the predicted values with shape batchsize d0 dn returns mean absolute error values with shape batchsize d0 dn1 example ytrue np random randint0 2 size2 3 ypred np random randomsize2 3 loss keras losses meanabsoluteerrorytrue ypred legacy aliases computes the mean absolute percentage error between ytrue ypred formula python loss 100 meanabsytrue ypred ytrue axis1 division by zero is prevented by dividing by maximumytrue epsilon where epsilon keras backend epsilon default to 1e7 args ytrue ground truth values with shape batchsize d0 dn ypred the predicted values with shape batchsize d0 dn returns mean absolute percentage error values with shape batchsize d0 dn1 example ytrue np random randomsize2 3 ypred np random randomsize2 3 loss keras losses meanabsolutepercentageerrorytrue ypred legacy aliases computes the mean squared logarithmic error between ytrue ypred formula python loss meansquarelogytrue 1 logypred 1 axis1 note that ypred and ytrue cannot be less or equal to 0 negative values and 0 values will be replaced with keras backend epsilon default to 1e7 args ytrue ground truth values with shape batchsize d0 dn ypred the predicted values with shape batchsize d0 dn returns mean squared logarithmic error values with shape batchsize d0 dn1 example ytrue np random randint0 2 size2 3 ypred np random randomsize2 3 loss keras losses meansquaredlogarithmicerrorytrue ypred computes the cosine similarity between labels and predictions formula python loss suml2normytrue l2normypred note that it is a number between 1 and 1 when it is a negative number between 1 and 0 0 indicates orthogonality and values closer to 1 indicate greater similarity this makes it usable as a loss function in a setting where you try to maximize the proximity between predictions and targets if either ytrue or ypred is a zero vector cosine similarity will be 0 regardless of the proximity between predictions and targets args ytrue tensor of true targets ypred tensor of predicted targets axis axis along which to determine similarity defaults to 1 returns cosine similarity tensor example ytrue 0 1 1 1 1 1 ypred 1 0 1 1 1 1 loss keras losses cosinesimilarityytrue ypred axis1 0 0 99999994 0 99999994 computes huber loss value formula python for x in error if absx delta loss append0 5 x2 elif absx delta loss appenddelta absx 0 5 delta2 loss meanloss axis1 see huber losshttps en wikipedia orgwikihuberloss example ytrue 0 1 0 0 ypred 0 6 0 4 0 4 0 6 loss keras losses huberytrue ypred 0 155 args ytrue tensor of true targets ypred tensor of predicted targets delta a float the point where the huber loss function changes from a quadratic to linear defaults to 1 0 returns tensor with one scalar loss entry per sample legacy aliases logarithm of the hyperbolic cosine of the prediction error formula python loss meanlogcoshypred ytrue axis1 note that logcoshx is approximately equal to x 2 2 for small x and to absx log2 for large x this means that logcosh works mostly like the mean squared error but will not be so strongly affected by the occasional wildly incorrect prediction example ytrue 0 1 0 0 ypred 1 1 0 0 loss keras losses logcoshytrue ypred 0 108 args ytrue ground truth values with shape batchsize d0 dn ypred the predicted values with shape batchsize d0 dn returns logcosh error values with shape batchsize d0 dn1 legacy aliases computes kullbackleibler divergence loss between ytrue ypred formula python loss ytrue logytrue ypred args ytrue tensor of true targets ypred tensor of predicted targets returns kl divergence loss values with shape batchsize d0 dn1 example ytrue np random randint0 2 size2 3 astypenp float32 ypred np random randomsize2 3 loss keras losses kldivergenceytrue ypred assert loss shape 2 ytrue ops clipytrue 1e7 1 ypred ops clipypred 1e7 1 assert np arrayequal loss np sumytrue np logytrue ypred axis1 computes the poisson loss between ytrue and ypred formula python loss ypred ytrue logypred args ytrue ground truth values shape batchsize d0 dn ypred the predicted values shape batchsize d0 dn returns poisson loss values with shape batchsize d0 dn1 example ytrue np random randint0 2 size2 3 ypred np random randomsize2 3 loss keras losses poissonytrue ypred assert loss shape 2 ypred ypred 1e7 assert np allclose loss np meanypred ytrue np logypred axis1 atol1e5 computes the categorical crossentropy loss args ytrue tensor of onehot true targets ypred tensor of predicted targets fromlogits whether ypred is expected to be a logits tensor by default we assume that ypred encodes a probability distribution labelsmoothing float in 0 1 if 0 then smooth the labels for example if 0 1 use 0 1 numclasses for nontarget labels and 0 9 0 1 numclasses for target labels axis defaults to 1 the dimension along which the entropy is computed returns categorical crossentropy loss value example ytrue 0 1 0 0 0 1 ypred 0 05 0 95 0 0 1 0 8 0 1 loss keras losses categoricalcrossentropyytrue ypred assert loss shape 2 loss array0 0513 2 303 dtypefloat32 computes the categorical focal crossentropy loss args ytrue tensor of onehot true targets ypred tensor of predicted targets alpha a weight balancing factor for all classes default is 0 25 as mentioned in the reference it can be a list of floats or a scalar in the multiclass case alpha may be set by inverse class frequency by using computeclassweight from sklearn utils gamma a focusing parameter default is 2 0 as mentioned in the reference it helps to gradually reduce the importance given to simple examples in a smooth manner when gamma 0 there is no focal effect on the categorical crossentropy fromlogits whether ypred is expected to be a logits tensor by default we assume that ypred encodes a probability distribution labelsmoothing float in 0 1 if 0 then smooth the labels for example if 0 1 use 0 1 numclasses for nontarget labels and 0 9 0 1 numclasses for target labels axis defaults to 1 the dimension along which the entropy is computed returns categorical focal crossentropy loss value example ytrue 0 1 0 0 0 1 ypred 0 05 0 9 0 05 0 1 0 85 0 05 loss keras losses categoricalfocalcrossentropyytrue ypred assert loss shape 2 loss array2 63401289e04 6 75912094e01 dtypefloat32 adjust the predictions so that the probability of each class for every sample adds up to 1 this is needed to ensure that the cross entropy is computed correctly calculate cross entropy calculate factors apply weighting factor computes the sparse categorical crossentropy loss args ytrue ground truth values ypred the predicted values fromlogits whether ypred is expected to be a logits tensor by default we assume that ypred encodes a probability distribution ignoreclass optional integer the id of a class to be ignored during loss computation this is useful for example in segmentation problems featuring a void class commonly 1 or 255 in segmentation maps by default ignoreclassnone all classes are considered axis defaults to 1 the dimension along which the entropy is computed returns sparse categorical crossentropy loss value examples ytrue 1 2 ypred 0 05 0 95 0 0 1 0 8 0 1 loss keras losses sparsecategoricalcrossentropyytrue ypred assert loss shape 2 loss array0 0513 2 303 dtypefloat32 computes the binary crossentropy loss args ytrue ground truth values shape batchsize d0 dn ypred the predicted values shape batchsize d0 dn fromlogits whether ypred is expected to be a logits tensor by default we assume that ypred encodes a probability distribution labelsmoothing float in 0 1 if 0 then smooth the labels by squeezing them towards 0 5 that is using 1 0 5 labelsmoothing for the target class and 0 5 labelsmoothing for the nontarget class axis the axis along which the mean is computed defaults to 1 returns binary crossentropy loss value shape batchsize d0 dn1 example ytrue 0 1 0 0 ypred 0 6 0 4 0 4 0 6 loss keras losses binarycrossentropyytrue ypred assert loss shape 2 loss array0 916 0 714 dtypefloat32 computes the binary focal crossentropy loss according to lin et al 2018https arxiv orgpdf1708 02002 pdf it helps to apply a focal factor to downweight easy examples and focus more on hard examples by default the focal tensor is computed as follows focalfactor 1 output gamma for class 1 focalfactor output gamma for class 0 where gamma is a focusing parameter when gamma 0 there is no focal effect on the binary crossentropy loss if applyclassbalancing true this function also takes into account a weight balancing factor for the binary classes 0 and 1 as follows weight alpha for class 1 target 1 weight 1 alpha for class 0 where alpha is a float in the range of 0 1 args ytrue ground truth values of shape batchsize d0 dn ypred the predicted values of shape batchsize d0 dn applyclassbalancing a bool whether to apply weight balancing on the binary classes 0 and 1 alpha a weight balancing factor for class 1 default is 0 25 as mentioned in the reference the weight for class 0 is 1 0 alpha gamma a focusing parameter default is 2 0 as mentioned in the reference fromlogits whether ypred is expected to be a logits tensor by default we assume that ypred encodes a probability distribution labelsmoothing float in 0 1 if 0 then smooth the labels by squeezing them towards 0 5 that is using 1 0 5 labelsmoothing for the target class and 0 5 labelsmoothing for the nontarget class axis the axis along which the mean is computed defaults to 1 returns binary focal crossentropy loss value with shape batchsize d0 dn1 example ytrue 0 1 0 0 ypred 0 6 0 4 0 4 0 6 loss keras losses binaryfocalcrossentropy ytrue ypred gamma2 assert loss shape 2 loss array0 330 0 206 dtypefloat32 calculate focal factor computes the mean of squares of errors between labels and predictions formula python loss mean square y_true y_pred args reduction type of reduction to apply to the loss in almost all cases this should be sum_over_batch_size supported options are sum sum_over_batch_size or none name optional name for the loss instance computes the mean of absolute difference between labels and predictions formula python loss mean abs y_true y_pred args reduction type of reduction to apply to the loss in almost all cases this should be sum_over_batch_size supported options are sum sum_over_batch_size or none name optional name for the loss instance computes the mean absolute percentage error between y_true y_pred formula python loss 100 mean abs y_true y_pred y_true args reduction type of reduction to apply to the loss in almost all cases this should be sum_over_batch_size supported options are sum sum_over_batch_size or none name optional name for the loss instance computes the mean squared logarithmic error between y_true y_pred formula python loss mean square log y_true 1 log y_pred 1 args reduction type of reduction to apply to the loss in almost all cases this should be sum_over_batch_size supported options are sum sum_over_batch_size or none name optional name for the loss instance computes the cosine similarity between y_true y_pred note that it is a number between 1 and 1 when it is a negative number between 1 and 0 0 indicates orthogonality and values closer to 1 indicate greater similarity this makes it usable as a loss function in a setting where you try to maximize the proximity between predictions and targets if either y_true or y_pred is a zero vector cosine similarity will be 0 regardless of the proximity between predictions and targets formula python loss sum l2_norm y_true l2_norm y_pred args axis the axis along which the cosine similarity is computed the features axis defaults to 1 reduction type of reduction to apply to the loss in almost all cases this should be sum_over_batch_size supported options are sum sum_over_batch_size or none name optional name for the loss instance computes the huber loss between y_true y_pred formula python for x in error if abs x delta loss append 0 5 x 2 elif abs x delta loss append delta abs x 0 5 delta 2 loss mean loss axis 1 see huber loss https en wikipedia org wiki huber_loss args delta a float the point where the huber loss function changes from a quadratic to linear reduction type of reduction to apply to loss options are sum sum_over_batch_size or none defaults to sum_over_batch_size name optional name for the instance computes the logarithm of the hyperbolic cosine of the prediction error formula python error y_pred y_true logcosh mean log exp error exp error 2 axis 1 where x is the error y_pred y_true args reduction type of reduction to apply to loss options are sum sum_over_batch_size or none defaults to sum_over_batch_size name optional name for the instance computes the hinge loss between y_true y_pred formula python loss maximum 1 y_true y_pred 0 y_true values are expected to be 1 or 1 if binary 0 or 1 labels are provided we will convert them to 1 or 1 args reduction type of reduction to apply to the loss in almost all cases this should be sum_over_batch_size supported options are sum sum_over_batch_size or none name optional name for the loss instance computes the squared hinge loss between y_true y_pred formula python loss square maximum 1 y_true y_pred 0 y_true values are expected to be 1 or 1 if binary 0 or 1 labels are provided we will convert them to 1 or 1 args reduction type of reduction to apply to the loss in almost all cases this should be sum_over_batch_size supported options are sum sum_over_batch_size or none name optional name for the loss instance computes the categorical hinge loss between y_true y_pred formula python loss maximum neg pos 1 0 where neg maximum 1 y_true y_pred and pos sum y_true y_pred args reduction type of reduction to apply to the loss in almost all cases this should be sum_over_batch_size supported options are sum sum_over_batch_size or none name optional name for the loss instance computes kullback leibler divergence loss between y_true y_pred formula python loss y_true log y_true y_pred args reduction type of reduction to apply to the loss in almost all cases this should be sum_over_batch_size supported options are sum sum_over_batch_size or none name optional name for the loss instance computes the poisson loss between y_true y_pred formula python loss y_pred y_true log y_pred args reduction type of reduction to apply to the loss in almost all cases this should be sum_over_batch_size supported options are sum sum_over_batch_size or none name optional name for the loss instance computes the cross entropy loss between true labels and predicted labels use this cross entropy loss for binary 0 or 1 classification applications the loss function requires the following inputs y_true true label this is either 0 or 1 y_pred predicted value this is the model s prediction i e a single floating point value which either represents a logit https en wikipedia org wiki logit i e value in inf inf when from_logits true or a probability i e value in 0 1 when from_logits false args from_logits whether to interpret y_pred as a tensor of logit https en wikipedia org wiki logit values by default we assume that y_pred is probabilities i e values in 0 1 label_smoothing float in range 0 1 when 0 no smoothing occurs when 0 we compute the loss between the predicted labels and a smoothed version of the true labels where the smoothing squeezes the labels towards 0 5 larger values of label_smoothing correspond to heavier smoothing axis the axis along which to compute crossentropy the features axis defaults to 1 reduction type of reduction to apply to the loss in almost all cases this should be sum_over_batch_size supported options are sum sum_over_batch_size or none name optional name for the loss instance examples recommended usage set from_logits true with compile api python model compile loss keras losses binarycrossentropy from_logits true as a standalone function example 1 batch_size 1 number of samples 4 y_true 0 1 0 0 y_pred 18 6 0 51 2 94 12 8 bce keras losses binarycrossentropy from_logits true bce y_true y_pred 0 865 example 2 batch_size 2 number of samples 4 y_true 0 1 0 0 y_pred 18 6 0 51 2 94 12 8 using default auto sum_over_batch_size reduction type bce keras losses binarycrossentropy from_logits true bce y_true y_pred 0 865 using sample_weight attribute bce y_true y_pred sample_weight 0 8 0 2 0 243 using sum reduction type bce keras losses binarycrossentropy from_logits true reduction sum bce y_true y_pred 1 730 using none reduction type bce keras losses binarycrossentropy from_logits true reduction none bce y_true y_pred array 0 235 1 496 dtype float32 default usage set from_logits false make the following updates to the above recommended usage section 1 set from_logits false keras losses binarycrossentropy or from_logits false 2 update y_pred to use probabilities instead of logits y_pred 0 6 0 3 0 2 0 8 or 0 6 0 3 0 2 0 8 computes focal cross entropy loss between true labels and predictions binary cross entropy loss is often used for binary 0 or 1 classification tasks the loss function requires the following inputs y_true true label this is either 0 or 1 y_pred predicted value this is the model s prediction i e a single floating point value which either represents a logit https en wikipedia org wiki logit i e value in inf inf when from_logits true or a probability i e value in 0 1 when from_logits false according to lin et al 2018 https arxiv org pdf 1708 02002 pdf it helps to apply a focal factor to down weight easy examples and focus more on hard examples by default the focal tensor is computed as follows focal_factor 1 output gamma for class 1 focal_factor output gamma for class 0 where gamma is a focusing parameter when gamma 0 this function is equivalent to the binary crossentropy loss args apply_class_balancing a bool whether to apply weight balancing on the binary classes 0 and 1 alpha a weight balancing factor for class 1 default is 0 25 as mentioned in reference lin et al 2018 https arxiv org pdf 1708 02002 pdf the weight for class 0 is 1 0 alpha gamma a focusing parameter used to compute the focal factor default is 2 0 as mentioned in the reference lin et al 2018 https arxiv org pdf 1708 02002 pdf from_logits whether to interpret y_pred as a tensor of logit https en wikipedia org wiki logit values by default we assume that y_pred are probabilities i e values in 0 1 label_smoothing float in 0 1 when 0 no smoothing occurs when 0 we compute the loss between the predicted labels and a smoothed version of the true labels where the smoothing squeezes the labels towards 0 5 larger values of label_smoothing correspond to heavier smoothing axis the axis along which to compute crossentropy the features axis defaults to 1 reduction type of reduction to apply to the loss in almost all cases this should be sum_over_batch_size supported options are sum sum_over_batch_size or none name optional name for the loss instance examples with the compile api python model compile loss keras losses binaryfocalcrossentropy gamma 2 0 from_logits true as a standalone function example 1 batch_size 1 number of samples 4 y_true 0 1 0 0 y_pred 18 6 0 51 2 94 12 8 loss keras losses binaryfocalcrossentropy gamma 2 from_logits true loss y_true y_pred 0 691 apply class weight loss keras losses binaryfocalcrossentropy apply_class_balancing true gamma 2 from_logits true loss y_true y_pred 0 51 example 2 batch_size 2 number of samples 4 y_true 0 1 0 0 y_pred 18 6 0 51 2 94 12 8 using default auto sum_over_batch_size reduction type loss keras losses binaryfocalcrossentropy gamma 3 from_logits true loss y_true y_pred 0 647 apply class weight loss keras losses binaryfocalcrossentropy apply_class_balancing true gamma 3 from_logits true loss y_true y_pred 0 482 using sample_weight attribute with focal effect loss keras losses binaryfocalcrossentropy gamma 3 from_logits true loss y_true y_pred sample_weight 0 8 0 2 0 133 apply class weight loss keras losses binaryfocalcrossentropy apply_class_balancing true gamma 3 from_logits true loss y_true y_pred sample_weight 0 8 0 2 0 097 using sum reduction type loss keras losses binaryfocalcrossentropy gamma 4 from_logits true reduction sum loss y_true y_pred 1 222 apply class weight loss keras losses binaryfocalcrossentropy apply_class_balancing true gamma 4 from_logits true reduction sum loss y_true y_pred 0 914 using none reduction type loss keras losses binaryfocalcrossentropy gamma 5 from_logits true reduction none loss y_true y_pred array 0 0017 1 1561 dtype float32 apply class weight loss keras losses binaryfocalcrossentropy apply_class_balancing true gamma 5 from_logits true reduction none loss y_true y_pred array 0 0004 0 8670 dtype float32 computes the crossentropy loss between the labels and predictions use this crossentropy loss function when there are two or more label classes we expect labels to be provided in a one_hot representation if you want to provide labels as integers please use sparsecategoricalcrossentropy loss there should be num_classes floating point values per feature i e the shape of both y_pred and y_true are batch_size num_classes args from_logits whether y_pred is expected to be a logits tensor by default we assume that y_pred encodes a probability distribution label_smoothing float in 0 1 when 0 label values are smoothed meaning the confidence on label values are relaxed for example if 0 1 use 0 1 num_classes for non target labels and 0 9 0 1 num_classes for target labels axis the axis along which to compute crossentropy the features axis defaults to 1 reduction type of reduction to apply to the loss in almost all cases this should be sum_over_batch_size supported options are sum sum_over_batch_size or none name optional name for the loss instance examples standalone usage y_true 0 1 0 0 0 1 y_pred 0 05 0 95 0 0 1 0 8 0 1 using auto sum_over_batch_size reduction type cce keras losses categoricalcrossentropy cce y_true y_pred 1 177 calling with sample_weight cce y_true y_pred sample_weight np array 0 3 0 7 0 814 using sum reduction type cce keras losses categoricalcrossentropy reduction sum cce y_true y_pred 2 354 using none reduction type cce keras losses categoricalcrossentropy reduction none cce y_true y_pred array 0 0513 2 303 dtype float32 usage with the compile api python model compile optimizer sgd loss keras losses categoricalcrossentropy computes the alpha balanced focal crossentropy loss use this crossentropy loss function when there are two or more label classes and if you want to handle class imbalance without using class_weights we expect labels to be provided in a one_hot representation according to lin et al 2018 https arxiv org pdf 1708 02002 pdf it helps to apply a focal factor to down weight easy examples and focus more on hard examples the general formula for the focal loss fl is as follows fl p_t 1 p_t gamma log p_t where p_t is defined as follows p_t output if y_true 1 else 1 output 1 p_t gamma is the modulating_factor where gamma is a focusing parameter when gamma 0 there is no focal effect on the cross entropy gamma reduces the importance given to simple examples in a smooth manner the s use alpha balanced variant of focal loss fl in the paper fl p_t alpha 1 p_t gamma log p_t where alpha is the weight factor for the classes if alpha 1 the loss won t be able to handle class imbalance properly as all classes will have the same weight this can be a constant or a list of constants if alpha is a list it must have the same length as the number of classes the formula above can be generalized to fl p_t alpha 1 p_t gamma crossentropy y_true y_pred where minus comes from crossentropy y_true y_pred ce extending this to multi class case is straightforward fl p_t alpha 1 p_t gamma categoricalce y_true y_pred in the snippet below there is num_classes floating pointing values per example the shape of both y_pred and y_true are batch_size num_classes args alpha a weight balancing factor for all classes default is 0 25 as mentioned in the reference it can be a list of floats or a scalar in the multi class case alpha may be set by inverse class frequency by using compute_class_weight from sklearn utils gamma a focusing parameter default is 2 0 as mentioned in the reference it helps to gradually reduce the importance given to simple easy examples in a smooth manner from_logits whether output is expected to be a logits tensor by default we consider that output encodes a probability distribution label_smoothing float in 0 1 when 0 label values are smoothed meaning the confidence on label values are relaxed for example if 0 1 use 0 1 num_classes for non target labels and 0 9 0 1 num_classes for target labels axis the axis along which to compute crossentropy the features axis defaults to 1 reduction type of reduction to apply to the loss in almost all cases this should be sum_over_batch_size supported options are sum sum_over_batch_size or none name optional name for the loss instance examples standalone usage y_true 0 1 0 0 0 1 y_pred 0 05 0 95 0 0 1 0 8 0 1 using auto sum_over_batch_size reduction type cce keras losses categoricalfocalcrossentropy cce y_true y_pred 0 23315276 calling with sample_weight cce y_true y_pred sample_weight np array 0 3 0 7 0 1632 using sum reduction type cce keras losses categoricalfocalcrossentropy reduction sum cce y_true y_pred 0 46631 using none reduction type cce keras losses categoricalfocalcrossentropy reduction none cce y_true y_pred array 3 2058331e 05 4 6627346e 01 dtype float32 usage with the compile api python model compile optimizer adam loss keras losses categoricalfocalcrossentropy initializes categoricalfocalcrossentropy instance computes the crossentropy loss between the labels and predictions use this crossentropy loss function when there are two or more label classes we expect labels to be provided as integers if you want to provide labels using one hot representation please use categoricalcrossentropy loss there should be classes floating point values per feature for y_pred and a single floating point value per feature for y_true in the snippet below there is a single floating point value per example for y_true and num_classes floating pointing values per example for y_pred the shape of y_true is batch_size and the shape of y_pred is batch_size num_classes args from_logits whether y_pred is expected to be a logits tensor by default we assume that y_pred encodes a probability distribution reduction type of reduction to apply to the loss in almost all cases this should be sum_over_batch_size supported options are sum sum_over_batch_size or none name optional name for the loss instance examples y_true 1 2 y_pred 0 05 0 95 0 0 1 0 8 0 1 using auto sum_over_batch_size reduction type scce keras losses sparsecategoricalcrossentropy scce y_true y_pred 1 177 calling with sample_weight scce y_true y_pred sample_weight np array 0 3 0 7 0 814 using sum reduction type scce keras losses sparsecategoricalcrossentropy reduction sum scce y_true y_pred 2 354 using none reduction type scce keras losses sparsecategoricalcrossentropy reduction none scce y_true y_pred array 0 0513 2 303 dtype float32 usage with the compile api python model compile optimizer sgd loss keras losses sparsecategoricalcrossentropy converts binary labels into 1 1 for hinge loss metric calculation convert the binary labels to 1 or 1 returns the labels unchanged if they are non binary computes the hinge loss between y_true y_pred formula python loss mean maximum 1 y_true y_pred 0 axis 1 args y_true the ground truth values y_true values are expected to be 1 or 1 if binary 0 or 1 labels are provided they will be converted to 1 or 1 with shape batch_size d0 dn y_pred the predicted values with shape batch_size d0 dn returns hinge loss values with shape batch_size d0 dn 1 example y_true np random choice 1 1 size 2 3 y_pred np random random size 2 3 loss keras losses hinge y_true y_pred computes the squared hinge loss between y_true y_pred formula python loss mean square maximum 1 y_true y_pred 0 axis 1 args y_true the ground truth values y_true values are expected to be 1 or 1 if binary 0 or 1 labels are provided we will convert them to 1 or 1 with shape batch_size d0 dn y_pred the predicted values with shape batch_size d0 dn returns squared hinge loss values with shape batch_size d0 dn 1 example y_true np random choice 1 1 size 2 3 y_pred np random random size 2 3 loss keras losses squared_hinge y_true y_pred computes the categorical hinge loss between y_true y_pred formula python loss maximum neg pos 1 0 where neg maximum 1 y_true y_pred and pos sum y_true y_pred args y_true the ground truth values y_true values are expected to be either 1 1 or 0 1 i e a one hot encoded tensor with shape batch_size d0 dn y_pred the predicted values with shape batch_size d0 dn returns categorical hinge loss values with shape batch_size d0 dn 1 example y_true np random randint 0 3 size 2 y_true np eye np max y_true 1 y_true y_pred np random random size 2 3 loss keras losses categorical_hinge y_true y_pred legacy aliases computes the mean squared error between labels and predictions formula python loss mean square y_true y_pred axis 1 example y_true np random randint 0 2 size 2 3 y_pred np random random size 2 3 loss keras losses mean_squared_error y_true y_pred args y_true ground truth values with shape batch_size d0 dn y_pred the predicted values with shape batch_size d0 dn returns mean squared error values with shape batch_size d0 dn 1 legacy aliases computes the mean absolute error between labels and predictions python loss mean abs y_true y_pred axis 1 args y_true ground truth values with shape batch_size d0 dn y_pred the predicted values with shape batch_size d0 dn returns mean absolute error values with shape batch_size d0 dn 1 example y_true np random randint 0 2 size 2 3 y_pred np random random size 2 3 loss keras losses mean_absolute_error y_true y_pred legacy aliases computes the mean absolute percentage error between y_true y_pred formula python loss 100 mean abs y_true y_pred y_true axis 1 division by zero is prevented by dividing by maximum y_true epsilon where epsilon keras backend epsilon default to 1e 7 args y_true ground truth values with shape batch_size d0 dn y_pred the predicted values with shape batch_size d0 dn returns mean absolute percentage error values with shape batch_size d0 dn 1 example y_true np random random size 2 3 y_pred np random random size 2 3 loss keras losses mean_absolute_percentage_error y_true y_pred legacy aliases computes the mean squared logarithmic error between y_true y_pred formula python loss mean square log y_true 1 log y_pred 1 axis 1 note that y_pred and y_true cannot be less or equal to 0 negative values and 0 values will be replaced with keras backend epsilon default to 1e 7 args y_true ground truth values with shape batch_size d0 dn y_pred the predicted values with shape batch_size d0 dn returns mean squared logarithmic error values with shape batch_size d0 dn 1 example y_true np random randint 0 2 size 2 3 y_pred np random random size 2 3 loss keras losses mean_squared_logarithmic_error y_true y_pred computes the cosine similarity between labels and predictions formula python loss sum l2_norm y_true l2_norm y_pred note that it is a number between 1 and 1 when it is a negative number between 1 and 0 0 indicates orthogonality and values closer to 1 indicate greater similarity this makes it usable as a loss function in a setting where you try to maximize the proximity between predictions and targets if either y_true or y_pred is a zero vector cosine similarity will be 0 regardless of the proximity between predictions and targets args y_true tensor of true targets y_pred tensor of predicted targets axis axis along which to determine similarity defaults to 1 returns cosine similarity tensor example y_true 0 1 1 1 1 1 y_pred 1 0 1 1 1 1 loss keras losses cosine_similarity y_true y_pred axis 1 0 0 99999994 0 99999994 computes huber loss value formula python for x in error if abs x delta loss append 0 5 x 2 elif abs x delta loss append delta abs x 0 5 delta 2 loss mean loss axis 1 see huber loss https en wikipedia org wiki huber_loss example y_true 0 1 0 0 y_pred 0 6 0 4 0 4 0 6 loss keras losses huber y_true y_pred 0 155 args y_true tensor of true targets y_pred tensor of predicted targets delta a float the point where the huber loss function changes from a quadratic to linear defaults to 1 0 returns tensor with one scalar loss entry per sample legacy aliases logarithm of the hyperbolic cosine of the prediction error formula python loss mean log cosh y_pred y_true axis 1 note that log cosh x is approximately equal to x 2 2 for small x and to abs x log 2 for large x this means that logcosh works mostly like the mean squared error but will not be so strongly affected by the occasional wildly incorrect prediction example y_true 0 1 0 0 y_pred 1 1 0 0 loss keras losses log_cosh y_true y_pred 0 108 args y_true ground truth values with shape batch_size d0 dn y_pred the predicted values with shape batch_size d0 dn returns logcosh error values with shape batch_size d0 dn 1 legacy aliases computes kullback leibler divergence loss between y_true y_pred formula python loss y_true log y_true y_pred args y_true tensor of true targets y_pred tensor of predicted targets returns kl divergence loss values with shape batch_size d0 dn 1 example y_true np random randint 0 2 size 2 3 astype np float32 y_pred np random random size 2 3 loss keras losses kl_divergence y_true y_pred assert loss shape 2 y_true ops clip y_true 1e 7 1 y_pred ops clip y_pred 1e 7 1 assert np array_equal loss np sum y_true np log y_true y_pred axis 1 computes the poisson loss between y_true and y_pred formula python loss y_pred y_true log y_pred args y_true ground truth values shape batch_size d0 dn y_pred the predicted values shape batch_size d0 dn returns poisson loss values with shape batch_size d0 dn 1 example y_true np random randint 0 2 size 2 3 y_pred np random random size 2 3 loss keras losses poisson y_true y_pred assert loss shape 2 y_pred y_pred 1e 7 assert np allclose loss np mean y_pred y_true np log y_pred axis 1 atol 1e 5 computes the categorical crossentropy loss args y_true tensor of one hot true targets y_pred tensor of predicted targets from_logits whether y_pred is expected to be a logits tensor by default we assume that y_pred encodes a probability distribution label_smoothing float in 0 1 if 0 then smooth the labels for example if 0 1 use 0 1 num_classes for non target labels and 0 9 0 1 num_classes for target labels axis defaults to 1 the dimension along which the entropy is computed returns categorical crossentropy loss value example y_true 0 1 0 0 0 1 y_pred 0 05 0 95 0 0 1 0 8 0 1 loss keras losses categorical_crossentropy y_true y_pred assert loss shape 2 loss array 0 0513 2 303 dtype float32 computes the categorical focal crossentropy loss args y_true tensor of one hot true targets y_pred tensor of predicted targets alpha a weight balancing factor for all classes default is 0 25 as mentioned in the reference it can be a list of floats or a scalar in the multi class case alpha may be set by inverse class frequency by using compute_class_weight from sklearn utils gamma a focusing parameter default is 2 0 as mentioned in the reference it helps to gradually reduce the importance given to simple examples in a smooth manner when gamma 0 there is no focal effect on the categorical crossentropy from_logits whether y_pred is expected to be a logits tensor by default we assume that y_pred encodes a probability distribution label_smoothing float in 0 1 if 0 then smooth the labels for example if 0 1 use 0 1 num_classes for non target labels and 0 9 0 1 num_classes for target labels axis defaults to 1 the dimension along which the entropy is computed returns categorical focal crossentropy loss value example y_true 0 1 0 0 0 1 y_pred 0 05 0 9 0 05 0 1 0 85 0 05 loss keras losses categorical_focal_crossentropy y_true y_pred assert loss shape 2 loss array 2 63401289e 04 6 75912094e 01 dtype float32 adjust the predictions so that the probability of each class for every sample adds up to 1 this is needed to ensure that the cross entropy is computed correctly calculate cross entropy calculate factors apply weighting factor computes the sparse categorical crossentropy loss args y_true ground truth values y_pred the predicted values from_logits whether y_pred is expected to be a logits tensor by default we assume that y_pred encodes a probability distribution ignore_class optional integer the id of a class to be ignored during loss computation this is useful for example in segmentation problems featuring a void class commonly 1 or 255 in segmentation maps by default ignore_class none all classes are considered axis defaults to 1 the dimension along which the entropy is computed returns sparse categorical crossentropy loss value examples y_true 1 2 y_pred 0 05 0 95 0 0 1 0 8 0 1 loss keras losses sparse_categorical_crossentropy y_true y_pred assert loss shape 2 loss array 0 0513 2 303 dtype float32 computes the binary crossentropy loss args y_true ground truth values shape batch_size d0 dn y_pred the predicted values shape batch_size d0 dn from_logits whether y_pred is expected to be a logits tensor by default we assume that y_pred encodes a probability distribution label_smoothing float in 0 1 if 0 then smooth the labels by squeezing them towards 0 5 that is using 1 0 5 label_smoothing for the target class and 0 5 label_smoothing for the non target class axis the axis along which the mean is computed defaults to 1 returns binary crossentropy loss value shape batch_size d0 dn 1 example y_true 0 1 0 0 y_pred 0 6 0 4 0 4 0 6 loss keras losses binary_crossentropy y_true y_pred assert loss shape 2 loss array 0 916 0 714 dtype float32 computes the binary focal crossentropy loss according to lin et al 2018 https arxiv org pdf 1708 02002 pdf it helps to apply a focal factor to down weight easy examples and focus more on hard examples by default the focal tensor is computed as follows focal_factor 1 output gamma for class 1 focal_factor output gamma for class 0 where gamma is a focusing parameter when gamma 0 there is no focal effect on the binary crossentropy loss if apply_class_balancing true this function also takes into account a weight balancing factor for the binary classes 0 and 1 as follows weight alpha for class 1 target 1 weight 1 alpha for class 0 where alpha is a float in the range of 0 1 args y_true ground truth values of shape batch_size d0 dn y_pred the predicted values of shape batch_size d0 dn apply_class_balancing a bool whether to apply weight balancing on the binary classes 0 and 1 alpha a weight balancing factor for class 1 default is 0 25 as mentioned in the reference the weight for class 0 is 1 0 alpha gamma a focusing parameter default is 2 0 as mentioned in the reference from_logits whether y_pred is expected to be a logits tensor by default we assume that y_pred encodes a probability distribution label_smoothing float in 0 1 if 0 then smooth the labels by squeezing them towards 0 5 that is using 1 0 5 label_smoothing for the target class and 0 5 label_smoothing for the non target class axis the axis along which the mean is computed defaults to 1 returns binary focal crossentropy loss value with shape batch_size d0 dn 1 example y_true 0 1 0 0 y_pred 0 6 0 4 0 4 0 6 loss keras losses binary_focal_crossentropy y_true y_pred gamma 2 assert loss shape 2 loss array 0 330 0 206 dtype float32 calculate focal factor
import warnings

from keras import backend
from keras import ops
from keras.api_export import keras_export
from keras.losses.loss import Loss
from keras.losses.loss import squeeze_to_same_rank
from keras.saving import serialization_lib
from keras.utils.numerical_utils import normalize


class LossFunctionWrapper(Loss):
    def __init__(
        self, fn, reduction="sum_over_batch_size", name=None, **kwargs
    ):
        super().__init__(reduction=reduction, name=name)
        self.fn = fn
        self._fn_kwargs = kwargs

    def call(self, y_true, y_pred):
        y_true, y_pred = squeeze_to_same_rank(y_true, y_pred)
        return self.fn(y_true, y_pred, **self._fn_kwargs)

    def get_config(self):
        base_config = super().get_config()
        config = {"fn": serialization_lib.serialize_keras_object(self.fn)}
        config.update(serialization_lib.serialize_keras_object(self._fn_kwargs))
        return {**base_config, **config}

    @classmethod
    def from_config(cls, config):
        if "fn" in config:
            config = serialization_lib.deserialize_keras_object(config)
        return cls(**config)


@keras_export("keras.losses.MeanSquaredError")
class MeanSquaredError(LossFunctionWrapper):
    

    def __init__(
        self, reduction="sum_over_batch_size", name="mean_squared_error"
    ):
        super().__init__(mean_squared_error, reduction=reduction, name=name)

    def get_config(self):
        return Loss.get_config(self)


@keras_export("keras.losses.MeanAbsoluteError")
class MeanAbsoluteError(LossFunctionWrapper):
    

    def __init__(
        self, reduction="sum_over_batch_size", name="mean_absolute_error"
    ):
        super().__init__(mean_absolute_error, reduction=reduction, name=name)

    def get_config(self):
        return Loss.get_config(self)


@keras_export("keras.losses.MeanAbsolutePercentageError")
class MeanAbsolutePercentageError(LossFunctionWrapper):
    

    def __init__(
        self,
        reduction="sum_over_batch_size",
        name="mean_absolute_percentage_error",
    ):
        super().__init__(
            mean_absolute_percentage_error, reduction=reduction, name=name
        )

    def get_config(self):
        return Loss.get_config(self)


@keras_export("keras.losses.MeanSquaredLogarithmicError")
class MeanSquaredLogarithmicError(LossFunctionWrapper):
    

    def __init__(
        self,
        reduction="sum_over_batch_size",
        name="mean_squared_logarithmic_error",
    ):
        super().__init__(
            mean_squared_logarithmic_error, reduction=reduction, name=name
        )

    def get_config(self):
        return Loss.get_config(self)


@keras_export("keras.losses.CosineSimilarity")
class CosineSimilarity(LossFunctionWrapper):
    

    def __init__(
        self,
        axis=-1,
        reduction="sum_over_batch_size",
        name="cosine_similarity",
    ):
        super().__init__(
            cosine_similarity, reduction=reduction, name=name, axis=axis
        )

    def get_config(self):
        return Loss.get_config(self)


@keras_export("keras.losses.Huber")
class Huber(LossFunctionWrapper):
    

    def __init__(
        self,
        delta=1.0,
        reduction="sum_over_batch_size",
        name="huber_loss",
    ):
        super().__init__(huber, name=name, reduction=reduction, delta=delta)

    def get_config(self):
        return Loss.get_config(self)


@keras_export("keras.losses.LogCosh")
class LogCosh(LossFunctionWrapper):
    

    def __init__(self, reduction="sum_over_batch_size", name="log_cosh"):
        super().__init__(log_cosh, name=name, reduction=reduction)

    def get_config(self):
        return Loss.get_config(self)


@keras_export("keras.losses.Hinge")
class Hinge(LossFunctionWrapper):
    

    def __init__(self, reduction="sum_over_batch_size", name="hinge"):
        super().__init__(hinge, reduction=reduction, name=name)

    def get_config(self):
        return Loss.get_config(self)


@keras_export("keras.losses.SquaredHinge")
class SquaredHinge(LossFunctionWrapper):
    

    def __init__(self, reduction="sum_over_batch_size", name="squared_hinge"):
        super().__init__(squared_hinge, reduction=reduction, name=name)

    def get_config(self):
        return Loss.get_config(self)


@keras_export("keras.losses.CategoricalHinge")
class CategoricalHinge(LossFunctionWrapper):
    

    def __init__(
        self, reduction="sum_over_batch_size", name="categorical_hinge"
    ):
        super().__init__(categorical_hinge, reduction=reduction, name=name)

    def get_config(self):
        return Loss.get_config(self)


@keras_export("keras.losses.KLDivergence")
class KLDivergence(LossFunctionWrapper):
    

    def __init__(self, reduction="sum_over_batch_size", name="kl_divergence"):
        super().__init__(kl_divergence, reduction=reduction, name=name)

    def get_config(self):
        return Loss.get_config(self)


@keras_export("keras.losses.Poisson")
class Poisson(LossFunctionWrapper):
    

    def __init__(self, reduction="sum_over_batch_size", name="poisson"):
        super().__init__(poisson, reduction=reduction, name=name)

    def get_config(self):
        return Loss.get_config(self)


@keras_export("keras.losses.BinaryCrossentropy")
class BinaryCrossentropy(LossFunctionWrapper):
    

    def __init__(
        self,
        from_logits=False,
        label_smoothing=0.0,
        axis=-1,
        reduction="sum_over_batch_size",
        name="binary_crossentropy",
    ):
        super().__init__(
            binary_crossentropy,
            name=name,
            reduction=reduction,
            from_logits=from_logits,
            label_smoothing=label_smoothing,
            axis=axis,
        )
        self.from_logits = from_logits
        self.label_smoothing = label_smoothing
        self.axis = axis

    def get_config(self):
        return {
            "name": self.name,
            "reduction": self.reduction,
            "from_logits": self.from_logits,
            "label_smoothing": self.label_smoothing,
            "axis": self.axis,
        }


@keras_export("keras.losses.BinaryFocalCrossentropy")
class BinaryFocalCrossentropy(LossFunctionWrapper):
    

    def __init__(
        self,
        apply_class_balancing=False,
        alpha=0.25,
        gamma=2.0,
        from_logits=False,
        label_smoothing=0.0,
        axis=-1,
        reduction="sum_over_batch_size",
        name="binary_focal_crossentropy",
    ):
        super().__init__(
            binary_focal_crossentropy,
            apply_class_balancing=apply_class_balancing,
            alpha=alpha,
            gamma=gamma,
            name=name,
            reduction=reduction,
            from_logits=from_logits,
            label_smoothing=label_smoothing,
            axis=axis,
        )
        self.from_logits = from_logits
        self.label_smoothing = label_smoothing
        self.axis = axis
        self.apply_class_balancing = apply_class_balancing
        self.alpha = alpha
        self.gamma = gamma

    def get_config(self):
        return {
            "name": self.name,
            "reduction": self.reduction,
            "from_logits": self.from_logits,
            "label_smoothing": self.label_smoothing,
            "axis": self.axis,
            "apply_class_balancing": self.apply_class_balancing,
            "alpha": self.alpha,
            "gamma": self.gamma,
        }


@keras_export("keras.losses.CategoricalCrossentropy")
class CategoricalCrossentropy(LossFunctionWrapper):
    

    def __init__(
        self,
        from_logits=False,
        label_smoothing=0.0,
        axis=-1,
        reduction="sum_over_batch_size",
        name="categorical_crossentropy",
    ):
        super().__init__(
            categorical_crossentropy,
            name=name,
            reduction=reduction,
            from_logits=from_logits,
            label_smoothing=label_smoothing,
            axis=axis,
        )
        self.from_logits = from_logits
        self.label_smoothing = label_smoothing
        self.axis = axis

    def get_config(self):
        return {
            "name": self.name,
            "reduction": self.reduction,
            "from_logits": self.from_logits,
            "label_smoothing": self.label_smoothing,
            "axis": self.axis,
        }


@keras_export("keras.losses.CategoricalFocalCrossentropy")
class CategoricalFocalCrossentropy(LossFunctionWrapper):
    

    def __init__(
        self,
        alpha=0.25,
        gamma=2.0,
        from_logits=False,
        label_smoothing=0.0,
        axis=-1,
        reduction="sum_over_batch_size",
        name="categorical_focal_crossentropy",
    ):
        
        super().__init__(
            categorical_focal_crossentropy,
            alpha=alpha,
            gamma=gamma,
            name=name,
            reduction=reduction,
            from_logits=from_logits,
            label_smoothing=label_smoothing,
            axis=axis,
        )
        self.from_logits = from_logits
        self.label_smoothing = label_smoothing
        self.axis = axis
        self.alpha = alpha
        self.gamma = gamma

    def get_config(self):
        return {
            "name": self.name,
            "reduction": self.reduction,
            "from_logits": self.from_logits,
            "label_smoothing": self.label_smoothing,
            "axis": self.axis,
            "alpha": self.alpha,
            "gamma": self.gamma,
        }


@keras_export("keras.losses.SparseCategoricalCrossentropy")
class SparseCategoricalCrossentropy(LossFunctionWrapper):
    

    def __init__(
        self,
        from_logits=False,
        ignore_class=None,
        reduction="sum_over_batch_size",
        name="sparse_categorical_crossentropy",
    ):
        super().__init__(
            sparse_categorical_crossentropy,
            name=name,
            reduction=reduction,
            from_logits=from_logits,
            ignore_class=ignore_class,
        )
        self.from_logits = from_logits
        self.ignore_class = ignore_class

    def get_config(self):
        return {
            "name": self.name,
            "reduction": self.reduction,
            "from_logits": self.from_logits,
            "ignore_class": self.ignore_class,
        }


def convert_binary_labels_to_hinge(y_true):
    
    are_zeros = ops.equal(y_true, 0)
    are_ones = ops.equal(y_true, 1)
    is_binary = ops.all((ops.logical_or(are_zeros, are_ones)))

    def _convert_binary_labels():
        
        return 2.0 * y_true - 1.0

    def _return_labels_unconverted():
        
        return y_true

    updated_y_true = ops.cond(
        is_binary, _convert_binary_labels, _return_labels_unconverted
    )
    return updated_y_true


@keras_export(
    [
        "keras.metrics.hinge",
        "keras.losses.hinge",
    ]
)
def hinge(y_true, y_pred):
    
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.cast(y_true, dtype=y_pred.dtype)
    y_true = ops.convert_to_tensor(y_true)
    y_true = convert_binary_labels_to_hinge(y_true)
    return ops.mean(ops.maximum(1.0 - y_true * y_pred, 0.0), axis=-1)


@keras_export(
    [
        "keras.metrics.squared_hinge",
        "keras.losses.squared_hinge",
    ]
)
def squared_hinge(y_true, y_pred):
    
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.cast(y_true, y_pred.dtype)
    y_true = convert_binary_labels_to_hinge(y_true)
    return ops.mean(
        ops.square(ops.maximum(1.0 - y_true * y_pred, 0.0)), axis=-1
    )


@keras_export(
    [
        "keras.metrics.categorical_hinge",
        "keras.losses.categorical_hinge",
    ]
)
def categorical_hinge(y_true, y_pred):
    
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.cast(y_true, y_pred.dtype)
    pos = ops.sum(y_true * y_pred, axis=-1)
    neg = ops.max((1.0 - y_true) * y_pred, axis=-1)
    zero = ops.cast(0.0, y_pred.dtype)
    return ops.maximum(neg - pos + 1.0, zero)


@keras_export(
    [
        "keras.metrics.mean_squared_error",
        "keras.losses.mean_squared_error",
        
        "keras._legacy.losses.mse",
        "keras._legacy.losses.MSE",
        "keras._legacy.metrics.mse",
        "keras._legacy.metrics.MSE",
    ]
)
def mean_squared_error(y_true, y_pred):
    
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.convert_to_tensor(y_true, dtype=y_pred.dtype)
    y_true, y_pred = squeeze_to_same_rank(y_true, y_pred)
    return ops.mean(ops.square(y_true - y_pred), axis=-1)


@keras_export(
    [
        "keras.metrics.mean_absolute_error",
        "keras.losses.mean_absolute_error",
        
        "keras._legacy.losses.MAE",
        "keras._legacy.losses.mae",
        "keras._legacy.metrics.MAE",
        "keras._legacy.metrics.mae",
    ]
)
def mean_absolute_error(y_true, y_pred):
    
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.convert_to_tensor(y_true, dtype=y_pred.dtype)
    y_true, y_pred = squeeze_to_same_rank(y_true, y_pred)
    return ops.mean(ops.abs(y_true - y_pred), axis=-1)


@keras_export(
    [
        "keras.metrics.mean_absolute_percentage_error",
        "keras.losses.mean_absolute_percentage_error",
        
        "keras._legacy.losses.mape",
        "keras._legacy.losses.MAPE",
        "keras._legacy.metrics.mape",
        "keras._legacy.metrics.MAPE",
    ]
)
def mean_absolute_percentage_error(y_true, y_pred):
    
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.convert_to_tensor(y_true, dtype=y_pred.dtype)
    epsilon = ops.convert_to_tensor(backend.epsilon())
    y_true, y_pred = squeeze_to_same_rank(y_true, y_pred)
    diff = ops.abs((y_true - y_pred) / ops.maximum(ops.abs(y_true), epsilon))
    return 100.0 * ops.mean(diff, axis=-1)


@keras_export(
    [
        "keras.metrics.mean_squared_logarithmic_error",
        "keras.losses.mean_squared_logarithmic_error",
        
        "keras._legacy.losses.msle",
        "keras._legacy.losses.MSLE",
        "keras._legacy.metrics.msle",
        "keras._legacy.metrics.MSLE",
    ]
)
def mean_squared_logarithmic_error(y_true, y_pred):
    
    epsilon = ops.convert_to_tensor(backend.epsilon())
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.convert_to_tensor(y_true, dtype=y_pred.dtype)
    y_true, y_pred = squeeze_to_same_rank(y_true, y_pred)
    first_log = ops.log(ops.maximum(y_pred, epsilon) + 1.0)
    second_log = ops.log(ops.maximum(y_true, epsilon) + 1.0)
    return ops.mean(ops.square(first_log - second_log), axis=-1)


@keras_export("keras.losses.cosine_similarity")
def cosine_similarity(y_true, y_pred, axis=-1):
    
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.convert_to_tensor(y_true, dtype=y_pred.dtype)
    y_true, y_pred = squeeze_to_same_rank(y_true, y_pred)
    y_pred = normalize(y_pred, axis=axis)
    y_true = normalize(y_true, axis=axis)
    return -ops.sum(y_true * y_pred, axis=axis)


@keras_export(["keras.losses.huber", "keras.metrics.huber"])
def huber(y_true, y_pred, delta=1.0):
    
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.convert_to_tensor(y_true, dtype=y_pred.dtype)
    y_true, y_pred = squeeze_to_same_rank(y_true, y_pred)
    delta = ops.convert_to_tensor(delta)
    error = ops.subtract(y_pred, y_true)
    abs_error = ops.abs(error)
    half = ops.convert_to_tensor(0.5, dtype=abs_error.dtype)
    return ops.mean(
        ops.where(
            abs_error <= delta,
            half * ops.square(error),
            delta * abs_error - half * ops.square(delta),
        ),
        axis=-1,
    )


@keras_export(
    [
        "keras.losses.log_cosh",
        "keras.metrics.log_cosh",
        
        "keras._legacy.losses.logcosh",
        "keras._legacy.metrics.logcosh",
    ]
)
def log_cosh(y_true, y_pred):
    
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.convert_to_tensor(y_true, dtype=y_pred.dtype)
    y_true, y_pred = squeeze_to_same_rank(y_true, y_pred)
    log2 = ops.convert_to_tensor(ops.log(2.0), dtype=y_pred.dtype)

    def _logcosh(x):
        return x + ops.softplus(x * -2.0) - log2

    return ops.mean(_logcosh(y_pred - y_true), axis=-1)


@keras_export(
    [
        "keras.metrics.kl_divergence",
        "keras.losses.kl_divergence",
        
        "keras._legacy.losses.KLD",
        "keras._legacy.losses.kld",
        "keras._legacy.losses.kullback_leibler_divergence",
        "keras._legacy.metrics.KLD",
        "keras._legacy.metrics.kld",
        "keras._legacy.metrics.kullback_leibler_divergence",
    ]
)
def kl_divergence(y_true, y_pred):
    
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.convert_to_tensor(y_true, y_pred.dtype)
    y_true = ops.clip(y_true, backend.epsilon(), 1)
    y_pred = ops.clip(y_pred, backend.epsilon(), 1)
    return ops.sum(y_true * ops.log(y_true / y_pred), axis=-1)


@keras_export(
    [
        "keras.metrics.poisson",
        "keras.losses.poisson",
    ]
)
def poisson(y_true, y_pred):
    
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.convert_to_tensor(y_true, dtype=y_pred.dtype)
    epsilon = ops.convert_to_tensor(backend.epsilon())
    return ops.mean(y_pred - y_true * ops.log(y_pred + epsilon), axis=-1)


@keras_export(
    [
        "keras.metrics.categorical_crossentropy",
        "keras.losses.categorical_crossentropy",
    ]
)
def categorical_crossentropy(
    y_true, y_pred, from_logits=False, label_smoothing=0.0, axis=-1
):
    
    if isinstance(axis, bool):
        raise ValueError(
            "`axis` must be of type `int`. "
            f"Received: axis={axis} of type {type(axis)}"
        )
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.cast(y_true, y_pred.dtype)

    if y_pred.shape[-1] == 1:
        warnings.warn(
            "In loss categorical_crossentropy, expected "
            "y_pred.shape to be (batch_size, num_classes) "
            f"with num_classes > 1. Received: y_pred.shape={y_pred.shape}. "
            "Consider using 'binary_crossentropy' if you only have 2 classes.",
            SyntaxWarning,
            stacklevel=2,
        )

    if label_smoothing:
        num_classes = ops.cast(ops.shape(y_true)[-1], y_pred.dtype)
        y_true = y_true * (1.0 - label_smoothing) + (
            label_smoothing / num_classes
        )

    return ops.categorical_crossentropy(
        y_true, y_pred, from_logits=from_logits, axis=axis
    )


@keras_export(
    [
        "keras.metrics.categorical_focal_crossentropy",
        "keras.losses.categorical_focal_crossentropy",
    ]
)
def categorical_focal_crossentropy(
    y_true,
    y_pred,
    alpha=0.25,
    gamma=2.0,
    from_logits=False,
    label_smoothing=0.0,
    axis=-1,
):
    
    if isinstance(axis, bool):
        raise ValueError(
            "`axis` must be of type `int`. "
            f"Received: axis={axis} of type {type(axis)}"
        )
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.cast(y_true, y_pred.dtype)

    if y_pred.shape[-1] == 1:
        warnings.warn(
            "In loss categorical_focal_crossentropy, expected "
            "y_pred.shape to be (batch_size, num_classes) "
            f"with num_classes > 1. Received: y_pred.shape={y_pred.shape}. "
            "Consider using 'binary_crossentropy' if you only have 2 classes.",
            SyntaxWarning,
            stacklevel=2,
        )

    if label_smoothing:
        num_classes = ops.cast(ops.shape(y_true)[-1], y_pred.dtype)
        y_true = y_true * (1.0 - label_smoothing) + (
            label_smoothing / num_classes
        )

    if from_logits:
        y_pred = ops.softmax(y_pred, axis=axis)

    
    
    
    
    output = y_pred / ops.sum(y_pred, axis=axis, keepdims=True)
    output = ops.clip(output, backend.epsilon(), 1.0 - backend.epsilon())

    
    cce = -y_true * ops.log(output)

    
    modulating_factor = ops.power(1.0 - output, gamma)
    weighting_factor = ops.multiply(modulating_factor, alpha)

    
    focal_cce = ops.multiply(weighting_factor, cce)
    focal_cce = ops.sum(focal_cce, axis=axis)
    return focal_cce


@keras_export(
    [
        "keras.metrics.sparse_categorical_crossentropy",
        "keras.losses.sparse_categorical_crossentropy",
    ]
)
def sparse_categorical_crossentropy(
    y_true, y_pred, from_logits=False, ignore_class=None, axis=-1
):
    

    if ignore_class is not None:
        res_shape = ops.shape(y_pred)[:-1]
        valid_mask = ops.not_equal(y_true, ops.cast(ignore_class, y_pred.dtype))
        y_true = y_true * ops.cast(valid_mask, y_true.dtype)
        y_pred = y_pred * ops.cast(
            ops.expand_dims(valid_mask, -1), y_pred.dtype
        )

    res = ops.sparse_categorical_crossentropy(
        y_true,
        y_pred,
        from_logits=from_logits,
        axis=axis,
    )

    if ignore_class is not None:
        valid_mask = ops.reshape(valid_mask, res_shape)
        res = ops.where(valid_mask, res, 0.0)

        try:
            res._keras_mask = valid_mask
        except AttributeError:
            pass

    return res


@keras_export(
    [
        "keras.metrics.binary_crossentropy",
        "keras.losses.binary_crossentropy",
    ]
)
def binary_crossentropy(
    y_true, y_pred, from_logits=False, label_smoothing=0.0, axis=-1
):
    
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.cast(y_true, y_pred.dtype)

    if label_smoothing:
        y_true = y_true * (1.0 - label_smoothing) + 0.5 * label_smoothing

    return ops.mean(
        ops.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
        axis=axis,
    )


@keras_export(
    [
        "keras.metrics.binary_focal_crossentropy",
        "keras.losses.binary_focal_crossentropy",
    ]
)
def binary_focal_crossentropy(
    y_true,
    y_pred,
    apply_class_balancing=False,
    alpha=0.25,
    gamma=2.0,
    from_logits=False,
    label_smoothing=0.0,
    axis=-1,
):
    
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.cast(y_true, y_pred.dtype)

    if label_smoothing:
        y_true = y_true * (1.0 - label_smoothing) + 0.5 * label_smoothing

    if from_logits:
        y_pred = ops.sigmoid(y_pred)

    bce = ops.binary_crossentropy(
        target=y_true,
        output=y_pred,
        from_logits=False,
    )

    
    p_t = y_true * y_pred + (1 - y_true) * (1 - y_pred)
    focal_factor = ops.power(1.0 - p_t, gamma)

    focal_bce = focal_factor * bce

    if apply_class_balancing:
        weight = y_true * alpha + (1 - y_true) * (1 - alpha)
        focal_bce = weight * focal_bce

    return ops.mean(focal_bce, axis=axis)