@tensorleap_custom_loss

code_loader.inner_leap_binder.leapbinder_decorators.tensorleap_preprocess.tensorleap_custom_loss

The purpose of the tensorleap_custom_loss decoder is to register the Custom Loss Function(s) to be used within the platform. This function adds the custom loss function to the selection list within the CustomLoss node.

@tensorleap_custom_loss(name='weighted_ce')
def loss(tensor_1: np.ndarray, tensor_2: np.ndarray, ...) -> np.ndarray
    pass
Args

name

(str) with the given name of the custom loss.

Loss Function inputs:

np.ndarray tensors with a batch dimension

Loss Function outputs:

np.ndarray batched loss values

Examples

Basic Usage

from code_loader.contract.datasetclasses import PreprocessResponse
from code_loader.inner_leap_binder.leapbinder_decorators import tensorleap_custom_loss
import numpy as np
...

@tensorleap_custom_loss(name='weighted_ce')
def weighted_categorical_crossentropy(y_true :np.ndarray, y_pred: np.ndarray) -> np.ndarray:
    # Normalize predictions so each sample's probabilities sum to 1
    y_pred = y_pred / np.sum(y_pred, axis=-1, keepdims=True)
    
    # Clip predictions to avoid log(0) and ensure numerical stability
    epsilon = 1e-7  # Similar to K.epsilon()
    y_pred = np.clip(y_pred, epsilon, 1 - epsilon)
    
    # Define class weights
    weights = np.array([0.5, 2.1, 3, 4, 4, 4, 4, 4])
    
    # Compute weighted log loss
    loss = y_true * np.log(y_pred) * weights
    loss = -np.sum(loss, axis=-1)
    return loss

Last updated

Was this helpful?