iBM
iBM

Reputation: 185

Custom loss function in Keras runs ony once

I have defined a custom loss function in Keras as follows:

def loss_function_test(yTrue, yPred):
    global i
    i += 1
    print("\n")
    print("Loss Function:", i, " ----- ", yTrue, yPred)
    print("\n")
    res = k.sum(yTrue - yPred)
    return res

and set it as follows:

model.compile(loss=loss_function_test,
              optimizer='sgd',
              metrics=['accuracy'])

The problem is checking the global i and the print function in the loss function code, I realize that the function is only called once in training when I call model.fit. Even when I debug it, it is only called once. It seems that maybe the loss function is only called while compile. Can anyone explain why?

Upvotes: 1

Views: 299

Answers (1)

sietschie
sietschie

Reputation: 7553

The function to construct the loss function op runs only once.

If you want to see how often the op is evaluated by tensorflow you need to add a tf.Print operation into the tensorflow graph.

Upvotes: 1

Related Questions