title: Keras custom loss function huber
body: <p>I wrote huber loss using Keras backend functions and this works well:</p>
  <pre><code>def huber_loss(y_true, y_pred, clip_delta=1.0):
      error = y_true - y_pred
      cond  = K.abs(error) &lt; clip_delta
           
      squared_loss = 0.5 * K.square(error)
      linear_loss  = clip_delta * (K.abs(error) - 0.5 * clip_delta)
  
      return tf_where(cond, squared_loss, linear_loss)
  </code></pre>
  <p>But I need a more complex loss function:</p>
  <ol>
  <li>if <code>error &lt;= A</code>, use <em>squared_loss</em></li>
  <li>if <code>A &lt;= error &lt; B</code>, use <em>linear_loss</em></li>
  <li>if <code>error &gt;= B</code>, used <em>sqrt_loss</em></li>
  </ol>
  <p>I wrote smth like this:</p>
  <pre><code>def best_loss(y_true, y_pred, A, B):
      error = K.abs(y_true - y_pred)
      cond  = error &lt;= A
      cond2 = tf_logical_and(A &lt; error, error &lt;= B)
  
      squared_loss = 0.5 * K.square(error)
      linear_loss  = A * (error - 0.5 * A)
      sqrt_loss = A * np.sqrt(B) * K.sqrt(error) - 0.5 * A**2
  
      return tf_where(cond, squared_loss, tf_where(cond2, linear_loss, sqrt_loss))
  </code></pre>
  <p>But it does not work, the model with this loss function does not converge, what is the bug?</p>