I used python Closures to enable me to pass additional argument (a scaler value) to a Keras custom loss function. This is script:
@staticmethod
def sdec_loss(add_loss2):
def loss_function(y_true, y_pred):
print("add_loss2= ", add_loss2)
return losses.kullback_leibler_divergence(y_true, y_pred) - add_loss2
return loss_function
I defined this python closure in a class called SDEC. Then I called it from another class as the following:
self.DEC.loss = SDEC.sdec_loss( add_loss2 )
The problem is that the value of the scaler (i.e. add_loss2) is not received within the inner function (i.e. the loss_function). whatever value the (add_loss2) variable holds, the print statement output is 0.