fn: The loss function to wrap, with signature `fn(y_true, y_pred,. **kwargs)`. reduction: Type of `tf.keras.losses.Reduction` to apply to.
Skiptocontent
{{message}}
keras-team
/
keras
Public
Notifications
Fork
19.2k
Star
56.4k
Code
Issues
251
Pullrequests
78
Actions
Projects
1
Wiki
Security
Insights
More
Code
Issues
Pullrequests
Actions
Projects
Wiki
Security
Insights
Permalink
master
Branches
Tags
Couldnotloadbranches
Nothingtoshow
{{refName}}
default
Couldnotloadtags
Nothingtoshow
{{refName}}
default
Atagalreadyexistswiththeprovidedbranchname.ManyGitcommandsacceptbothtagandbranchnames,socreatingthisbranchmaycauseunexpectedbehavior.Areyousureyouwanttocreatethisbranch?
keras/keras/losses.py
/
Jumpto
Loss
Class
__init__
Function
_set_name_scope
Function
__call__
Function
from_config
Function
get_config
Function
call
Function
_get_reduction
Function
LossFunctionWrapper
Class
__init__
Function
call
Function
get_config
Function
from_config
Function
MeanSquaredError
Class
__init__
Function
MeanAbsoluteError
Class
__init__
Function
MeanAbsolutePercentageError
Class
__init__
Function
MeanSquaredLogarithmicError
Class
__init__
Function
BinaryCrossentropy
Class
__init__
Function
BinaryFocalCrossentropy
Class
__init__
Function
get_config
Function
CategoricalCrossentropy
Class
__init__
Function
SparseCategoricalCrossentropy
Class
__init__
Function
Hinge
Class
__init__
Function
SquaredHinge
Class
__init__
Function
CategoricalHinge
Class
__init__
Function
Poisson
Class
__init__
Function
LogCosh
Class
__init__
Function
KLDivergence
Class
__init__
Function
Huber
Class
__init__
Function
mean_squared_error
Function
_ragged_tensor_apply_loss
Function
rt_is_equiv_dense
Function
_convert_to_dense
Function
_call_loss
Function
_wrapper
Function
_ragged_tensor_mse
Function
mean_absolute_error
Function
_ragged_tensor_mae
Function
mean_absolute_percentage_error
Function
_ragged_tensor_mape
Function
mean_squared_logarithmic_error
Function
_ragged_tensor_msle
Function
_maybe_convert_labels
Function
_convert_binary_labels
Function
squared_hinge
Function
hinge
Function
categorical_hinge
Function
huber
Function
log_cosh
Function
_logcosh
Function
categorical_crossentropy
Function
_smooth_labels
Function
_ragged_tensor_categorical_crossentropy
Function
sparse_categorical_crossentropy
Function
_ragged_tensor_sparse_categorical_crossentropy
Function
binary_crossentropy
Function
_smooth_labels
Function
_ragged_tensor_binary_crossentropy
Function
binary_focal_crossentropy
Function
_smooth_labels
Function
_ragged_tensor_binary_focal_crossentropy
Function
kl_divergence
Function
poisson
Function
cosine_similarity
Function
CosineSimilarity
Class
__init__
Function
is_categorical_crossentropy
Function
serialize
Function
deserialize
Function
get
Function
Gotofile
Gotofile
T
Gotoline
L
Gotodefinition
R
Copypath
Copypermalink
Thiscommitdoesnotbelongtoanybranchonthisrepository,andmaybelongtoaforkoutsideoftherepository.
tensorflower-gardener
Mergepullrequest#17159fromKarahanS:master
…
Latestcommit
57b0fe4
Oct17,2022
History
PiperOrigin-RevId:481677610
26
contributors
Userswhohavecontributedtothisfile
+14
2663lines(2197sloc)
94.9KB
Raw
Blame
Editthisfile
E
OpeninGitHubDesktop
OpenwithDesktop
Viewraw
Viewblame
ThisfilecontainsbidirectionalUnicodetextthatmaybeinterpretedorcompileddifferentlythanwhatappearsbelow.Toreview,openthefileinaneditorthatrevealshiddenUnicodecharacters.
LearnmoreaboutbidirectionalUnicodecharacters
Showhiddencharacters
#Copyright2015TheTensorFlowAuthors.AllRightsReserved.
#
#LicensedundertheApacheLicense,Version2.0(the"License");
#youmaynotusethisfileexceptincompliancewiththeLicense.
#YoumayobtainacopyoftheLicenseat
#
#http://www.apache.org/licenses/LICENSE-2.0
#
#Unlessrequiredbyapplicablelaworagreedtoinwriting,software
#distributedundertheLicenseisdistributedonan"ASIS"BASIS,
#WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
#SeetheLicenseforthespecificlanguagegoverningpermissionsand
#limitationsundertheLicense.
#==============================================================================
"""Built-inlossfunctions."""
importabc
importfunctools
importtensorflow.compat.v2astf
fromkerasimportbackend
fromkeras.saving.experimentalimportsaving_lib
fromkeras.saving.legacy.serializationimportdeserialize_keras_object
fromkeras.saving.legacy.serializationimportserialize_keras_object
fromkeras.utilsimportlosses_utils
fromkeras.utilsimporttf_utils
#isort:off
fromtensorflow.python.ops.raggedimportragged_map_ops
fromtensorflow.python.ops.raggedimportragged_util
fromtensorflow.python.utilimportdispatch
fromtensorflow.python.util.tf_exportimportkeras_export
fromtensorflow.tools.docsimportdoc_controls
@keras_export("keras.losses.Loss")
classLoss:
"""Lossbaseclass.
Tobeimplementedbysubclasses:
*`call()`:Containsthelogicforlosscalculationusing`y_true`,
`y_pred`.
Examplesubclassimplementation:
```python
classMeanSquaredError(Loss):
defcall(self,y_true,y_pred):
returntf.reduce_mean(tf.math.square(y_pred-y_true),axis=-1)
```
Whenusedwith`tf.distribute.Strategy`,outsideofbuilt-intrainingloops
suchas`tf.keras``compile`and`fit`,pleaseuse'SUM'or'NONE'reduction
types,andreducelossesexplicitlyinyourtrainingloop.Using'AUTO'or
'SUM_OVER_BATCH_SIZE'willraiseanerror.
Pleaseseethiscustomtraining[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)formore
detailsonthis.
Youcanimplement'SUM_OVER_BATCH_SIZE'usingglobalbatchsizelike:
```python
withstrategy.scope():
loss_obj=tf.keras.losses.CategoricalCrossentropy(
reduction=tf.keras.losses.Reduction.NONE)
....
loss=(tf.reduce_sum(loss_obj(labels,predictions))*
(1./global_batch_size))
```
"""
def__init__(self,reduction=losses_utils.ReductionV2.AUTO,name=None):
"""Initializes`Loss`class.
Args:
reduction:Typeof`tf.keras.losses.Reduction`toapplyto
loss.Defaultvalueis`AUTO`.`AUTO`indicatesthatthereduction
optionwillbedeterminedbytheusagecontext.Foralmostallcases
thisdefaultsto`SUM_OVER_BATCH_SIZE`.Whenusedwith
`tf.distribute.Strategy`,outsideofbuilt-intrainingloopssuchas
`tf.keras``compile`and`fit`,using`AUTO`or
`SUM_OVER_BATCH_SIZE`
willraiseanerror.Pleaseseethiscustomtraining[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)
formoredetails.
name:Optionalnamefortheinstance.
"""
losses_utils.ReductionV2.validate(reduction)
self.reduction=reduction
self.name=name
#SUM_OVER_BATCHisonlyallowedinlossesmanagedby`fit`or
#CannedEstimators.
self._allow_sum_over_batch_size=False
self._set_name_scope()
def_set_name_scope(self):
"""Createsavalid`name_scope`name."""
ifself.nameisNone:
self._name_scope=self.__class__.__name__.strip("_")
elifself.name=="":
self._name_scope="lambda"
else:
#E.g.'_my_loss'=>'my_loss'
self._name_scope=self.name.strip("_")
def__call__(self,y_true,y_pred,sample_weight=None):
"""Invokesthe`Loss`instance.
Args:
y_true:Groundtruthvalues.shape=`[batch_size,d0,..dN]`,except
sparselossfunctionssuchassparsecategoricalcrossentropywhere
shape=`[batch_size,d0,..dN-1]`
y_pred:Thepredictedvalues.shape=`[batch_size,d0,..dN]`
sample_weight:Optional`sample_weight`actsasacoefficientforthe
loss.Ifascalarisprovided,thenthelossissimplyscaledbythe
givenvalue.If`sample_weight`isatensorofsize`[batch_size]`,
thenthetotallossforeachsampleofthebatchisrescaledbythe
correspondingelementinthe`sample_weight`vector.Iftheshapeof
`sample_weight`is`[batch_size,d0,..dN-1]`(orcanbe
broadcastedtothisshape),theneachlosselementof`y_pred`is
scaledbythecorrespondingvalueof`sample_weight`.(Note
on`dN-1`:alllossfunctionsreduceby1dimension,usually
axis=-1.)
Returns:
Weightedlossfloat`Tensor`.If`reduction`is`NONE`,thishas
shape`[batch_size,d0,..dN-1]`;otherwise,itisscalar.(Note
`dN-1`becausealllossfunctionsreduceby1dimension,usually
axis=-1.)
Raises:
ValueError:Iftheshapeof`sample_weight`isinvalid.
"""
#Ifwearewrappingalambdafunctionstrip'<>'fromthenameasitis
#notacceptedinscopename.
graph_ctx=tf_utils.graph_context_for_symbolic_tensors(
y_true,y_pred,sample_weight
)
withbackend.name_scope(self._name_scope),graph_ctx:
iftf.executing_eagerly():
call_fn=self.call
else:
call_fn=tf.__internal__.autograph.tf_convert(
self.call,tf.__internal__.autograph.control_status_ctx()
)
losses=call_fn(y_true,y_pred)
mask=losses_utils.get_mask(losses)
reduction=self._get_reduction()
sample_weight=losses_utils.apply_valid_mask(
losses,sample_weight,mask,reduction
)
returnlosses_utils.compute_weighted_loss(
losses,sample_weight,reduction=reduction
)
@classmethod
deffrom_config(cls,config):
"""Instantiatesa`Loss`fromitsconfig(outputof`get_config()`).
Args:
config:Outputof`get_config()`.
Returns:
A`Loss`instance.
"""
returncls(**config)
defget_config(self):
"""Returnstheconfigdictionaryfora`Loss`instance."""
return{"reduction":self.reduction,"name":self.name}
@abc.abstractmethod
@doc_controls.for_subclass_implementers
defcall(self,y_true,y_pred):
"""Invokesthe`Loss`instance.
Args:
y_true:Groundtruthvalues.shape=`[batch_size,d0,..dN]`,except
sparselossfunctionssuchassparsecategoricalcrossentropywhere
shape=`[batch_size,d0,..dN-1]`
y_pred:Thepredictedvalues.shape=`[batch_size,d0,..dN]`
Returns:
Lossvalueswiththeshape`[batch_size,d0,..dN-1]`.
"""
raiseNotImplementedError("Mustbeimplementedinsubclasses.")
def_get_reduction(self):
"""Handles`AUTO`reductioncasesandreturnsthereductionvalue."""
if(
notself._allow_sum_over_batch_size
andtf.distribute.has_strategy()
and(
self.reduction==losses_utils.ReductionV2.AUTO
orself.reduction
==losses_utils.ReductionV2.SUM_OVER_BATCH_SIZE
)
):
raiseValueError(
"Pleaseuse`tf.keras.losses.Reduction.SUM`or"
"`tf.keras.losses.Reduction.NONE`forlossreductionwhen"
"lossesareusedwith`tf.distribute.Strategy`outside"
"ofthebuilt-intrainingloops.Youcanimplement"
"`tf.keras.losses.Reduction.SUM_OVER_BATCH_SIZE`using"
"globalbatchsizelike:\n```\nwithstrategy.scope():\n"
"loss_obj=tf.keras.losses.CategoricalCrossentropy("
"reduction=tf.keras.losses.Reduction.NONE)\n....\n"
"loss=tf.reduce_sum(loss_obj(labels,predictions))*"
"(1./global_batch_size)\n```\nPleasesee"
"https://www.tensorflow.org/tutorials"
"/distribute/custom_training"
"formoredetails."
)
ifself.reduction==losses_utils.ReductionV2.AUTO:
returnlosses_utils.ReductionV2.SUM_OVER_BATCH_SIZE
returnself.reduction
classLossFunctionWrapper(Loss):
"""Wrapsalossfunctioninthe`Loss`class."""
def__init__(
self,fn,reduction=losses_utils.ReductionV2.AUTO,name=None,**kwargs
):
"""Initializes`LossFunctionWrapper`class.
Args:
fn:Thelossfunctiontowrap,withsignature`fn(y_true,y_pred,
**kwargs)`.
reduction:Typeof`tf.keras.losses.Reduction`toapplyto
loss.Defaultvalueis`AUTO`.`AUTO`indicatesthatthereduction
optionwillbedeterminedbytheusagecontext.Foralmostallcases
thisdefaultsto`SUM_OVER_BATCH_SIZE`.Whenusedwith
`tf.distribute.Strategy`,outsideofbuilt-intrainingloopssuchas
`tf.keras``compile`and`fit`,using`AUTO`or
`SUM_OVER_BATCH_SIZE`willraiseanerror.Pleaseseethiscustom
training[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)for
moredetails.
name:Optionalnamefortheinstance.
**kwargs:Thekeywordargumentsthatarepassedonto`fn`.
"""
super().__init__(reduction=reduction,name=name)
self.fn=fn
self._fn_kwargs=kwargs
defcall(self,y_true,y_pred):
"""Invokesthe`LossFunctionWrapper`instance.
Args:
y_true:Groundtruthvalues.
y_pred:Thepredictedvalues.
Returns:
Lossvaluespersample.
"""
iftf.is_tensor(y_pred)andtf.is_tensor(y_true):
y_pred,y_true=losses_utils.squeeze_or_expand_dimensions(
y_pred,y_true
)
ag_fn=tf.__internal__.autograph.tf_convert(
self.fn,tf.__internal__.autograph.control_status_ctx()
)
returnag_fn(y_true,y_pred,**self._fn_kwargs)
defget_config(self):
config={}
fork,vinself._fn_kwargs.items():
config[k]=(
backend.eval(v)iftf_utils.is_tensor_or_variable(v)elsev
)
ifgetattr(saving_lib._SAVING_V3_ENABLED,"value",False):
fromkeras.utilsimportget_registered_name
config["fn"]=get_registered_name(self.fn)
base_config=super().get_config()
returndict(list(base_config.items())+list(config.items()))
@classmethod
deffrom_config(cls,config):
"""Instantiatesa`Loss`fromitsconfig(outputof`get_config()`).
Args:
config:Outputof`get_config()`.
Returns:
A`keras.losses.Loss`instance.
"""
ifgetattr(saving_lib._SAVING_V3_ENABLED,"value",False):
fn_name=config.pop("fn",None)
iffn_nameandclsisLossFunctionWrapper:
config["fn"]=get(fn_name)
returncls(**config)
@keras_export("keras.losses.MeanSquaredError")
classMeanSquaredError(LossFunctionWrapper):
"""Computesthemeanofsquaresoferrorsbetweenlabelsandpredictions.
`loss=square(y_true-y_pred)`
Standaloneusage:
>>>y_true=[[0.,1.],[0.,0.]]
>>>y_pred=[[1.,1.],[1.,0.]]
>>>#Using'auto'/'sum_over_batch_size'reductiontype.
>>>mse=tf.keras.losses.MeanSquaredError()
>>>mse(y_true,y_pred).numpy()
0.5
>>>#Callingwith'sample_weight'.
>>>mse(y_true,y_pred,sample_weight=[0.7,0.3]).numpy()
0.25
>>>#Using'sum'reductiontype.
>>>mse=tf.keras.losses.MeanSquaredError(
...reduction=tf.keras.losses.Reduction.SUM)
>>>mse(y_true,y_pred).numpy()
1.0
>>>#Using'none'reductiontype.
>>>mse=tf.keras.losses.MeanSquaredError(
...reduction=tf.keras.losses.Reduction.NONE)
>>>mse(y_true,y_pred).numpy()
array([0.5,0.5],dtype=float32)
Usagewiththe`compile()`API:
```python
model.compile(optimizer='sgd',loss=tf.keras.losses.MeanSquaredError())
```
"""
def__init__(
self,reduction=losses_utils.ReductionV2.AUTO,name="mean_squared_error"
):
"""Initializes`MeanSquaredError`instance.
Args:
reduction:Typeof`tf.keras.losses.Reduction`toapplyto
loss.Defaultvalueis`AUTO`.`AUTO`indicatesthatthereduction
optionwillbedeterminedbytheusagecontext.Foralmostallcases
thisdefaultsto`SUM_OVER_BATCH_SIZE`.Whenusedwith
`tf.distribute.Strategy`,outsideofbuilt-intrainingloopssuchas
`tf.keras``compile`and`fit`,using`AUTO`or
`SUM_OVER_BATCH_SIZE`willraiseanerror.Pleaseseethiscustom
training[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)for
moredetails.
name:Optionalnamefortheinstance.Defaultsto
'mean_squared_error'.
"""
super().__init__(mean_squared_error,name=name,reduction=reduction)
@keras_export("keras.losses.MeanAbsoluteError")
classMeanAbsoluteError(LossFunctionWrapper):
"""Computesthemeanofabsolutedifferencebetweenlabelsandpredictions.
`loss=abs(y_true-y_pred)`
Standaloneusage:
>>>y_true=[[0.,1.],[0.,0.]]
>>>y_pred=[[1.,1.],[1.,0.]]
>>>#Using'auto'/'sum_over_batch_size'reductiontype.
>>>mae=tf.keras.losses.MeanAbsoluteError()
>>>mae(y_true,y_pred).numpy()
0.5
>>>#Callingwith'sample_weight'.
>>>mae(y_true,y_pred,sample_weight=[0.7,0.3]).numpy()
0.25
>>>#Using'sum'reductiontype.
>>>mae=tf.keras.losses.MeanAbsoluteError(
...reduction=tf.keras.losses.Reduction.SUM)
>>>mae(y_true,y_pred).numpy()
1.0
>>>#Using'none'reductiontype.
>>>mae=tf.keras.losses.MeanAbsoluteError(
...reduction=tf.keras.losses.Reduction.NONE)
>>>mae(y_true,y_pred).numpy()
array([0.5,0.5],dtype=float32)
Usagewiththe`compile()`API:
```python
model.compile(optimizer='sgd',loss=tf.keras.losses.MeanAbsoluteError())
```
"""
def__init__(
self,
reduction=losses_utils.ReductionV2.AUTO,
name="mean_absolute_error",
):
"""Initializes`MeanAbsoluteError`instance.
Args:
reduction:Typeof`tf.keras.losses.Reduction`toapplyto
loss.Defaultvalueis`AUTO`.`AUTO`indicatesthatthereduction
optionwillbedeterminedbytheusagecontext.Foralmostallcases
thisdefaultsto`SUM_OVER_BATCH_SIZE`.Whenusedwith
`tf.distribute.Strategy`,outsideofbuilt-intrainingloopssuchas
`tf.keras``compile`and`fit`,using`AUTO`or
`SUM_OVER_BATCH_SIZE`willraiseanerror.Pleaseseethiscustom
training[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)for
moredetails.
name:Optionalnamefortheinstance.Defaultsto
'mean_absolute_error'.
"""
super().__init__(mean_absolute_error,name=name,reduction=reduction)
@keras_export("keras.losses.MeanAbsolutePercentageError")
classMeanAbsolutePercentageError(LossFunctionWrapper):
"""Computesthemeanabsolutepercentageerrorbetween`y_true`&`y_pred`.
Formula:
`loss=100*abs((y_true-y_pred)/y_true)`
Notethattoavoiddividingbyzero,asmallepsilonvalue
isaddedtothedenominator.
Standaloneusage:
>>>y_true=[[2.,1.],[2.,3.]]
>>>y_pred=[[1.,1.],[1.,0.]]
>>>#Using'auto'/'sum_over_batch_size'reductiontype.
>>>mape=tf.keras.losses.MeanAbsolutePercentageError()
>>>mape(y_true,y_pred).numpy()
50.
>>>#Callingwith'sample_weight'.
>>>mape(y_true,y_pred,sample_weight=[0.7,0.3]).numpy()
20.
>>>#Using'sum'reductiontype.
>>>mape=tf.keras.losses.MeanAbsolutePercentageError(
...reduction=tf.keras.losses.Reduction.SUM)
>>>mape(y_true,y_pred).numpy()
100.
>>>#Using'none'reductiontype.
>>>mape=tf.keras.losses.MeanAbsolutePercentageError(
...reduction=tf.keras.losses.Reduction.NONE)
>>>mape(y_true,y_pred).numpy()
array([25.,75.],dtype=float32)
Usagewiththe`compile()`API:
```python
model.compile(optimizer='sgd',
loss=tf.keras.losses.MeanAbsolutePercentageError())
```
"""
def__init__(
self,
reduction=losses_utils.ReductionV2.AUTO,
name="mean_absolute_percentage_error",
):
"""Initializes`MeanAbsolutePercentageError`instance.
Args:
reduction:Typeof`tf.keras.losses.Reduction`toapplyto
loss.Defaultvalueis`AUTO`.`AUTO`indicatesthatthereduction
optionwillbedeterminedbytheusagecontext.Foralmostallcases
thisdefaultsto`SUM_OVER_BATCH_SIZE`.Whenusedwith
`tf.distribute.Strategy`,outsideofbuilt-intrainingloopssuchas
`tf.keras``compile`and`fit`,using`AUTO`or
`SUM_OVER_BATCH_SIZE`willraiseanerror.Pleaseseethiscustom
training[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)for
moredetails.
name:Optionalnamefortheinstance.Defaultsto
'mean_absolute_percentage_error'.
"""
super().__init__(
mean_absolute_percentage_error,name=name,reduction=reduction
)
@keras_export("keras.losses.MeanSquaredLogarithmicError")
classMeanSquaredLogarithmicError(LossFunctionWrapper):
"""Computesthemeansquaredlogarithmicerrorbetween`y_true`&`y_pred`.
`loss=square(log(y_true+1.)-log(y_pred+1.))`
Standaloneusage:
>>>y_true=[[0.,1.],[0.,0.]]
>>>y_pred=[[1.,1.],[1.,0.]]
>>>#Using'auto'/'sum_over_batch_size'reductiontype.
>>>msle=tf.keras.losses.MeanSquaredLogarithmicError()
>>>msle(y_true,y_pred).numpy()
0.240
>>>#Callingwith'sample_weight'.
>>>msle(y_true,y_pred,sample_weight=[0.7,0.3]).numpy()
0.120
>>>#Using'sum'reductiontype.
>>>msle=tf.keras.losses.MeanSquaredLogarithmicError(
...reduction=tf.keras.losses.Reduction.SUM)
>>>msle(y_true,y_pred).numpy()
0.480
>>>#Using'none'reductiontype.
>>>msle=tf.keras.losses.MeanSquaredLogarithmicError(
...reduction=tf.keras.losses.Reduction.NONE)
>>>msle(y_true,y_pred).numpy()
array([0.240,0.240],dtype=float32)
Usagewiththe`compile()`API:
```python
model.compile(optimizer='sgd',
loss=tf.keras.losses.MeanSquaredLogarithmicError())
```
"""
def__init__(
self,
reduction=losses_utils.ReductionV2.AUTO,
name="mean_squared_logarithmic_error",
):
"""Initializes`MeanSquaredLogarithmicError`instance.
Args:
reduction:Typeof`tf.keras.losses.Reduction`toapplyto
loss.Defaultvalueis`AUTO`.`AUTO`indicatesthatthereduction
optionwillbedeterminedbytheusagecontext.Foralmostallcases
thisdefaultsto`SUM_OVER_BATCH_SIZE`.Whenusedwith
`tf.distribute.Strategy`,outsideofbuilt-intrainingloopssuchas
`tf.keras``compile`and`fit`,using`AUTO`or
`SUM_OVER_BATCH_SIZE`willraiseanerror.Pleaseseethiscustom
training[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)for
moredetails.
name:Optionalnamefortheinstance.Defaultsto
'mean_squared_logarithmic_error'.
"""
super().__init__(
mean_squared_logarithmic_error,name=name,reduction=reduction
)
@keras_export("keras.losses.BinaryCrossentropy")
classBinaryCrossentropy(LossFunctionWrapper):
"""Computesthecross-entropylossbetweentruelabelsandpredictedlabels.
Usethiscross-entropylossforbinary(0or1)classificationapplications.
Thelossfunctionrequiresthefollowinginputs:
-`y_true`(truelabel):Thisiseither0or1.
-`y_pred`(predictedvalue):Thisisthemodel'sprediction,i.e,asingle
floating-pointvaluewhicheitherrepresentsa
[logit](https://en.wikipedia.org/wiki/Logit),(i.e,valuein[-inf,inf]
when`from_logits=True`)oraprobability(i.e,valuein[0.,1.]when
`from_logits=False`).
**RecommendedUsage:**(set`from_logits=True`)
With`tf.keras`API:
```python
model.compile(
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
....
)
```
Asastandalonefunction:
>>>#Example1:(batch_size=1,numberofsamples=4)
>>>y_true=[0,1,0,0]
>>>y_pred=[-18.6,0.51,2.94,-12.8]
>>>bce=tf.keras.losses.BinaryCrossentropy(from_logits=True)
>>>bce(y_true,y_pred).numpy()
0.865
>>>#Example2:(batch_size=2,numberofsamples=4)
>>>y_true=[[0,1],[0,0]]
>>>y_pred=[[-18.6,0.51],[2.94,-12.8]]
>>>#Usingdefault'auto'/'sum_over_batch_size'reductiontype.
>>>bce=tf.keras.losses.BinaryCrossentropy(from_logits=True)
>>>bce(y_true,y_pred).numpy()
0.865
>>>#Using'sample_weight'attribute
>>>bce(y_true,y_pred,sample_weight=[0.8,0.2]).numpy()
0.243
>>>#Using'sum'reduction`type.
>>>bce=tf.keras.losses.BinaryCrossentropy(from_logits=True,
...reduction=tf.keras.losses.Reduction.SUM)
>>>bce(y_true,y_pred).numpy()
1.730
>>>#Using'none'reductiontype.
>>>bce=tf.keras.losses.BinaryCrossentropy(from_logits=True,
...reduction=tf.keras.losses.Reduction.NONE)
>>>bce(y_true,y_pred).numpy()
array([0.235,1.496],dtype=float32)
**DefaultUsage:**(set`from_logits=False`)
>>>#Makethefollowingupdatestotheabove"RecommendedUsage"section
>>>#1.Set`from_logits=False`
>>>tf.keras.losses.BinaryCrossentropy()#OR...('from_logits=False')
>>>#2.Update`y_pred`touseprobabilitiesinsteadoflogits
>>>y_pred=[0.6,0.3,0.2,0.8]#OR[[0.6,0.3],[0.2,0.8]]
"""
def__init__(
self,
from_logits=False,
label_smoothing=0.0,
axis=-1,
reduction=losses_utils.ReductionV2.AUTO,
name="binary_crossentropy",
):
"""Initializes`BinaryCrossentropy`instance.
Args:
from_logits:Whethertointerpret`y_pred`asatensorof
[logit](https://en.wikipedia.org/wiki/Logit)values.Bydefault,we
assumethat`y_pred`containsprobabilities(i.e.,valuesin[0,
1]).
label_smoothing:Floatin[0,1].When0,nosmoothingoccurs.When>
0,wecomputethelossbetweenthepredictedlabelsandasmoothed
versionofthetruelabels,wherethesmoothingsqueezesthelabels
towards0.5.Largervaluesof`label_smoothing`correspondto
heaviersmoothing.
axis:Theaxisalongwhichtocomputecrossentropy(thefeatures
axis).Defaultsto-1.
reduction:Typeof`tf.keras.losses.Reduction`toapplyto
loss.Defaultvalueis`AUTO`.`AUTO`indicatesthatthereduction
optionwillbedeterminedbytheusagecontext.Foralmostallcases
thisdefaultsto`SUM_OVER_BATCH_SIZE`.Whenusedwith
`tf.distribute.Strategy`,outsideofbuilt-intrainingloopssuchas
`tf.keras``compile`and`fit`,using`AUTO`or
`SUM_OVER_BATCH_SIZE`willraiseanerror.Pleaseseethiscustom
training[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)for
moredetails.
name:Namefortheop.Defaultsto'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
@keras_export("keras.losses.BinaryFocalCrossentropy")
classBinaryFocalCrossentropy(LossFunctionWrapper):
"""Computesthefocalcross-entropylossbetweentruelabelsandpredictions.
Binarycross-entropylossisoftenusedforbinary(0or1)classification
tasks.Thelossfunctionrequiresthefollowinginputs:
-`y_true`(truelabel):Thisiseither0or1.
-`y_pred`(predictedvalue):Thisisthemodel'sprediction,i.e,asingle
floating-pointvaluewhicheitherrepresentsa
[logit](https://en.wikipedia.org/wiki/Logit),(i.e,valuein[-inf,inf]
when`from_logits=True`)oraprobability(i.e,valuein`[0.,1.]`when
`from_logits=False`).
Accordingto[Linetal.,2018](https://arxiv.org/pdf/1708.02002.pdf),it
helpstoapplya"focalfactor"todown-weighteasyexamplesandfocusmore
onhardexamples.Bydefault,thefocaltensoriscomputedasfollows:
`focal_factor=(1-output)**gamma`forclass1
`focal_factor=output**gamma`forclass0
where`gamma`isafocusingparameter.When`gamma=0`,thisfunctionis
equivalenttothebinarycrossentropyloss.
Withthe`compile()`API:
```python
model.compile(
loss=tf.keras.losses.BinaryFocalCrossentropy(gamma=2.0,from_logits=True),
....
)
```
Asastandalonefunction:
>>>#Example1:(batch_size=1,numberofsamples=4)
>>>y_true=[0,1,0,0]
>>>y_pred=[-18.6,0.51,2.94,-12.8]
>>>loss=tf.keras.losses.BinaryFocalCrossentropy(gamma=2,
...from_logits=True)
>>>loss(y_true,y_pred).numpy()
0.691
>>>#Applyclassweight
>>>loss=tf.keras.losses.BinaryFocalCrossentropy(
...apply_class_balancing=True,gamma=2,from_logits=True)
>>>loss(y_true,y_pred).numpy()
0.51
>>>#Example2:(batch_size=2,numberofsamples=4)
>>>y_true=[[0,1],[0,0]]
>>>y_pred=[[-18.6,0.51],[2.94,-12.8]]
>>>#Usingdefault'auto'/'sum_over_batch_size'reductiontype.
>>>loss=tf.keras.losses.BinaryFocalCrossentropy(gamma=3,
...from_logits=True)
>>>loss(y_true,y_pred).numpy()
0.647
>>>#Applyclassweight
>>>loss=tf.keras.losses.BinaryFocalCrossentropy(
...apply_class_balancing=True,gamma=3,from_logits=True)
>>>loss(y_true,y_pred).numpy()
0.482
>>>#Using'sample_weight'attributewithfocaleffect
>>>loss=tf.keras.losses.BinaryFocalCrossentropy(gamma=3,
...from_logits=True)
>>>loss(y_true,y_pred,sample_weight=[0.8,0.2]).numpy()
0.133
>>>#Applyclassweight
>>>loss=tf.keras.losses.BinaryFocalCrossentropy(
...apply_class_balancing=True,gamma=3,from_logits=True)
>>>loss(y_true,y_pred,sample_weight=[0.8,0.2]).numpy()
0.097
>>>#Using'sum'reduction`type.
>>>loss=tf.keras.losses.BinaryFocalCrossentropy(gamma=4,
...from_logits=True,
...reduction=tf.keras.losses.Reduction.SUM)
>>>loss(y_true,y_pred).numpy()
1.222
>>>#Applyclassweight
>>>loss=tf.keras.losses.BinaryFocalCrossentropy(
...apply_class_balancing=True,gamma=4,from_logits=True,
...reduction=tf.keras.losses.Reduction.SUM)
>>>loss(y_true,y_pred).numpy()
0.914
>>>#Using'none'reductiontype.
>>>loss=tf.keras.losses.BinaryFocalCrossentropy(
...gamma=5,from_logits=True,
...reduction=tf.keras.losses.Reduction.NONE)
>>>loss(y_true,y_pred).numpy()
array([0.00171.1561],dtype=float32)
>>>#Applyclassweight
>>>loss=tf.keras.losses.BinaryFocalCrossentropy(
...apply_class_balancing=True,gamma=5,from_logits=True,
...reduction=tf.keras.losses.Reduction.NONE)
>>>loss(y_true,y_pred).numpy()
array([0.00040.8670],dtype=float32)
Args:
apply_class_balancing:Abool,whethertoapplyweightbalancingonthe
binaryclasses0and1.
alpha:Aweightbalancingfactorforclass1,defaultis`0.25`as
mentionedinreference[Linetal.,2018](
https://arxiv.org/pdf/1708.02002.pdf).Theweightforclass0is
`1.0-alpha`.
gamma:Afocusingparameterusedtocomputethefocalfactor,defaultis
`2.0`asmentionedinthereference
[Linetal.,2018](https://arxiv.org/pdf/1708.02002.pdf).
from_logits:Whethertointerpret`y_pred`asatensorof
[logit](https://en.wikipedia.org/wiki/Logit)values.Bydefault,we
assumethat`y_pred`areprobabilities(i.e.,valuesin`[0,1]`).
label_smoothing:Floatin`[0,1]`.When`0`,nosmoothingoccurs.When>
`0`,wecomputethelossbetweenthepredictedlabelsandasmoothed
versionofthetruelabels,wherethesmoothingsqueezesthelabels
towards`0.5`.Largervaluesof`label_smoothing`correspondtoheavier
smoothing.
axis:Theaxisalongwhichtocomputecrossentropy(thefeaturesaxis).
Defaultsto`-1`.
reduction:Typeof`tf.keras.losses.Reduction`toapplyto
loss.Defaultvalueis`AUTO`.`AUTO`indicatesthatthereduction
optionwillbedeterminedbytheusagecontext.Foralmostallcases
thisdefaultsto`SUM_OVER_BATCH_SIZE`.Whenusedwith
`tf.distribute.Strategy`,outsideofbuilt-intrainingloopssuchas
`tf.keras`,`compile()`and`fit()`,using`SUM_OVER_BATCH_SIZE`or
`AUTO`willraiseanerror.Pleaseseethiscustomtraining[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)for
moredetails.
name:Namefortheop.Defaultsto'binary_focal_crossentropy'.
"""
def__init__(
self,
apply_class_balancing=False,
alpha=0.25,
gamma=2.0,
from_logits=False,
label_smoothing=0.0,
axis=-1,
reduction=losses_utils.ReductionV2.AUTO,
name="binary_focal_crossentropy",
):
"""Initializes`BinaryFocalCrossentropy`instance."""
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.apply_class_balancing=apply_class_balancing
self.alpha=alpha
self.gamma=gamma
defget_config(self):
config={
"apply_class_balancing":self.apply_class_balancing,
"alpha":self.alpha,
"gamma":self.gamma,
}
base_config=super().get_config()
returndict(list(base_config.items())+list(config.items()))
@keras_export("keras.losses.CategoricalCrossentropy")
classCategoricalCrossentropy(LossFunctionWrapper):
"""Computesthecrossentropylossbetweenthelabelsandpredictions.
Usethiscrossentropylossfunctionwhentherearetwoormorelabel
classes.Weexpectlabelstobeprovidedina`one_hot`representation.If
youwanttoprovidelabelsasintegers,pleaseuse
`SparseCategoricalCrossentropy`loss.Thereshouldbe`#classes`floating
pointvaluesperfeature.
Inthesnippetbelow,thereis`#classes`floatingpointingvaluesper
example.Theshapeofboth`y_pred`and`y_true`are
`[batch_size,num_classes]`.
Standaloneusage:
>>>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'reductiontype.
>>>cce=tf.keras.losses.CategoricalCrossentropy()
>>>cce(y_true,y_pred).numpy()
1.177
>>>#Callingwith'sample_weight'.
>>>cce(y_true,y_pred,sample_weight=tf.constant([0.3,0.7])).numpy()
0.814
>>>#Using'sum'reductiontype.
>>>cce=tf.keras.losses.CategoricalCrossentropy(
...reduction=tf.keras.losses.Reduction.SUM)
>>>cce(y_true,y_pred).numpy()
2.354
>>>#Using'none'reductiontype.
>>>cce=tf.keras.losses.CategoricalCrossentropy(
...reduction=tf.keras.losses.Reduction.NONE)
>>>cce(y_true,y_pred).numpy()
array([0.0513,2.303],dtype=float32)
Usagewiththe`compile()`API:
```python
model.compile(optimizer='sgd',
loss=tf.keras.losses.CategoricalCrossentropy())
```
"""
def__init__(
self,
from_logits=False,
label_smoothing=0.0,
axis=-1,
reduction=losses_utils.ReductionV2.AUTO,
name="categorical_crossentropy",
):
"""Initializes`CategoricalCrossentropy`instance.
Args:
from_logits:Whether`y_pred`isexpectedtobealogitstensor.By
default,weassumethat`y_pred`encodesaprobabilitydistribution.
label_smoothing:Floatin[0,1].When>0,labelvaluesaresmoothed,
meaningtheconfidenceonlabelvaluesarerelaxed.Forexample,if
`0.1`,use`0.1/num_classes`fornon-targetlabelsand
`0.9+0.1/num_classes`fortargetlabels.
axis:Theaxisalongwhichtocomputecrossentropy(thefeatures
axis).Defaultsto-1.
reduction:Typeof`tf.keras.losses.Reduction`toapplyto
loss.Defaultvalueis`AUTO`.`AUTO`indicatesthatthereduction
optionwillbedeterminedbytheusagecontext.Foralmostallcases
thisdefaultsto`SUM_OVER_BATCH_SIZE`.Whenusedwith
`tf.distribute.Strategy`,outsideofbuilt-intrainingloopssuchas
`tf.keras``compile`and`fit`,using`AUTO`or
`SUM_OVER_BATCH_SIZE`willraiseanerror.Pleaseseethiscustom
training[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)for
moredetails.
name:Optionalnamefortheinstance.
Defaultsto'categorical_crossentropy'.
"""
super().__init__(
categorical_crossentropy,
name=name,
reduction=reduction,
from_logits=from_logits,
label_smoothing=label_smoothing,
axis=axis,
)
@keras_export("keras.losses.SparseCategoricalCrossentropy")
classSparseCategoricalCrossentropy(LossFunctionWrapper):
"""Computesthecrossentropylossbetweenthelabelsandpredictions.
Usethiscrossentropylossfunctionwhentherearetwoormorelabel
classes.Weexpectlabelstobeprovidedasintegers.Ifyouwantto
providelabelsusing`one-hot`representation,pleaseuse
`CategoricalCrossentropy`loss.Thereshouldbe`#classes`floatingpoint
valuesperfeaturefor`y_pred`andasinglefloatingpointvalueper
featurefor`y_true`.
Inthesnippetbelow,thereisasinglefloatingpointvalueperexamplefor
`y_true`and`#classes`floatingpointingvaluesperexamplefor`y_pred`.
Theshapeof`y_true`is`[batch_size]`andtheshapeof`y_pred`is
`[batch_size,num_classes]`.
Standaloneusage:
>>>y_true=[1,2]
>>>y_pred=[[0.05,0.95,0],[0.1,0.8,0.1]]
>>>#Using'auto'/'sum_over_batch_size'reductiontype.
>>>scce=tf.keras.losses.SparseCategoricalCrossentropy()
>>>scce(y_true,y_pred).numpy()
1.177
>>>#Callingwith'sample_weight'.
>>>scce(y_true,y_pred,sample_weight=tf.constant([0.3,0.7])).numpy()
0.814
>>>#Using'sum'reductiontype.
>>>scce=tf.keras.losses.SparseCategoricalCrossentropy(
...reduction=tf.keras.losses.Reduction.SUM)
>>>scce(y_true,y_pred).numpy()
2.354
>>>#Using'none'reductiontype.
>>>scce=tf.keras.losses.SparseCategoricalCrossentropy(
...reduction=tf.keras.losses.Reduction.NONE)
>>>scce(y_true,y_pred).numpy()
array([0.0513,2.303],dtype=float32)
Usagewiththe`compile()`API:
```python
model.compile(optimizer='sgd',
loss=tf.keras.losses.SparseCategoricalCrossentropy())
```
"""
def__init__(
self,
from_logits=False,
ignore_class=None,
reduction=losses_utils.ReductionV2.AUTO,
name="sparse_categorical_crossentropy",
):
"""Initializes`SparseCategoricalCrossentropy`instance.
Args:
from_logits:Whether`y_pred`isexpectedtobealogitstensor.By
default,weassumethat`y_pred`encodesaprobabilitydistribution.
ignore_class:Optionalinteger.TheIDofaclasstobeignoredduring
losscomputation.Thisisuseful,forexample,insegmentation
problemsfeaturinga"void"class(commonly-1or255)in
segmentationmaps.
Bydefault(`ignore_class=None`),allclassesareconsidered.
reduction:Typeof`tf.keras.losses.Reduction`toapplyto
loss.Defaultvalueis`AUTO`.`AUTO`indicatesthatthereduction
optionwillbedeterminedbytheusagecontext.Foralmostallcases
thisdefaultsto`SUM_OVER_BATCH_SIZE`.Whenusedwith
`tf.distribute.Strategy`,outsideofbuilt-intrainingloopssuchas
`tf.keras``compile`and`fit`,using`AUTO`or
`SUM_OVER_BATCH_SIZE`willraiseanerror.Pleaseseethiscustom
training[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)for
moredetails.
name:Optionalnamefortheinstance.Defaultsto
'sparse_categorical_crossentropy'.
"""
super().__init__(
sparse_categorical_crossentropy,
name=name,
reduction=reduction,
from_logits=from_logits,
ignore_class=ignore_class,
)
@keras_export("keras.losses.Hinge")
classHinge(LossFunctionWrapper):
"""Computesthehingelossbetween`y_true`&`y_pred`.
`loss=maximum(1-y_true*y_pred,0)`
`y_true`valuesareexpectedtobe-1or1.Ifbinary(0or1)labelsare
providedwewillconvertthemto-1or1.
Standaloneusage:
>>>y_true=[[0.,1.],[0.,0.]]
>>>y_pred=[[0.6,0.4],[0.4,0.6]]
>>>#Using'auto'/'sum_over_batch_size'reductiontype.
>>>h=tf.keras.losses.Hinge()
>>>h(y_true,y_pred).numpy()
1.3
>>>#Callingwith'sample_weight'.
>>>h(y_true,y_pred,sample_weight=[1,0]).numpy()
0.55
>>>#Using'sum'reductiontype.
>>>h=tf.keras.losses.Hinge(
...reduction=tf.keras.losses.Reduction.SUM)
>>>h(y_true,y_pred).numpy()
2.6
>>>#Using'none'reductiontype.
>>>h=tf.keras.losses.Hinge(
...reduction=tf.keras.losses.Reduction.NONE)
>>>h(y_true,y_pred).numpy()
array([1.1,1.5],dtype=float32)
Usagewiththe`compile()`API:
```python
model.compile(optimizer='sgd',loss=tf.keras.losses.Hinge())
```
"""
def__init__(self,reduction=losses_utils.ReductionV2.AUTO,name="hinge"):
"""Initializes`Hinge`instance.
Args:
reduction:Typeof`tf.keras.losses.Reduction`toapplyto
loss.Defaultvalueis`AUTO`.`AUTO`indicatesthatthereduction
optionwillbedeterminedbytheusagecontext.Foralmostallcases
thisdefaultsto`SUM_OVER_BATCH_SIZE`.Whenusedwith
`tf.distribute.Strategy`,outsideofbuilt-intrainingloopssuchas
`tf.keras``compile`and`fit`,using`AUTO`or
`SUM_OVER_BATCH_SIZE`willraiseanerror.Pleaseseethiscustom
training[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)for
moredetails.
name:Optionalnamefortheinstance.Defaultsto'hinge'.
"""
super().__init__(hinge,name=name,reduction=reduction)
@keras_export("keras.losses.SquaredHinge")
classSquaredHinge(LossFunctionWrapper):
"""Computesthesquaredhingelossbetween`y_true`&`y_pred`.
`loss=square(maximum(1-y_true*y_pred,0))`
`y_true`valuesareexpectedtobe-1or1.Ifbinary(0or1)labelsare
providedwewillconvertthemto-1or1.
Standaloneusage:
>>>y_true=[[0.,1.],[0.,0.]]
>>>y_pred=[[0.6,0.4],[0.4,0.6]]
>>>#Using'auto'/'sum_over_batch_size'reductiontype.
>>>h=tf.keras.losses.SquaredHinge()
>>>h(y_true,y_pred).numpy()
1.86
>>>#Callingwith'sample_weight'.
>>>h(y_true,y_pred,sample_weight=[1,0]).numpy()
0.73
>>>#Using'sum'reductiontype.
>>>h=tf.keras.losses.SquaredHinge(
...reduction=tf.keras.losses.Reduction.SUM)
>>>h(y_true,y_pred).numpy()
3.72
>>>#Using'none'reductiontype.
>>>h=tf.keras.losses.SquaredHinge(
...reduction=tf.keras.losses.Reduction.NONE)
>>>h(y_true,y_pred).numpy()
array([1.46,2.26],dtype=float32)
Usagewiththe`compile()`API:
```python
model.compile(optimizer='sgd',loss=tf.keras.losses.SquaredHinge())
```
"""
def__init__(
self,reduction=losses_utils.ReductionV2.AUTO,name="squared_hinge"
):
"""Initializes`SquaredHinge`instance.
Args:
reduction:Typeof`tf.keras.losses.Reduction`toapplyto
loss.Defaultvalueis`AUTO`.`AUTO`indicatesthatthereduction
optionwillbedeterminedbytheusagecontext.Foralmostallcases
thisdefaultsto`SUM_OVER_BATCH_SIZE`.Whenusedwith
`tf.distribute.Strategy`,outsideofbuilt-intrainingloopssuchas
`tf.keras``compile`and`fit`,using`AUTO`or
`SUM_OVER_BATCH_SIZE`willraiseanerror.Pleaseseethiscustom
training[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)for
moredetails.
name:Optionalnamefortheinstance.Defaultsto'squared_hinge'.
"""
super().__init__(squared_hinge,name=name,reduction=reduction)
@keras_export("keras.losses.CategoricalHinge")
classCategoricalHinge(LossFunctionWrapper):
"""Computesthecategoricalhingelossbetween`y_true`&`y_pred`.
`loss=maximum(neg-pos+1,0)`
where`neg=maximum((1-y_true)*y_pred)andpos=sum(y_true*y_pred)`
Standaloneusage:
>>>y_true=[[0,1],[0,0]]
>>>y_pred=[[0.6,0.4],[0.4,0.6]]
>>>#Using'auto'/'sum_over_batch_size'reductiontype.
>>>h=tf.keras.losses.CategoricalHinge()
>>>h(y_true,y_pred).numpy()
1.4
>>>#Callingwith'sample_weight'.
>>>h(y_true,y_pred,sample_weight=[1,0]).numpy()
0.6
>>>#Using'sum'reductiontype.
>>>h=tf.keras.losses.CategoricalHinge(
...reduction=tf.keras.losses.Reduction.SUM)
>>>h(y_true,y_pred).numpy()
2.8
>>>#Using'none'reductiontype.
>>>h=tf.keras.losses.CategoricalHinge(
...reduction=tf.keras.losses.Reduction.NONE)
>>>h(y_true,y_pred).numpy()
array([1.2,1.6],dtype=float32)
Usagewiththe`compile()`API:
```python
model.compile(optimizer='sgd',loss=tf.keras.losses.CategoricalHinge())
```
"""
def__init__(
self,reduction=losses_utils.ReductionV2.AUTO,name="categorical_hinge"
):
"""Initializes`CategoricalHinge`instance.
Args:
reduction:Typeof`tf.keras.losses.Reduction`toapplyto
loss.Defaultvalueis`AUTO`.`AUTO`indicatesthatthereduction
optionwillbedeterminedbytheusagecontext.Foralmostallcases
thisdefaultsto`SUM_OVER_BATCH_SIZE`.Whenusedwith
`tf.distribute.Strategy`,outsideofbuilt-intrainingloopssuchas
`tf.keras``compile`and`fit`,using`AUTO`or
`SUM_OVER_BATCH_SIZE`willraiseanerror.Pleaseseethiscustom
training[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)for
moredetails.
name:Optionalnamefortheinstance.Defaultsto'categorical_hinge'.
"""
super().__init__(categorical_hinge,name=name,reduction=reduction)
@keras_export("keras.losses.Poisson")
classPoisson(LossFunctionWrapper):
"""ComputesthePoissonlossbetween`y_true`&`y_pred`.
`loss=y_pred-y_true*log(y_pred)`
Standaloneusage:
>>>y_true=[[0.,1.],[0.,0.]]
>>>y_pred=[[1.,1.],[0.,0.]]
>>>#Using'auto'/'sum_over_batch_size'reductiontype.
>>>p=tf.keras.losses.Poisson()
>>>p(y_true,y_pred).numpy()
0.5
>>>#Callingwith'sample_weight'.
>>>p(y_true,y_pred,sample_weight=[0.8,0.2]).numpy()
0.4
>>>#Using'sum'reductiontype.
>>>p=tf.keras.losses.Poisson(
...reduction=tf.keras.losses.Reduction.SUM)
>>>p(y_true,y_pred).numpy()
0.999
>>>#Using'none'reductiontype.
>>>p=tf.keras.losses.Poisson(
...reduction=tf.keras.losses.Reduction.NONE)
>>>p(y_true,y_pred).numpy()
array([0.999,0.],dtype=float32)
Usagewiththe`compile()`API:
```python
model.compile(optimizer='sgd',loss=tf.keras.losses.Poisson())
```
"""
def__init__(self,reduction=losses_utils.ReductionV2.AUTO,name="poisson"):
"""Initializes`Poisson`instance.
Args:
reduction:Typeof`tf.keras.losses.Reduction`toapplyto
loss.Defaultvalueis`AUTO`.`AUTO`indicatesthatthereduction
optionwillbedeterminedbytheusagecontext.Foralmostallcases
thisdefaultsto`SUM_OVER_BATCH_SIZE`.Whenusedwith
`tf.distribute.Strategy`,outsideofbuilt-intrainingloopssuchas
`tf.keras``compile`and`fit`,using`AUTO`or
`SUM_OVER_BATCH_SIZE`willraiseanerror.Pleaseseethiscustom
training[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)for
moredetails.
name:Optionalnamefortheinstance.Defaultsto'poisson'.
"""
super().__init__(poisson,name=name,reduction=reduction)
@keras_export("keras.losses.LogCosh")
classLogCosh(LossFunctionWrapper):
"""Computesthelogarithmofthehyperboliccosineofthepredictionerror.
`logcosh=log((exp(x)+exp(-x))/2)`,
wherexistheerror`y_pred-y_true`.
Standaloneusage:
>>>y_true=[[0.,1.],[0.,0.]]
>>>y_pred=[[1.,1.],[0.,0.]]
>>>#Using'auto'/'sum_over_batch_size'reductiontype.
>>>l=tf.keras.losses.LogCosh()
>>>l(y_true,y_pred).numpy()
0.108
>>>#Callingwith'sample_weight'.
>>>l(y_true,y_pred,sample_weight=[0.8,0.2]).numpy()
0.087
>>>#Using'sum'reductiontype.
>>>l=tf.keras.losses.LogCosh(
...reduction=tf.keras.losses.Reduction.SUM)
>>>l(y_true,y_pred).numpy()
0.217
>>>#Using'none'reductiontype.
>>>l=tf.keras.losses.LogCosh(
...reduction=tf.keras.losses.Reduction.NONE)
>>>l(y_true,y_pred).numpy()
array([0.217,0.],dtype=float32)
Usagewiththe`compile()`API:
```python
model.compile(optimizer='sgd',loss=tf.keras.losses.LogCosh())
```
"""
def__init__(
self,reduction=losses_utils.ReductionV2.AUTO,name="log_cosh"
):
"""Initializes`LogCosh`instance.
Args:
reduction:Typeof`tf.keras.losses.Reduction`toapplyto
loss.Defaultvalueis`AUTO`.`AUTO`indicatesthatthereduction
optionwillbedeterminedbytheusagecontext.Foralmostallcases
thisdefaultsto`SUM_OVER_BATCH_SIZE`.Whenusedwith
`tf.distribute.Strategy`,outsideofbuilt-intrainingloopssuchas
`tf.keras``compile`and`fit`,using`AUTO`or
`SUM_OVER_BATCH_SIZE`willraiseanerror.Pleaseseethiscustom
training[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)for
moredetails.
name:Optionalnamefortheinstance.Defaultsto'log_cosh'.
"""
super().__init__(log_cosh,name=name,reduction=reduction)
@keras_export("keras.losses.KLDivergence")
classKLDivergence(LossFunctionWrapper):
"""ComputesKullback-Leiblerdivergencelossbetween`y_true`&`y_pred`.
`loss=y_true*log(y_true/y_pred)`
See:https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence
Standaloneusage:
>>>y_true=[[0,1],[0,0]]
>>>y_pred=[[0.6,0.4],[0.4,0.6]]
>>>#Using'auto'/'sum_over_batch_size'reductiontype.
>>>kl=tf.keras.losses.KLDivergence()
>>>kl(y_true,y_pred).numpy()
0.458
>>>#Callingwith'sample_weight'.
>>>kl(y_true,y_pred,sample_weight=[0.8,0.2]).numpy()
0.366
>>>#Using'sum'reductiontype.
>>>kl=tf.keras.losses.KLDivergence(
...reduction=tf.keras.losses.Reduction.SUM)
>>>kl(y_true,y_pred).numpy()
0.916
>>>#Using'none'reductiontype.
>>>kl=tf.keras.losses.KLDivergence(
...reduction=tf.keras.losses.Reduction.NONE)
>>>kl(y_true,y_pred).numpy()
array([0.916,-3.08e-06],dtype=float32)
Usagewiththe`compile()`API:
```python
model.compile(optimizer='sgd',loss=tf.keras.losses.KLDivergence())
```
"""
def__init__(
self,reduction=losses_utils.ReductionV2.AUTO,name="kl_divergence"
):
"""Initializes`KLDivergence`instance.
Args:
reduction:Typeof`tf.keras.losses.Reduction`toapplyto
loss.Defaultvalueis`AUTO`.`AUTO`indicatesthatthereduction
optionwillbedeterminedbytheusagecontext.Foralmostallcases
thisdefaultsto`SUM_OVER_BATCH_SIZE`.Whenusedwith
`tf.distribute.Strategy`,outsideofbuilt-intrainingloopssuchas
`tf.keras``compile`and`fit`,using`AUTO`or
`SUM_OVER_BATCH_SIZE`willraiseanerror.Pleaseseethiscustom
training[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)for
moredetails.
name:Optionalnamefortheinstance.Defaultsto'kl_divergence'.
"""
super().__init__(kl_divergence,name=name,reduction=reduction)
@keras_export("keras.losses.Huber")
classHuber(LossFunctionWrapper):
"""ComputestheHuberlossbetween`y_true`&`y_pred`.
Foreachvaluexin`error=y_true-y_pred`:
```
loss=0.5*x^2if|x|<=d
loss=0.5*d^2+d*(|x|-d)if|x|>d
```
wheredis`delta`.See:https://en.wikipedia.org/wiki/Huber_loss
Standaloneusage:
>>>y_true=[[0,1],[0,0]]
>>>y_pred=[[0.6,0.4],[0.4,0.6]]
>>>#Using'auto'/'sum_over_batch_size'reductiontype.
>>>h=tf.keras.losses.Huber()
>>>h(y_true,y_pred).numpy()
0.155
>>>#Callingwith'sample_weight'.
>>>h(y_true,y_pred,sample_weight=[1,0]).numpy()
0.09
>>>#Using'sum'reductiontype.
>>>h=tf.keras.losses.Huber(
...reduction=tf.keras.losses.Reduction.SUM)
>>>h(y_true,y_pred).numpy()
0.31
>>>#Using'none'reductiontype.
>>>h=tf.keras.losses.Huber(
...reduction=tf.keras.losses.Reduction.NONE)
>>>h(y_true,y_pred).numpy()
array([0.18,0.13],dtype=float32)
Usagewiththe`compile()`API:
```python
model.compile(optimizer='sgd',loss=tf.keras.losses.Huber())
```
"""
def__init__(
self,
delta=1.0,
reduction=losses_utils.ReductionV2.AUTO,
name="huber_loss",
):
"""Initializes`Huber`instance.
Args:
delta:Afloat,thepointwheretheHuberlossfunctionchangesfroma
quadratictolinear.
reduction:Typeof`tf.keras.losses.Reduction`toapplyto
loss.Defaultvalueis`AUTO`.`AUTO`indicatesthatthereduction
optionwillbedeterminedbytheusagecontext.Foralmostallcases
thisdefaultsto`SUM_OVER_BATCH_SIZE`.Whenusedwith
`tf.distribute.Strategy`,outsideofbuilt-intrainingloopssuchas
`tf.keras``compile`and`fit`,using`AUTO`or
`SUM_OVER_BATCH_SIZE`willraiseanerror.Pleaseseethiscustom
training[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)for
moredetails.
name:Optionalnamefortheinstance.Defaultsto'huber_loss'.
"""
super().__init__(huber,name=name,reduction=reduction,delta=delta)
@keras_export(
"keras.metrics.mean_squared_error",
"keras.metrics.mse",
"keras.metrics.MSE",
"keras.losses.mean_squared_error",
"keras.losses.mse",
"keras.losses.MSE",
)
@tf.__internal__.dispatch.add_dispatch_support
defmean_squared_error(y_true,y_pred):
"""Computesthemeansquarederrorbetweenlabelsandpredictions.
Aftercomputingthesquareddistancebetweentheinputs,themeanvalueover
thelastdimensionisreturned.
`loss=mean(square(y_true-y_pred),axis=-1)`
Standaloneusage:
>>>y_true=np.random.randint(0,2,size=(2,3))
>>>y_pred=np.random.random(size=(2,3))
>>>loss=tf.keras.losses.mean_squared_error(y_true,y_pred)
>>>assertloss.shape==(2,)
>>>assertnp.array_equal(
...loss.numpy(),np.mean(np.square(y_true-y_pred),axis=-1))
Args:
y_true:Groundtruthvalues.shape=`[batch_size,d0,..dN]`.
y_pred:Thepredictedvalues.shape=`[batch_size,d0,..dN]`.
Returns:
Meansquarederrorvalues.shape=`[batch_size,d0,..dN-1]`.
"""
y_pred=tf.convert_to_tensor(y_pred)
y_true=tf.cast(y_true,y_pred.dtype)
returnbackend.mean(tf.math.squared_difference(y_pred,y_true),axis=-1)
def_ragged_tensor_apply_loss(loss_fn,y_true,y_pred,y_pred_extra_dim=False):
"""Applyalossfunctiononaperbatchbasis.
Args:
loss_fn:Thelossfunction
y_true:truthvalues(RaggedTensor)
y_pred:predictedvalues(RaggedTensor)
y_pred_extra_dim:whethery_predhasanadditionaldimensioncomparedto
y_true
Returns:
Loss-functionresult.Adensetensoriftheoutputhasasingledimension
(per-batchlossvalue);araggedtensorotherwise.
"""
defrt_is_equiv_dense(rt):
"""ReturnstrueifthisRaggedTensorhasthesamerow_lengthsacross
allraggeddimensionsandthuscanbeconvertedtoadensetensor
withoutlossofinformation.
Args:
rt:RaggedTensor.
"""
returntf.reduce_all(
[
tf.equal(
tf.math.reduce_variance(
tf.cast(row_lens,backend.floatx())
),
tf.constant([0.0]),
)
forrow_lensinrt.nested_row_lengths()
]
)
def_convert_to_dense(inputs):
returntuple(
rt.to_tensor()ifisinstance(rt,tf.RaggedTensor)elsert
forrtininputs
)
def_call_loss(inputs,ragged_output):
"""Adapttheresulttoraggedordensetensoraccordingtotheexpected
outputtype.Thisisdonesothatallthereturnvaluesofthemap
operationhavethesametype.
"""
r=loss_fn(*inputs)
ifragged_outputandnotisinstance(r,tf.RaggedTensor):
r=tf.RaggedTensor.from_tensor(r)
elifnotragged_outputandisinstance(r,tf.RaggedTensor):
r=r.to_tensor()
returnr
def_wrapper(inputs,ragged_output):
_,y_pred=inputs
ifisinstance(y_pred,tf.RaggedTensor):
returntf.cond(
rt_is_equiv_dense(y_pred),
lambda:_call_loss(_convert_to_dense(inputs),ragged_output),
lambda:_call_loss(inputs,ragged_output),
)
returnloss_fn(*inputs)
ifnotisinstance(y_true,tf.RaggedTensor):
returnloss_fn(y_true,y_pred.to_tensor())
lshape=y_pred.shape.as_list()[1:-1]
iflen(lshape)>0:
spec=tf.RaggedTensorSpec(shape=lshape,dtype=y_pred.dtype)
else:
spec=tf.TensorSpec(shape=[],dtype=y_pred.dtype)
nested_splits_list=[rt.nested_row_splitsforrtin(y_true,y_pred)]
ify_pred_extra_dim:
#Thelastdimensionofacategoricalpredictionmayberaggedornot.
rdims=[len(slist)forslistinnested_splits_list]
ifrdims[0]==rdims[1]-1:
nested_splits_list[1]=nested_splits_list[1][:-1]
map_fn=functools.partial(_wrapper,ragged_output=len(lshape)>1)
assertion_list=ragged_util.assert_splits_match(nested_splits_list)
withtf.control_dependencies(assertion_list):
returnragged_map_ops.map_fn(map_fn,elems=(y_true,y_pred),dtype=spec)
@dispatch.dispatch_for_types(mean_squared_error,tf.RaggedTensor)
def_ragged_tensor_mse(y_true,y_pred):
"""ImplementssupportforhandlingRaggedTensors.
Args:
y_true:RaggedTensortruthvalues.shape=`[batch_size,d0,..dN]`.
y_pred:RaggedTensorpredictedvalues.shape=`[batch_size,d0,..dN]`.
Returns:
Meansquarederrorvalues.shape=`[batch_size,d0,..dN-1]`.
Whenthenumberofdimensionsofthebatchfeaturevector[d0,..dN]is
greaterthanonethereturnvalueisaRaggedTensor.OtherwiseaDense
tensorwithdimensions[batch_size]isreturned.
"""
return_ragged_tensor_apply_loss(mean_squared_error,y_true,y_pred)
@keras_export(
"keras.metrics.mean_absolute_error",
"keras.metrics.mae",
"keras.metrics.MAE",
"keras.losses.mean_absolute_error",
"keras.losses.mae",
"keras.losses.MAE",
)
@tf.__internal__.dispatch.add_dispatch_support
defmean_absolute_error(y_true,y_pred):
"""Computesthemeanabsoluteerrorbetweenlabelsandpredictions.
`loss=mean(abs(y_true-y_pred),axis=-1)`
Standaloneusage:
>>>y_true=np.random.randint(0,2,size=(2,3))
>>>y_pred=np.random.random(size=(2,3))
>>>loss=tf.keras.losses.mean_absolute_error(y_true,y_pred)
>>>assertloss.shape==(2,)
>>>assertnp.array_equal(
...loss.numpy(),np.mean(np.abs(y_true-y_pred),axis=-1))
Args:
y_true:Groundtruthvalues.shape=`[batch_size,d0,..dN]`.
y_pred:Thepredictedvalues.shape=`[batch_size,d0,..dN]`.
Returns:
Meanabsoluteerrorvalues.shape=`[batch_size,d0,..dN-1]`.
"""
y_pred=tf.convert_to_tensor(y_pred)
y_true=tf.cast(y_true,y_pred.dtype)
returnbackend.mean(tf.abs(y_pred-y_true),axis=-1)
@dispatch.dispatch_for_types(mean_absolute_error,tf.RaggedTensor)
def_ragged_tensor_mae(y_true,y_pred):
"""RaggedTensoradapterformean_absolute_error."""
return_ragged_tensor_apply_loss(mean_absolute_error,y_true,y_pred)
@keras_export(
"keras.metrics.mean_absolute_percentage_error",
"keras.metrics.mape",
"keras.metrics.MAPE",
"keras.losses.mean_absolute_percentage_error",
"keras.losses.mape",
"keras.losses.MAPE",
)
@tf.__internal__.dispatch.add_dispatch_support
defmean_absolute_percentage_error(y_true,y_pred):
"""Computesthemeanabsolutepercentageerrorbetween`y_true`&`y_pred`.
`loss=100*mean(abs((y_true-y_pred)/y_true),axis=-1)`
Standaloneusage:
>>>y_true=np.random.random(size=(2,3))
>>>y_true=np.maximum(y_true,1e-7)#Preventdivisionbyzero
>>>y_pred=np.random.random(size=(2,3))
>>>loss=tf.keras.losses.mean_absolute_percentage_error(y_true,y_pred)
>>>assertloss.shape==(2,)
>>>assertnp.array_equal(
...loss.numpy(),
...100.*np.mean(np.abs((y_true-y_pred)/y_true),axis=-1))
Args:
y_true:Groundtruthvalues.shape=`[batch_size,d0,..dN]`.
y_pred:Thepredictedvalues.shape=`[batch_size,d0,..dN]`.
Returns:
Meanabsolutepercentageerrorvalues.shape=`[batch_size,d0,..
dN-1]`.
"""
y_pred=tf.convert_to_tensor(y_pred)
y_true=tf.cast(y_true,y_pred.dtype)
diff=tf.abs(
(y_true-y_pred)/backend.maximum(tf.abs(y_true),backend.epsilon())
)
return100.0*backend.mean(diff,axis=-1)
@dispatch.dispatch_for_types(mean_absolute_percentage_error,tf.RaggedTensor)
def_ragged_tensor_mape(y_true,y_pred):
"""SupportRaggedTensors."""
return_ragged_tensor_apply_loss(
mean_absolute_percentage_error,y_true,y_pred
)
@keras_export(
"keras.metrics.mean_squared_logarithmic_error",
"keras.metrics.msle",
"keras.metrics.MSLE",
"keras.losses.mean_squared_logarithmic_error",
"keras.losses.msle",
"keras.losses.MSLE",
)
@tf.__internal__.dispatch.add_dispatch_support
defmean_squared_logarithmic_error(y_true,y_pred):
"""Computesthemeansquaredlogarithmicerrorbetween`y_true`&`y_pred`.
`loss=mean(square(log(y_true+1)-log(y_pred+1)),axis=-1)`
Standaloneusage:
>>>y_true=np.random.randint(0,2,size=(2,3))
>>>y_pred=np.random.random(size=(2,3))
>>>loss=tf.keras.losses.mean_squared_logarithmic_error(y_true,y_pred)
>>>assertloss.shape==(2,)
>>>y_true=np.maximum(y_true,1e-7)
>>>y_pred=np.maximum(y_pred,1e-7)
>>>assertnp.allclose(
...loss.numpy(),
...np.mean(
...np.square(np.log(y_true+1.)-np.log(y_pred+1.)),axis=-1))
Args:
y_true:Groundtruthvalues.shape=`[batch_size,d0,..dN]`.
y_pred:Thepredictedvalues.shape=`[batch_size,d0,..dN]`.
Returns:
Meansquaredlogarithmicerrorvalues.shape=`[batch_size,d0,..
dN-1]`.
"""
y_pred=tf.convert_to_tensor(y_pred)
y_true=tf.cast(y_true,y_pred.dtype)
first_log=tf.math.log(backend.maximum(y_pred,backend.epsilon())+1.0)
second_log=tf.math.log(backend.maximum(y_true,backend.epsilon())+1.0)
returnbackend.mean(
tf.math.squared_difference(first_log,second_log),axis=-1
)
@dispatch.dispatch_for_types(mean_squared_logarithmic_error,tf.RaggedTensor)
def_ragged_tensor_msle(y_true,y_pred):
"""ImplementssupportforhandlingRaggedTensors."""
return_ragged_tensor_apply_loss(
mean_squared_logarithmic_error,y_true,y_pred
)
def_maybe_convert_labels(y_true):
"""Convertsbinarylabelsinto-1/1."""
are_zeros=tf.equal(y_true,0)
are_ones=tf.equal(y_true,1)
is_binary=tf.reduce_all(tf.logical_or(are_zeros,are_ones))
def_convert_binary_labels():
#Convertthebinarylabelsto-1or1.
return2.0*y_true-1.0
updated_y_true=tf.__internal__.smart_cond.smart_cond(
is_binary,_convert_binary_labels,lambda:y_true
)
returnupdated_y_true
@keras_export("keras.metrics.squared_hinge","keras.losses.squared_hinge")
@tf.__internal__.dispatch.add_dispatch_support
defsquared_hinge(y_true,y_pred):
"""Computesthesquaredhingelossbetween`y_true`&`y_pred`.
`loss=mean(square(maximum(1-y_true*y_pred,0)),axis=-1)`
Standaloneusage:
>>>y_true=np.random.choice([-1,1],size=(2,3))
>>>y_pred=np.random.random(size=(2,3))
>>>loss=tf.keras.losses.squared_hinge(y_true,y_pred)
>>>assertloss.shape==(2,)
>>>assertnp.array_equal(
...loss.numpy(),
...np.mean(np.square(np.maximum(1.-y_true*y_pred,0.)),axis=-1))
Args:
y_true:Thegroundtruthvalues.`y_true`valuesareexpectedtobe-1or
1.Ifbinary(0or1)labelsareprovidedwewillconvertthemto-1or
1.shape=`[batch_size,d0,..dN]`.
y_pred:Thepredictedvalues.shape=`[batch_size,d0,..dN]`.
Returns:
Squaredhingelossvalues.shape=`[batch_size,d0,..dN-1]`.
"""
y_pred=tf.convert_to_tensor(y_pred)
y_true=tf.cast(y_true,y_pred.dtype)
y_true=_maybe_convert_labels(y_true)
returnbackend.mean(
tf.square(tf.maximum(1.0-y_true*y_pred,0.0)),axis=-1
)
@keras_export("keras.metrics.hinge","keras.losses.hinge")
@tf.__internal__.dispatch.add_dispatch_support
defhinge(y_true,y_pred):
"""Computesthehingelossbetween`y_true`&`y_pred`.
`loss=mean(maximum(1-y_true*y_pred,0),axis=-1)`
Standaloneusage:
>>>y_true=np.random.choice([-1,1],size=(2,3))
>>>y_pred=np.random.random(size=(2,3))
>>>loss=tf.keras.losses.hinge(y_true,y_pred)
>>>assertloss.shape==(2,)
>>>assertnp.array_equal(
...loss.numpy(),
...np.mean(np.maximum(1.-y_true*y_pred,0.),axis=-1))
Args:
y_true:Thegroundtruthvalues.`y_true`valuesareexpectedtobe-1or
1.Ifbinary(0or1)labelsareprovidedtheywillbeconvertedto-1
or1.shape=`[batch_size,d0,..dN]`.
y_pred:Thepredictedvalues.shape=`[batch_size,d0,..dN]`.
Returns:
Hingelossvalues.shape=`[batch_size,d0,..dN-1]`.
"""
y_pred=tf.convert_to_tensor(y_pred)
y_true=tf.cast(y_true,y_pred.dtype)
y_true=_maybe_convert_labels(y_true)
returnbackend.mean(tf.maximum(1.0-y_true*y_pred,0.0),axis=-1)
@keras_export("keras.losses.categorical_hinge")
@tf.__internal__.dispatch.add_dispatch_support
defcategorical_hinge(y_true,y_pred):
"""Computesthecategoricalhingelossbetween`y_true`&`y_pred`.
`loss=maximum(neg-pos+1,0)`
where`neg=maximum((1-y_true)*y_pred)andpos=sum(y_true*y_pred)`
Standaloneusage:
>>>y_true=np.random.randint(0,3,size=(2,))
>>>y_true=tf.keras.utils.to_categorical(y_true,num_classes=3)
>>>y_pred=np.random.random(size=(2,3))
>>>loss=tf.keras.losses.categorical_hinge(y_true,y_pred)
>>>assertloss.shape==(2,)
>>>pos=np.sum(y_true*y_pred,axis=-1)
>>>neg=np.amax((1.-y_true)*y_pred,axis=-1)
>>>assertnp.array_equal(loss.numpy(),np.maximum(0.,neg-pos+1.))
Args:
y_true:Thegroundtruthvalues.`y_true`valuesareexpectedtobe
either`{-1,+1}`or`{0,1}`(i.e.aone-hot-encodedtensor).
y_pred:Thepredictedvalues.
Returns:
Categoricalhingelossvalues.
"""
y_pred=tf.convert_to_tensor(y_pred)
y_true=tf.cast(y_true,y_pred.dtype)
pos=tf.reduce_sum(y_true*y_pred,axis=-1)
neg=tf.reduce_max((1.0-y_true)*y_pred,axis=-1)
zero=tf.cast(0.0,y_pred.dtype)
returntf.maximum(neg-pos+1.0,zero)
@keras_export("keras.losses.huber",v1=[])
@tf.__internal__.dispatch.add_dispatch_support
defhuber(y_true,y_pred,delta=1.0):
"""ComputesHuberlossvalue.
Foreachvaluexin`error=y_true-y_pred`:
```
loss=0.5*x^2if|x|<=d
loss=d*|x|-0.5*d^2if|x|>d
```
wheredis`delta`.See:https://en.wikipedia.org/wiki/Huber_loss
Args:
y_true:tensoroftruetargets.
y_pred:tensorofpredictedtargets.
delta:Afloat,thepointwheretheHuberlossfunctionchangesfroma
quadratictolinear.
Returns:
Tensorwithonescalarlossentrypersample.
"""
y_pred=tf.cast(y_pred,dtype=backend.floatx())
y_true=tf.cast(y_true,dtype=backend.floatx())
delta=tf.cast(delta,dtype=backend.floatx())
error=tf.subtract(y_pred,y_true)
abs_error=tf.abs(error)
half=tf.convert_to_tensor(0.5,dtype=abs_error.dtype)
returnbackend.mean(
tf.where(
abs_error<=delta,
half*tf.square(error),
delta*abs_error-half*tf.square(delta),
),
axis=-1,
)
@keras_export(
"keras.losses.log_cosh",
"keras.losses.logcosh",
"keras.metrics.log_cosh",
"keras.metrics.logcosh",
)
@tf.__internal__.dispatch.add_dispatch_support
deflog_cosh(y_true,y_pred):
"""Logarithmofthehyperboliccosineofthepredictionerror.
`log(cosh(x))`isapproximatelyequalto`(x**2)/2`forsmall`x`and
to`abs(x)-log(2)`forlarge`x`.Thismeansthat'logcosh'worksmostly
likethemeansquarederror,butwillnotbesostronglyaffectedbythe
occasionalwildlyincorrectprediction.
Standaloneusage:
>>>y_true=np.random.random(size=(2,3))
>>>y_pred=np.random.random(size=(2,3))
>>>loss=tf.keras.losses.logcosh(y_true,y_pred)
>>>assertloss.shape==(2,)
>>>x=y_pred-y_true
>>>assertnp.allclose(
...loss.numpy(),
...np.mean(x+np.log(np.exp(-2.*x)+1.)-tf.math.log(2.),
...axis=-1),
...atol=1e-5)
Args:
y_true:Groundtruthvalues.shape=`[batch_size,d0,..dN]`.
y_pred:Thepredictedvalues.shape=`[batch_size,d0,..dN]`.
Returns:
Logcosherrorvalues.shape=`[batch_size,d0,..dN-1]`.
"""
y_pred=tf.convert_to_tensor(y_pred)
y_true=tf.cast(y_true,y_pred.dtype)
def_logcosh(x):
return(
x+tf.math.softplus(-2.0*x)-tf.cast(tf.math.log(2.0),x.dtype)
)
returnbackend.mean(_logcosh(y_pred-y_true),axis=-1)
@keras_export(
"keras.metrics.categorical_crossentropy",
"keras.losses.categorical_crossentropy",
)
@tf.__internal__.dispatch.add_dispatch_support
defcategorical_crossentropy(
y_true,y_pred,from_logits=False,label_smoothing=0.0,axis=-1
):
"""Computesthecategoricalcrossentropyloss.
Standaloneusage:
>>>y_true=[[0,1,0],[0,0,1]]
>>>y_pred=[[0.05,0.95,0],[0.1,0.8,0.1]]
>>>loss=tf.keras.losses.categorical_crossentropy(y_true,y_pred)
>>>assertloss.shape==(2,)
>>>loss.numpy()
array([0.0513,2.303],dtype=float32)
Args:
y_true:Tensorofone-hottruetargets.
y_pred:Tensorofpredictedtargets.
from_logits:Whether`y_pred`isexpectedtobealogitstensor.By
default,weassumethat`y_pred`encodesaprobabilitydistribution.
label_smoothing:Floatin[0,1].If>`0`thensmooththelabels.For
example,if`0.1`,use`0.1/num_classes`fornon-targetlabels
and`0.9+0.1/num_classes`fortargetlabels.
axis:Defaultsto-1.Thedimensionalongwhichtheentropyis
computed.
Returns:
Categoricalcrossentropylossvalue.
"""
ifisinstance(axis,bool):
raiseValueError(
"`axis`mustbeoftype`int`."
f"Received:axis={axis}oftype{type(axis)}"
)
y_pred=tf.convert_to_tensor(y_pred)
y_true=tf.cast(y_true,y_pred.dtype)
label_smoothing=tf.convert_to_tensor(label_smoothing,dtype=y_pred.dtype)
def_smooth_labels():
num_classes=tf.cast(tf.shape(y_true)[-1],y_pred.dtype)
returny_true*(1.0-label_smoothing)+(
label_smoothing/num_classes
)
y_true=tf.__internal__.smart_cond.smart_cond(
label_smoothing,_smooth_labels,lambda:y_true
)
returnbackend.categorical_crossentropy(
y_true,y_pred,from_logits=from_logits,axis=axis
)
@dispatch.dispatch_for_types(categorical_crossentropy,tf.RaggedTensor)
def_ragged_tensor_categorical_crossentropy(
y_true,y_pred,from_logits=False,label_smoothing=0.0,axis=-1
):
"""ImplementssupportforhandlingRaggedTensors.
Args:
y_true:Tensorofone-hottruetargets.
y_pred:Tensorofpredictedtargets.
from_logits:Whether`y_pred`isexpectedtobealogitstensor.By
default,weassumethat`y_pred`encodesaprobabilitydistribution.
label_smoothing:Floatin[0,1].If>`0`thensmooththelabels.For
example,if`0.1`,use`0.1/num_classes`fornon-targetlabels
and`0.9+0.1/num_classes`fortargetlabels.
axis:Theaxisalongwhichtocomputecrossentropy(thefeaturesaxis).
Defaultsto-1.
Returns:
Categoricalcrossentropylossvalue.
Expectedshape:(batch,sequence_len,n_classes)withsequence_len
beingvariableperbatch.
Returnshape:(batch,sequence_len).
WhenusedbyCategoricalCrossentropy()withthedefaultreduction
(SUM_OVER_BATCH_SIZE),thereductionaveragesthelossoverthe
numberofelementsindependentofthebatch.E.g.iftheRaggedTensor
has2batcheswith[2,1]valuesrespectivelytheresultinglossis
thesumoftheindividuallossvaluesdividedby3.
"""
fn=functools.partial(
categorical_crossentropy,
from_logits=from_logits,
label_smoothing=label_smoothing,
axis=axis,
)
return_ragged_tensor_apply_loss(fn,y_true,y_pred)
@keras_export(
"keras.metrics.sparse_categorical_crossentropy",
"keras.losses.sparse_categorical_crossentropy",
)
@tf.__internal__.dispatch.add_dispatch_support
defsparse_categorical_crossentropy(
y_true,y_pred,from_logits=False,axis=-1,ignore_class=None
):
"""Computesthesparsecategoricalcrossentropyloss.
Standaloneusage:
>>>y_true=[1,2]
>>>y_pred=[[0.05,0.95,0],[0.1,0.8,0.1]]
>>>loss=tf.keras.losses.sparse_categorical_crossentropy(y_true,y_pred)
>>>assertloss.shape==(2,)
>>>loss.numpy()
array([0.0513,2.303],dtype=float32)
>>>y_true=[[[0,2],
...[-1,-1]],
...[[0,2],
...[-1,-1]]]
>>>y_pred=[[[[1.0,0.0,0.0],[0.0,0.0,1.0]],
...[[0.2,0.5,0.3],[0.0,1.0,0.0]]],
...[[[1.0,0.0,0.0],[0.0,0.5,0.5]],
...[[0.2,0.5,0.3],[0.0,1.0,0.0]]]]
>>>loss=tf.keras.losses.sparse_categorical_crossentropy(
...y_true,y_pred,ignore_class=-1)
>>>loss.numpy()
array([[[2.3841855e-07,2.3841855e-07],
[0.0000000e+00,0.0000000e+00]],
[[2.3841855e-07,6.9314730e-01],
[0.0000000e+00,0.0000000e+00]]],dtype=float32)
Args:
y_true:Groundtruthvalues.
y_pred:Thepredictedvalues.
from_logits:Whether`y_pred`isexpectedtobealogitstensor.By
default,weassumethat`y_pred`encodesaprobabilitydistribution.
axis:Defaultsto-1.Thedimensionalongwhichtheentropyis
computed.
ignore_class:Optionalinteger.TheIDofaclasstobeignoredduring
losscomputation.Thisisuseful,forexample,insegmentation
problemsfeaturinga"void"class(commonly-1or255)insegmentation
maps.Bydefault(`ignore_class=None`),allclassesareconsidered.
Returns:
Sparsecategoricalcrossentropylossvalue.
"""
returnbackend.sparse_categorical_crossentropy(
y_true,
y_pred,
from_logits=from_logits,
ignore_class=ignore_class,
axis=axis,
)
@dispatch.dispatch_for_types(sparse_categorical_crossentropy,tf.RaggedTensor)
def_ragged_tensor_sparse_categorical_crossentropy(
y_true,y_pred,from_logits=False,axis=-1,ignore_class=None
):
"""ImplementssupportforhandlingRaggedTensors.
Expectedy_predshape:(batch,sequence_len,n_classes)withsequence_len
beingvariableperbatch.
Returnshape:(batch,sequence_len).
WhenusedbySparseCategoricalCrossentropy()withthedefaultreduction
(SUM_OVER_BATCH_SIZE),thereductionaveragesthelossoverthe
numberofelementsindependentofthebatch.E.g.iftheRaggedTensor
has2batcheswith[2,1]valuesrespectively,theresultinglossis
thesumoftheindividuallossvaluesdividedby3.
"""
fn=functools.partial(
sparse_categorical_crossentropy,
from_logits=from_logits,
ignore_class=ignore_class,
axis=axis,
)
return_ragged_tensor_apply_loss(fn,y_true,y_pred,y_pred_extra_dim=True)
@keras_export(
"keras.metrics.binary_crossentropy","keras.losses.binary_crossentropy"
)
@tf.__internal__.dispatch.add_dispatch_support
defbinary_crossentropy(
y_true,y_pred,from_logits=False,label_smoothing=0.0,axis=-1
):
"""Computesthebinarycrossentropyloss.
Standaloneusage:
>>>y_true=[[0,1],[0,0]]
>>>y_pred=[[0.6,0.4],[0.4,0.6]]
>>>loss=tf.keras.losses.binary_crossentropy(y_true,y_pred)
>>>assertloss.shape==(2,)
>>>loss.numpy()
array([0.916,0.714],dtype=float32)
Args:
y_true:Groundtruthvalues.shape=`[batch_size,d0,..dN]`.
y_pred:Thepredictedvalues.shape=`[batch_size,d0,..dN]`.
from_logits:Whether`y_pred`isexpectedtobealogitstensor.By
default,weassumethat`y_pred`encodesaprobabilitydistribution.
label_smoothing:Floatin[0,1].If>`0`thensmooththelabelsby
squeezingthemtowards0.5Thatis,using`1.-0.5*label_smoothing`
forthetargetclassand`0.5*label_smoothing`forthenon-target
class.
axis:Theaxisalongwhichthemeaniscomputed.Defaultsto-1.
Returns:
Binarycrossentropylossvalue.shape=`[batch_size,d0,..dN-1]`.
"""
y_pred=tf.convert_to_tensor(y_pred)
y_true=tf.cast(y_true,y_pred.dtype)
label_smoothing=tf.convert_to_tensor(label_smoothing,dtype=y_pred.dtype)
def_smooth_labels():
returny_true*(1.0-label_smoothing)+0.5*label_smoothing
y_true=tf.__internal__.smart_cond.smart_cond(
label_smoothing,_smooth_labels,lambda:y_true
)
returnbackend.mean(
backend.binary_crossentropy(y_true,y_pred,from_logits=from_logits),
axis=axis,
)
@dispatch.dispatch_for_types(binary_crossentropy,tf.RaggedTensor)
def_ragged_tensor_binary_crossentropy(
y_true,y_pred,from_logits=False,label_smoothing=0.0,axis=-1
):
"""ImplementssupportforhandlingRaggedTensors.
Args:
y_true:Tensorofone-hottruetargets.
y_pred:Tensorofpredictedtargets.
from_logits:Whether`y_pred`isexpectedtobealogitstensor.By
default,weassumethat`y_pred`encodesaprobabilitydistribution.
label_smoothing:Floatin[0,1].If>`0`thensmooththelabels.For
example,if`0.1`,use`0.1/num_classes`fornon-targetlabels
and`0.9+0.1/num_classes`fortargetlabels.
axis:Axisalongwhichtocomputecrossentropy.
Returns:
Binarycrossentropylossvalue.
Expectedshape:(batch,sequence_len)withsequence_lenbeingvariable
perbatch.
Returnshape:(batch,);returnstheperbatchmeanofthelossvalues.
WhenusedbyBinaryCrossentropy()withthedefaultreduction
(SUM_OVER_BATCH_SIZE),thereductionaveragestheperbatchlossesover
thenumberofbatches.
"""
fn=functools.partial(
binary_crossentropy,
from_logits=from_logits,
label_smoothing=label_smoothing,
axis=axis,
)
return_ragged_tensor_apply_loss(fn,y_true,y_pred)
@keras_export(
"keras.metrics.binary_focal_crossentropy",
"keras.losses.binary_focal_crossentropy",
)
@tf.__internal__.dispatch.add_dispatch_support
defbinary_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,
):
"""Computesthebinaryfocalcrossentropyloss.
Accordingto[Linetal.,2018](https://arxiv.org/pdf/1708.02002.pdf),it
helpstoapplyafocalfactortodown-weighteasyexamplesandfocusmoreon
hardexamples.Bydefault,thefocaltensoriscomputedasfollows:
`focal_factor=(1-output)**gamma`forclass1
`focal_factor=output**gamma`forclass0
where`gamma`isafocusingparameter.When`gamma`=0,thereisnofocal
effectonthebinarycrossentropyloss.
If`apply_class_balancing==True`,thisfunctionalsotakesintoaccounta
weightbalancingfactorforthebinaryclasses0and1asfollows:
`weight=alpha`forclass1(`target==1`)
`weight=1-alpha`forclass0
where`alpha`isafloatintherangeof`[0,1]`.
Standaloneusage:
>>>y_true=[[0,1],[0,0]]
>>>y_pred=[[0.6,0.4],[0.4,0.6]]
>>>loss=tf.keras.losses.binary_focal_crossentropy(y_true,y_pred,
...gamma=2)
>>>assertloss.shape==(2,)
>>>loss.numpy()
array([0.330,0.206],dtype=float32)
Args:
y_true:Groundtruthvalues,ofshape`(batch_size,d0,..dN)`.
y_pred:Thepredictedvalues,ofshape`(batch_size,d0,..dN)`.
apply_class_balancing:Abool,whethertoapplyweightbalancingonthe
binaryclasses0and1.
alpha:Aweightbalancingfactorforclass1,defaultis`0.25`as
mentionedinthereference.Theweightforclass0is`1.0-alpha`.
gamma:Afocusingparameter,defaultis`2.0`asmentionedinthe
reference.
from_logits:Whether`y_pred`isexpectedtobealogitstensor.By
default,weassumethat`y_pred`encodesaprobabilitydistribution.
label_smoothing:Floatin`[0,1]`.Ifhigherthan0thensmooththe
labelsbysqueezingthemtowards`0.5`,i.e.,using`1.-0.5*
label_smoothing`forthetargetclassand`0.5*label_smoothing`for
thenon-targetclass.
axis:Theaxisalongwhichthemeaniscomputed.Defaultsto`-1`.
Returns:
Binaryfocalcrossentropylossvalue.shape=`[batch_size,d0,..dN-1]`.
"""
y_pred=tf.convert_to_tensor(y_pred)
y_true=tf.cast(y_true,y_pred.dtype)
label_smoothing=tf.convert_to_tensor(label_smoothing,dtype=y_pred.dtype)
def_smooth_labels():
returny_true*(1.0-label_smoothing)+0.5*label_smoothing
y_true=tf.__internal__.smart_cond.smart_cond(
label_smoothing,_smooth_labels,lambda:y_true
)
returnbackend.mean(
backend.binary_focal_crossentropy(
target=y_true,
output=y_pred,
apply_class_balancing=apply_class_balancing,
alpha=alpha,
gamma=gamma,
from_logits=from_logits,
),
axis=axis,
)
@dispatch.dispatch_for_types(binary_focal_crossentropy,tf.RaggedTensor)
def_ragged_tensor_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,
):
"""ImplementssupportforhandlingRaggedTensors.
Expectedshape:`(batch,sequence_len)`withsequence_lenbeingvariableper
batch.
Returnshape:`(batch,)`;returnstheperbatchmeanofthelossvalues.
WhenusedbyBinaryFocalCrossentropy()withthedefaultreduction
(SUM_OVER_BATCH_SIZE),thereductionaveragestheperbatchlossesover
thenumberofbatches.
Args:
y_true:Tensorofone-hottruetargets.
y_pred:Tensorofpredictedtargets.
apply_class_balancing:Abool,whethertoapplyweightbalancingonthe
binaryclasses0and1.
alpha:Aweightbalancingfactorforclass1,defaultis`0.25`as
mentionedinthereference[Linetal.,2018](
https://arxiv.org/pdf/1708.02002.pdf).Theweightforclass0is
`1.0-alpha`.
gamma:Afocusingparameter,defaultis`2.0`asmentionedinthe
reference.
from_logits:Whether`y_pred`isexpectedtobealogitstensor.By
default,weassumethat`y_pred`encodesaprobabilitydistribution.
label_smoothing:Floatin`[0,1]`.If>`0`thensmooththelabels.For
example,if`0.1`,use`0.1/num_classes`fornon-targetlabels
and`0.9+0.1/num_classes`fortargetlabels.
axis:Axisalongwhichtocomputecrossentropy.
Returns:
Binaryfocalcrossentropylossvalue.
"""
fn=functools.partial(
binary_focal_crossentropy,
apply_class_balancing=apply_class_balancing,
alpha=alpha,
gamma=gamma,
from_logits=from_logits,
label_smoothing=label_smoothing,
axis=axis,
)
return_ragged_tensor_apply_loss(fn,y_true,y_pred)
@keras_export(
"keras.metrics.kl_divergence",
"keras.metrics.kullback_leibler_divergence",
"keras.metrics.kld",
"keras.metrics.KLD",
"keras.losses.kl_divergence",
"keras.losses.kullback_leibler_divergence",
"keras.losses.kld",
"keras.losses.KLD",
)
@tf.__internal__.dispatch.add_dispatch_support
defkl_divergence(y_true,y_pred):
"""ComputesKullback-Leiblerdivergencelossbetween`y_true`&`y_pred`.
`loss=y_true*log(y_true/y_pred)`
See:https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence
Standaloneusage:
>>>y_true=np.random.randint(0,2,size=(2,3)).astype(np.float64)
>>>y_pred=np.random.random(size=(2,3))
>>>loss=tf.keras.losses.kullback_leibler_divergence(y_true,y_pred)
>>>assertloss.shape==(2,)
>>>y_true=tf.keras.backend.clip(y_true,1e-7,1)
>>>y_pred=tf.keras.backend.clip(y_pred,1e-7,1)
>>>assertnp.array_equal(
...loss.numpy(),np.sum(y_true*np.log(y_true/y_pred),axis=-1))
Args:
y_true:Tensoroftruetargets.
y_pred:Tensorofpredictedtargets.
Returns:
A`Tensor`withloss.
Raises:
TypeError:If`y_true`cannotbecasttothe`y_pred.dtype`.
"""
y_pred=tf.convert_to_tensor(y_pred)
y_true=tf.cast(y_true,y_pred.dtype)
y_true=backend.clip(y_true,backend.epsilon(),1)
y_pred=backend.clip(y_pred,backend.epsilon(),1)
returntf.reduce_sum(y_true*tf.math.log(y_true/y_pred),axis=-1)
@keras_export("keras.metrics.poisson","keras.losses.poisson")
@tf.__internal__.dispatch.add_dispatch_support
defpoisson(y_true,y_pred):
"""ComputesthePoissonlossbetweeny_trueandy_pred.
ThePoissonlossisthemeanoftheelementsofthe`Tensor`
`y_pred-y_true*log(y_pred)`.
Standaloneusage:
>>>y_true=np.random.randint(0,2,size=(2,3))
>>>y_pred=np.random.random(size=(2,3))
>>>loss=tf.keras.losses.poisson(y_true,y_pred)
>>>assertloss.shape==(2,)
>>>y_pred=y_pred+1e-7
>>>assertnp.allclose(
...loss.numpy(),np.mean(y_pred-y_true*np.log(y_pred),axis=-1),
...atol=1e-5)
Args:
y_true:Groundtruthvalues.shape=`[batch_size,d0,..dN]`.
y_pred:Thepredictedvalues.shape=`[batch_size,d0,..dN]`.
Returns:
Poissonlossvalue.shape=`[batch_size,d0,..dN-1]`.
Raises:
InvalidArgumentError:If`y_true`and`y_pred`haveincompatibleshapes.
"""
y_pred=tf.convert_to_tensor(y_pred)
y_true=tf.cast(y_true,y_pred.dtype)
returnbackend.mean(
y_pred-y_true*tf.math.log(y_pred+backend.epsilon()),axis=-1
)
@keras_export(
"keras.losses.cosine_similarity",
v1=[
"keras.metrics.cosine_proximity",
"keras.metrics.cosine",
"keras.losses.cosine_proximity",
"keras.losses.cosine",
"keras.losses.cosine_similarity",
],
)
@tf.__internal__.dispatch.add_dispatch_support
defcosine_similarity(y_true,y_pred,axis=-1):
"""Computesthecosinesimilaritybetweenlabelsandpredictions.
Notethatitisanumberbetween-1and1.Whenitisanegativenumber
between-1and0,0indicatesorthogonalityandvaluescloserto-1
indicategreatersimilarity.Thevaluescloserto1indicategreater
dissimilarity.Thismakesitusableasalossfunctioninasetting
whereyoutrytomaximizetheproximitybetweenpredictionsand
targets.Ifeither`y_true`or`y_pred`isazerovector,cosine
similaritywillbe0regardlessoftheproximitybetweenpredictions
andtargets.
`loss=-sum(l2_norm(y_true)*l2_norm(y_pred))`
Standaloneusage:
>>>y_true=[[0.,1.],[1.,1.],[1.,1.]]
>>>y_pred=[[1.,0.],[1.,1.],[-1.,-1.]]
>>>loss=tf.keras.losses.cosine_similarity(y_true,y_pred,axis=1)
>>>loss.numpy()
array([-0.,-0.999,0.999],dtype=float32)
Args:
y_true:Tensoroftruetargets.
y_pred:Tensorofpredictedtargets.
axis:Axisalongwhichtodeterminesimilarity.
Returns:
Cosinesimilaritytensor.
"""
y_true=tf.linalg.l2_normalize(y_true,axis=axis)
y_pred=tf.linalg.l2_normalize(y_pred,axis=axis)
return-tf.reduce_sum(y_true*y_pred,axis=axis)
@keras_export("keras.losses.CosineSimilarity")
classCosineSimilarity(LossFunctionWrapper):
"""Computesthecosinesimilaritybetweenlabelsandpredictions.
Notethatitisanumberbetween-1and1.Whenitisanegativenumber
between-1and0,0indicatesorthogonalityandvaluescloserto-1
indicategreatersimilarity.Thevaluescloserto1indicategreater
dissimilarity.Thismakesitusableasalossfunctioninasetting
whereyoutrytomaximizetheproximitybetweenpredictionsandtargets.
Ifeither`y_true`or`y_pred`isazerovector,cosinesimilaritywillbe0
regardlessoftheproximitybetweenpredictionsandtargets.
`loss=-sum(l2_norm(y_true)*l2_norm(y_pred))`
Standaloneusage:
>>>y_true=[[0.,1.],[1.,1.]]
>>>y_pred=[[1.,0.],[1.,1.]]
>>>#Using'auto'/'sum_over_batch_size'reductiontype.
>>>cosine_loss=tf.keras.losses.CosineSimilarity(axis=1)
>>>#l2_norm(y_true)=[[0.,1.],[1./1.414,1./1.414]]
>>>#l2_norm(y_pred)=[[1.,0.],[1./1.414,1./1.414]]
>>>#l2_norm(y_true).l2_norm(y_pred)=[[0.,0.],[0.5,0.5]]
>>>#loss=mean(sum(l2_norm(y_true).l2_norm(y_pred),axis=1))
>>>#=-((0.+0.)+(0.5+0.5))/2
>>>cosine_loss(y_true,y_pred).numpy()
-0.5
>>>#Callingwith'sample_weight'.
>>>cosine_loss(y_true,y_pred,sample_weight=[0.8,0.2]).numpy()
-0.0999
>>>#Using'sum'reductiontype.
>>>cosine_loss=tf.keras.losses.CosineSimilarity(axis=1,
...reduction=tf.keras.losses.Reduction.SUM)
>>>cosine_loss(y_true,y_pred).numpy()
-0.999
>>>#Using'none'reductiontype.
>>>cosine_loss=tf.keras.losses.CosineSimilarity(axis=1,
...reduction=tf.keras.losses.Reduction.NONE)
>>>cosine_loss(y_true,y_pred).numpy()
array([-0.,-0.999],dtype=float32)
Usagewiththe`compile()`API:
```python
model.compile(optimizer='sgd',
loss=tf.keras.losses.CosineSimilarity(axis=1))
```
Args:
axis:Theaxisalongwhichthecosinesimilarityiscomputed
(thefeaturesaxis).Defaultsto-1.
reduction:Typeof`tf.keras.losses.Reduction`toapplytoloss.
Defaultvalueis`AUTO`.`AUTO`indicatesthatthereductionoptionwill
bedeterminedbytheusagecontext.Foralmostallcasesthisdefaults
to`SUM_OVER_BATCH_SIZE`.Whenusedwith`tf.distribute.Strategy`,
outsideofbuilt-intrainingloopssuchas`tf.keras``compile`and
`fit`,using`AUTO`or`SUM_OVER_BATCH_SIZE`willraiseanerror.Please
seethiscustomtraining[tutorial](
https://www.tensorflow.org/tutorials/distribute/custom_training)for
moredetails.
name:Optionalnamefortheinstance.
"""
def__init__(
self,
axis=-1,
reduction=losses_utils.ReductionV2.AUTO,
name="cosine_similarity",
):
super().__init__(
cosine_similarity,reduction=reduction,name=name,axis=axis
)
#Aliases.
bce=BCE=binary_crossentropy
mse=MSE=mean_squared_error
mae=MAE=mean_absolute_error
mape=MAPE=mean_absolute_percentage_error
msle=MSLE=mean_squared_logarithmic_error
kld=KLD=kullback_leibler_divergence=kl_divergence
logcosh=log_cosh
huber_loss=huber
defis_categorical_crossentropy(loss):
result=(
isinstance(loss,CategoricalCrossentropy)
or(
isinstance(loss,LossFunctionWrapper)
andloss.fn==categorical_crossentropy
)
or(
hasattr(loss,"__name__")
andloss.__name__=="categorical_crossentropy"
)
or(loss=="categorical_crossentropy")
)
returnresult
@keras_export("keras.losses.serialize")
defserialize(loss):
"""Serializeslossfunctionor`Loss`instance.
Args:
loss:AKeras`Loss`instanceoralossfunction.
Returns:
Lossconfigurationdictionary.
"""
returnserialize_keras_object(loss)
@keras_export("keras.losses.deserialize")
defdeserialize(name,custom_objects=None):
"""Deserializesaserializedlossclass/functioninstance.
Args:
name:Lossconfiguration.
custom_objects:Optionaldictionarymappingnames(strings)tocustom
objects(classesandfunctions)tobeconsideredduring
deserialization.
Returns:
AKeras`Loss`instanceoralossfunction.
"""
returndeserialize_keras_object(
name,
module_objects=globals(),
custom_objects=custom_objects,
printable_module_name="lossfunction",
)
@keras_export("keras.losses.get")
defget(identifier):
"""RetrievesaKeraslossasa`function`/`Loss`classinstance.
The`identifier`maybethestringnameofalossfunctionor`Loss`class.
>>>loss=tf.keras.losses.get("categorical_crossentropy")
>>>type(loss)
>>>loss=tf.keras.losses.get("CategoricalCrossentropy")
>>>type(loss)
Youcanalsospecify`config`ofthelosstothisfunctionbypassingdict
containing`class_name`and`config`asanidentifier.Alsonotethatthe
`class_name`mustmaptoa`Loss`class
>>>identifier={"class_name":"CategoricalCrossentropy",
..."config":{"from_logits":True}}
>>>loss=tf.keras.losses.get(identifier)
>>>type(loss)
Args:
identifier:Alossidentifier.OneofNoneorstringnameofaloss
function/classorlossconfigurationdictionaryoralossfunctionora
lossclassinstance.
Returns:
AKeraslossasa`function`/`Loss`classinstance.
Raises:
ValueError:If`identifier`cannotbeinterpreted.
"""
ifidentifierisNone:
returnNone
ifisinstance(identifier,str):
identifier=str(identifier)
returndeserialize(identifier)
ifisinstance(identifier,dict):
returndeserialize(identifier)
ifcallable(identifier):
returnidentifier
raiseValueError(
f"Couldnotinterpretlossfunctionidentifier:{identifier}"
)
LABEL_DTYPES_FOR_LOSSES={
tf.compat.v1.losses.sparse_softmax_cross_entropy:"int32",
sparse_categorical_crossentropy:"int32",
}
Copylines
Copypermalink
Viewgitblame
Referenceinnewissue
Go
Youcan’tperformthatactionatthistime.
Yousignedinwithanothertaborwindow.Reloadtorefreshyoursession.
Yousignedoutinanothertaborwindow.Reloadtorefreshyoursession.