Why does a penalty not change the predictions of a Keras model?

Solution 1:

Your error must be somewhere else, because the loss functions you posted do generate different results:

import tensorflow as tf
tf.random.set_seed(11)

def binary_crossentropy1(y_true, y_pred): 

  bin_cross = tf.keras.losses.BinaryCrossentropy(from_logits=True)
  bce = tf.keras.backend.mean(bin_cross(y_true, y_pred))
  return bce

def binary_crossentropy2(y_true, y_pred): 

  bin_cross = tf.keras.losses.BinaryCrossentropy(from_logits=True)
  bce = tf.keras.backend.mean(bin_cross(y_true, y_pred)) + tf.random.normal([], mean=0.0, stddev=10.0)
  return bce

y_true = tf.constant([0, 1, 0, 0])
y_pred = tf.constant([-18.6, 0.51, 2.94, -12.8])
print(binary_crossentropy1(y_true, y_pred))
print(binary_crossentropy2(y_true, y_pred))
tf.Tensor(0.865458, shape=(), dtype=float32)
tf.Tensor(-14.364014, shape=(), dtype=float32)