Search code examples
tensorflowkerasloss-function

tensorflow custom loss function with additional parameters


I understand how custom loss functions work in tensorflow. Suppose in the following code , a and b are numbers.

def customLoss( a,b):
    
    def loss(y_true,y_pred):    
        loss=tf.math.reduce_mean(a*y_pred + b*y_pred)
        return loss
    return loss    

But what if a and b are arrays which have the same shape as y_pred. let's say

y_pred= np.array([0,1,0,1])
a= np.arange(4)
b= np.ones(4)

I would like to the value of loss function is equal to 6:

np.mean(a*y_pred + b*y_pred) #element-wise.

I feel like my loss function is wrong now. It should be two additional inputs or weights of each sample. Can anyone help please? Thank you.


Solution

  • Assuming that a and b are fixed numbers across all loss computations, you can do something similar to your original loss function:

    import numpy as np
    import tensorflow as tf
    
    y_pred = np.array([0, 1, 0, 1])
    y_true = np.array([0, 1, 0, 1])
    a = np.arange(4)
    b = np.ones(4)
    
    
    def get_custom_loss(a, b):
      a = tf.constant(a, dtype=tf.float32)
      b = tf.constant(b, dtype=tf.float32)
    
      def loss_fn(y_true, y_pred):
        y_true = tf.cast(y_true, dtype=tf.float32)
        y_pred = tf.cast(y_pred, dtype=tf.float32)
    
        loss = tf.math.reduce_sum(
            [tf.math.multiply(a, y_pred), tf.math.multiply(b, y_pred)])
        return loss
    
      return loss_fn
    
    loss_fn = get_custom_loss(a, b)
    print(loss_fn(y_pred, y_true))
    

    tf.math.multiply(foo, bar) will perform element-wise multiplication of two tensors (see docs). I assume you also want to sum the result of the two products, rather than take the mean (which would be 1.5).

    Just a note: you aren't currently using y_true in your loss.