Computes the multi similarity loss in an online fashion.
Inherits From: MetricLoss
TFSimilarity.losses.MultiSimilarityLoss(
alpha: float = 2.0,
beta: float = 40.0,
epsilon: float = 0.1,
lmda: float = 0.5,
center: float = 1.0,
name: str = MultiSimilarityLoss,
**kwargs
)
y_true must be a 1-D integer Tensor of shape (batch_size,). It's values represent the classes associated with the examples as integer values.
y_pred must be 2-D float Tensor of L2 normalized embedding vectors. you can use the layer tensorflow_similarity.layers.L2Embedding() as the last layer of your model to ensure your model output is properly normalized.
distance | Which distance function to use to compute the pairwise distances between embeddings. Defaults to 'cosine'. |
alpha | The exponential weight for the positive pairs. Increasing alpha makes the logsumexp softmax closer to the max positive pair distance, while decreasing it makes it closer to max(P) + log(batch_size). |
beta | The exponential weight for the negative pairs. Increasing beta makes the logsumexp softmax closer to the max negative pair distance, while decreasing it makes the softmax closer to max(N) + log(batch_size). |
epsilon | Used to remove easy positive and negative pairs. We only keep positives that we greater than the (smallest negative pair - epsilon) and we only keep negatives that are less than the (largest positive pair + epsilon). |
lmda | Used to weight the distance. 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. |
center | This represents the expected distance value and will be used to center the values in the pairwise distance matrix. This is used when weighting the positive and negative examples, with the hardest examples receiving an up weight and the easiest examples receiving a down weight. This should 1 for cosine distances which we expect to be between [0,2]. The value will depend on the data for L2 and L1 distances. |
name | Loss name. Defaults to MultiSimilarityLoss. |
<b>python @classmethod</b>
from_config(
config
)
Instantiates a Loss from its config (output of get_config()).
Args | |
---|---|
config | Output of get_config(). |
Returns | |
---|---|
A Loss instance. |
get_config() -> Dict[str, Any]
Contains the loss configuration.
Returns | |
---|---|
A Python dict containing the configuration of the loss. |
__call__(
y_true, y_pred, sample_weight=None
)
Invokes the Loss instance.
Args | |
---|---|
y_true | Ground truth values. shape = [batch_size, d0, .. dN], except sparse loss functions such as sparse categorical crossentropy where shape = [batch_size, d0, .. dN-1] |
y_pred | The predicted values. shape = [batch_size, d0, .. dN] |
sample_weight | Optional sample_weight acts as a coefficient for the loss. If a scalar is provided, then the loss is simply scaled by the given value. If sample_weight is a tensor of size [batch_size], then the total loss for each sample of the batch is rescaled by the corresponding element in the sample_weight vector. If the shape of sample_weight is [batch_size, d0, .. dN-1] (or can be broadcasted to this shape), then each loss element of y_pred is scaled by the corresponding value of sample_weight. (Note ondN-1: all loss functions reduce by 1 dimension, usually axis=-1.) |
Returns | |
---|---|
Weighted loss float Tensor. If reduction is NONE, this has shape [batch_size, d0, .. dN-1]; otherwise, it is scalar. (Note dN-1 because all loss functions reduce by 1 dimension, usually axis=-1.) |
Raises | |
---|---|
ValueError | If the shape of sample_weight is invalid. |