tf.keras.losses.Circle

Computes Circle Loss between integer labels and L2-normalized embeddings.

Inherits From: Loss

This is a metric learning loss designed to minimize within-class distance and maximize between-class distance in a flexible manner by dynamically adjusting the penalty strength based on optimization status of each similarity score.

To use Circle Loss effectively, the model should output embeddings without an activation function (such as a Dense layer with activation=None) followed by UnitNormalization layer to ensure unit-norm embeddings.

gamma Scaling factor that determines the largest scale of each similarity score. Defaults to 80.
margin The relaxation factor, below this distance, negatives are up weighted and positives are down weighted. Similarly, above this distance negatives are down weighted and positive are up weighted. Defaults to 0.4.
remove_diagonal Boolean, whether to remove self-similarities from the positive mask. Defaults to True.
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", "mean", "mean_with_sample_weight" or None. "sum" sums the loss, "sum_over_batch_size" and "mean" sum the loss and divide by the sample size, and "mean_with_sample_weight" sums the loss and divides by the sum of the sample weights. "none" and None perform no aggregation. Defaults to "sum_over_batch_size".
name Optional name for the loss instance.
dtype The dtype of the loss's computations. Defaults to None, which means using keras.backend.floatx(). keras.backend.floatx() is a "float32" unless set to different value (via keras.backend.set_floatx()). If a keras.DTypePolicy is provided, then the compute_dtype will be utilized.

Examples:

Usage with the compile() API:

model = models.Sequential([
    keras.layers.Input(shape=(224, 224, 3)),
    keras.layers.Conv2D(16, (3, 3), activation='relu'),
    keras.layers.Flatten(),
    keras.layers.Dense(64, activation=None),  # No activation
    keras.layers.UnitNormalization()  # L2 normalization
])

model.compile(optimizer="adam", loss=keras.losses.Circle())

Reference:

dtype

Methods

call

View source

from_config

View source

get_config

View source

__call__

View source

Call self as a function.