Categories
Misc

Can you define a custom loss function that is different for each observation?

Probably a stupid question, but I am a complete noob with tensorflow (and haven’t used python in years) and I’m trying to see if I can use it to solve a very specific problem that I have. Basically, I am running a multivariate regression and the loss function is supposed to vary depending on the observation.

For example, assume that for each row i of the data frame the model outputs a vector y_hat with 5 values, and the true vector for that observation is y. The “default” loss function would be the L1 norm of the error:

sum([abs(y[k] - y_hat[k]) for k in range(0,5)]) 

But I would like to add a penalty depending on i, like this:

sum([abs(y[k] - y_hat[k]) for k in range(0,5)]) + abs(y[i%5] - y_hat[i%5]) 

Is this even possible? I was thinking that maybe I could somehow modify the output layer such that it outputs a vector with 6 values, making sure that the first value is always equal to i and the remaining 5 values are equal to y_hat, but I don’t know how to do that either.

Sorry for the rambling, any help would be greatly appreciated!

submitted by /u/fr-pscz
[visit reddit] [comments]

Leave a Reply

Your email address will not be published. Required fields are marked *