Skip to content

Module kerod.core.losses

None

None

View Source
import tensorflow as tf

from tensorflow.keras.losses import Loss

class L1Loss(Loss):

    def call(self, y_true, y_pred):

        return tf.norm(y_true - y_pred, ord=1, axis=-1)

Classes

L1Loss

class L1Loss(
    reduction='auto',
    name=None
)

To be implemented by subclasses: * call(): Contains the logic for loss calculation using y_true, y_pred.

Example subclass implementation:

class MeanSquaredError(Loss):

  def call(self, y_true, y_pred):
    y_pred = tf.convert_to_tensor_v2(y_pred)
    y_true = tf.cast(y_true, y_pred.dtype)
    return tf.reduce_mean(math_ops.square(y_pred - y_true), axis=-1)

When used with tf.distribute.Strategy, outside of built-in training loops such as tf.keras compile and fit, please use 'SUM' or 'NONE' reduction types, and reduce losses explicitly in your training loop. Using 'AUTO' or 'SUM_OVER_BATCH_SIZE' will raise an error.

Please see this custom training tutorial for more details on this.

You can implement 'SUM_OVER_BATCH_SIZE' using global batch size like:

with strategy.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))

Ancestors (in MRO)

  • tensorflow.python.keras.losses.Loss

Static methods

from_config

def from_config(
    config
)

Instantiates a Loss from its config (output of get_config()).

Parameters:

Name Description
config Output of get_config().

Returns:

Type Description
None A Loss instance.
View Source
  @classmethod

  def from_config(cls, config):

    """Instantiates a `Loss` from its config (output of `get_config()`).

    Args:

        config: Output of `get_config()`.

    Returns:

        A `Loss` instance.

    """

    return cls(**config)

Methods

call

def call(
    self,
    y_true,
    y_pred
)
View Source
    def call(self, y_true, y_pred):

        return tf.norm(y_true - y_pred, ord=1, axis=-1)

get_config

def get_config(
    self
)

Returns the config dictionary for a Loss instance.

View Source
  def get_config(self):

    """Returns the config dictionary for a `Loss` instance."""

    return {'reduction': self.reduction, 'name': self.name}