title: how to prevent negative predictions in keras custom loss function
body: <p>I'm using a custom loss function:</p>
  
  <pre><code>def ratio_loss(y, y0):
     return (K.mean(K.abs(y - y0) / y))
  </code></pre>
  
  <p>and get negative predicted values - which in my case doesn't makes scene (I use CNN and regression as last layer to get a length of an object).
  I used division in order to penalize more where the true value is relative small to the predicted).</p>
  
  <p>how can i prevent the negative predictions ?</p>
  
  <p>this is the mode (for now..):</p>
  
  <pre><code>def create_model():
      model = Sequential()
      model.add(Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same', input_shape=(128, 128, 1)))
      model.add(Dropout(0.5))
  
      model.add(Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same'))
      model.add(Dropout(0.25))
      model.add(BatchNormalization())
      model.add(MaxPooling2D(pool_size=(2, 2)))
  
      model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'))
      model.add(Dropout(0.25))
      model.add(BatchNormalization())
      model.add(MaxPooling2D(pool_size=(2, 2)))
      #
      #
      model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'))
      model.add(Dropout(0.25))
      model.add(BatchNormalization())
      model.add(MaxPooling2D(pool_size=(2, 2)))
  
      model.add(Flatten())
      model.add(Dense(512, activation='relu'))
  
      model.add(Dropout(0.15))
      model.add(Dense(1))
      #model.compile(loss=keras.losses.mean_squared_error, optimizer=keras.optimizers.Adadelta(), metrics=[sacc])
      model.compile(loss=ratio_loss, optimizer=keras.optimizers.Adadelta(), metrics=[sacc])
      return model
  </code></pre>
  
  <p>Thanks,
  Amir</p>