Custom loss function with additional parameter in Keras 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?
You can write a function that returns another function, as is done here on GitHub

def penalized_loss(noise):
    def loss(y_true, y_pred):
        return K.mean(K.square(y_pred - y_true) - K.square(y_true - noise), axis=-1)
    return loss

input1 = Input(batch_shape=(batch_size, timesteps, features))
lstm =  LSTM(features, stateful=True, return_sequences=True)(input1)
output1 = TimeDistributed(Dense(features, activation='sigmoid'))(lstm)
output2 = TimeDistributed(Dense(features, activation='sigmoid'))(lstm)
model = Model(input=[input1], output=[output1, output2])
model.compile(loss=[penalized_loss(noise=output2), 
penalized_loss(noise=output1)], optimizer='rmsprop')


In the wrapper function you can pass scalars or keras tensors like additional inputs. In your case you would add the weights in the top wrapper function and reference them in your inward function.