I'm looking for a way to create a loss function that looks like this: The function should then maximize for the reward. Is this possible to achieve in Keras? Any suggestions how this can be achieved are highly appreciated.
def special_loss_function(y_true, y_pred, reward_if_correct, punishment_if_false):
loss = if binary classification is correct apply reward for that training item in accordance with the weight
if binary classification is wrong, apply punishment for that training item in accordance with the weight
)
return K.mean(loss, axis=-1)
The approach I've been looking at for my example is to pass in the weights along with y_true and then cut the tensor into two, separating out the weights and the y_true as seen below. Would an approach like this be possible at all or would this interfere with the normalization process etc?
def decompose_y_true(y_true_and_weights):
y_true = y_true_and_weights[:,1]
weights= y_true_and_weights[:,1:]
return y_true, weights
def custom_loss(y_true_and_weights, y_pred):
y_true, y_weights = decompose_y_true(y_true_and_weights)
loss = # some loss operation
return K.mean(loss, axis=-1)
Much more elegant would be if I could pass in my weights over the sample_weights parameter in the fit function, but it seems there are some limits what shape those weights can have, and also there's no way to retrieve them within the loss function as far as I can tell. Or is there any way to pass this into the loss function somehow so I can operate on them from there?