Welcome to our Support Center

Custom

Description

A custom loss function allows you to define your own loss logic, making it possible to go beyond the standard loss functions provided by libraries. Instead of being limited to the default losses (e.g., Binary Crossentropy, MSE), you can create a custom loss by combining existing operations or designing a new formula that better suits your task. Type : polymorphic.

 

 

However, custom loss functions can also accept additional arguments if needed, for example, class weights, auxiliary data, or even internal model states. This makes it possible to build more sophisticated loss strategies, including :

  • Modifying an existing loss (e.g., adding regularization or a masking condition)
  • Designing multi-input losses (e.g., for Siamese or triplet networks)
  • Combining multiple objectives (e.g., reconstruction + classification)
  • Integrating domain-specific rules directly into the training signal

 

The custom loss must be provided as a valid ONNX model. This model must follow two rules :  

  • It must have an input named “prediction”, which will receive the output of the network during training. 
  • It must have a single scalar output, representing the computed loss value. 

 

Input parameters

 

 Model in : model architecture.

 

Output parameters

 

Loss : cluster, this cluster defines the loss function used for model training.

enum : enum, an enumeration indicating the loss type (e.g., MSE, CrossEntropy, etc.). If enum is set to CustomLoss, the custom class on the right will be used as the loss function. Otherwise, the selected loss will be applied with its default configuration.
 Class : object, a custom loss class instance.

Required data

 y_pred : array, the model’s predictions.
 y_true : array, the ground truth data.

Use cases

Custom losses are especially valuable when your performance metric is complex, composite, or cannot be captured by standard loss functions. By enabling fine control over the optimization target, they ensure the training process is aligned with the true goal of your application.

Example

All these exemples are snippets PNG, you can drop these Snippet onto the block diagram and get the depicted code added to your VI (Do not forget to install Deep Learning library to run it).
Table of Contents