How do you create a custom loss in tensorflow which uses a external tensor?
I have a problem where I want to minimize the monetary cost associated with the prediction error (Mean Error, ME) from the feature I want to predict. The monetary cost is calculated by multiplying ME with the cost tensor.
Therefore I want to use a custom conditional loss function that uses one type of loss (MAE) if the loss is above a threshold and returns another loss if it is below the threshold. The second loss is just the mean error multiplied with a external tensor not used in the model. The external tensor is the cost of the mean error, which should be minimized.
Below is how I'm thinking the code should look like:
import keras.backend as K
def conditional_loss_function(cost_tensor):
def loss(y_true, y_pred):
first_loss = K.abs(y_true-y_true)
threshold = 10
if first_loss= threshold:
return (y_true - y_pred)*(cost_tensor)
else:
return first_loss
return loss
I know that the loss will probably flip around the threshold and I guess this could be fixed with at moving average threshold for past losses but I'm not quite sure how that would look like.
Topic cost-function tensorflow loss-function
Category Data Science