简体   繁体   中英

Keras custom loss function. How to know which output samples are currently called in the loss funciton

I an building a custom loss function in keras where I want to manipulate the y_true and y_pred before calling mean_absolute_error() . This manipulation involves knowing which sample the current y_true is, therefore, I can easily add another column to the output which will have a sequential index. So I have

  • outputs[:,0] which is the acual output variable for all the samples.

  • outputs[:,1] a sequential index which I do not want to predict but just use in my custom loss function

     def loss(y_true, y_pred): for i in range(len(y_true)): index = y_true[i, 1] # some manipulations involving the index return mean_absolute_error(new_true, new_pred)

How can I achieve the above in a way that the model ignores the index column output and does not try to predict it as well.

There might be a better solution to my problem. My ultimate goal is to know which sample output(s) are currently in the loss function (by an index).

You can try this:

def customLoss(y_true, y_pred):
    
    # you can do your preprocessing here

    def loss(y_true, y_pred):
        for i in range(len(y_true)):
             index = y_true[i, 1]
             # some manipulations involving the index
        return mean_absolute_error(new_true, new_pred)


    return loss(y_true, y_pred)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM