crested.tl.losses.CosineMSELoss#

class crested.tl.losses.CosineMSELoss(max_weight=1.0, name='CosineMSELoss', reduction='sum_over_batch_size')#

Custom loss function that combines cosine similarity and mean squared error (MSE).

This loss function computes both cosine similarity and MSE between the true and predicted values. A dynamic weight based on the MSE is used to balance these two components.

Parameters:
  • max_weight (float (default: 1.0)) – The maximum weight applied to the cosine similarity loss component (default is 1.0). Lower values will emphasize the MSE component, while higher values will emphasize the cosine similarity component.

  • name (str | None (default: 'CosineMSELoss')) – Name of the loss function.

  • reduction (str (default: 'sum_over_batch_size')) – Type of reduction to apply to loss.

Notes

  • The cosine similarity is computed between L2-normalized true and predicted values.

  • The dynamic weight for the cosine similarity component is constrained between 1.0 and max_weight.

Examples

>>> loss = CosineMSELoss(max_weight=2.0)
>>> y_true = np.array([1.0, 0.0, -1.0])
>>> y_pred = np.array([1.2, -0.1, -0.9])
>>> loss(y_true, y_pred)

Attributes table#

Methods table#

call(y_true, y_pred)

Compute the loss value.

from_config(config)

Create a loss function from the configuration.

get_config()

Return the configuration of the loss function.

Attributes#

CosineMSELoss.dtype#

Methods#

CosineMSELoss.call(y_true, y_pred)#

Compute the loss value.

classmethod CosineMSELoss.from_config(config)#

Create a loss function from the configuration.

CosineMSELoss.get_config()#

Return the configuration of the loss function.