3

In Keras I have a target vector of y_true that fits onto a network that has one output neuron. y_true = [0, 1, 0, 1, 1....] and I have some payoffs [1,1,1,-5,1...]

I'm trying to put the payoffs as extra parameters into a custom loss function of keras. Keras only allows two parameters to be passed into it (y_true and y_pred), but I would also like to pass the payoffs that are assigned to each sample. To that end I have added a second column to y_true that contains those values.

I then try to separate the actual y_true (first column) and the payoffs (second column) again in the loss function by doing the following:

def custom_loss(y_true, y_pred)    
    # y_true has the payoffs in the second row
    payoffs = y_true[:, 1]
    payoffs = K.expand_dims(payoffs, 1)
    y_true = y_true[:, 0]
    y_true = K.expand_dims(y_true, 1))

    loss = K.binary_crossentropy(y_true, y_pred)
    return loss

This is a simplified version of what I want to do (in the real version I will integrate the payoffs into the loss function). But for the example above I would expect the loss function to be identical to just calling binary_cross entropy directly with having y_true only containing y_true (without any payoffs).

However, the result is not as expected as the accuracy values are around half with the custom loss function above.

What could be the cause for this error? Am I not slicing y_true correctly?

The problem is related to what is described in this post (curiale's comment on 12 Dec 2017 suggests to use slice_stack, but the problem is the same).

1 Answer 1

2

I think the problem was that I needed to customize the metric function as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.