title: slicing inputs for loss function in keras with tensorflow
body: <p>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...]</p>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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).</p>
  
  <p>However, the result is not as expected as the accuracy values are around half with the custom loss function above.</p>
  
  <p>What could be the cause for this error? Am I not slicing y_true correctly?</p>
  
  <p>The problem is related to what is described in <a href="https://github.com/keras-team/keras/issues/2115" rel="nofollow noreferrer">this post</a> (curiale's comment on 12 Dec 2017 suggests to use slice_stack, but the problem is the same).</p>