title: Keras: weighted binary crossentropy
body: <p>I tried to implement a weighted binary crossentropy with Keras, but I am not sure if the code is correct. The training output seems to be a bit confusing. After a few epochs I just get an accuracy of ~0.15. I think thats much too less (even for a random guess).</p>
  
  <p>There are in general about 11% ones in the output and 89% zeros, therefore the weights are w_zero=0.89 and w_one=0.11.</p>
  
  <p>My code:</p>
  
  
  
  <pre class="lang-python prettyprint-override"><code>def create_weighted_binary_crossentropy(zero_weight, one_weight):
  
      def weighted_binary_crossentropy(y_true, y_pred):
  
          # Original binary crossentropy (see losses.py):
          # K.mean(K.binary_crossentropy(y_true, y_pred), axis=-1)
  
          # Calculate the binary crossentropy
          b_ce = K.binary_crossentropy(y_true, y_pred)
  
          # Apply the weights
          weight_vector = y_true * one_weight + (1. - y_true) * zero_weight
          weighted_b_ce = weight_vector * b_ce
  
          # Return the mean error
          return K.mean(weighted_b_ce)
  
      return weighted_binary_crossentropy
  </code></pre>
  
  <p>Maybe someone sees whats wrong?</p>
  
  <p>Thank you</p>