Sergej Andrejev
Sergej Andrejev

Reputation: 9423

Logging scalars from loss function

I'm working on a ML model implemented in Keras. For this model I wrote a custom loss function which where the loss is a sum of performances of 3 other variables (a_cost, b_cost, c_cost). The loss function works but I would like to tune it a bit and for that I would like to see how these 3 other variables behave. How do I log these scalars so that they could be displayed in TensorBoard?

def custom_cost(y_true, y_pred):
    # compute a_cost, b_cost, c_cost

    cost = a_cost + b_cost + c_cost
    return cost

# ..build model...
model.compile(loss=custom_cost, optimizer=optimizers.Adam())
tensorboard = callbacks.TensorBoard(log_dir="./logs", write_graph=True)
tensorboard.set_model(model)
model.fit_generator(generator=custom_generator, epochs=100, steps_per_epoch=180,     callbacks=[tensorboard], verbose=True)

Upvotes: 1

Views: 162

Answers (1)

Marcin Możejko
Marcin Możejko

Reputation: 40526

As a_cost, b_cost and c_cost are computed you can define three separate functions to compute them separately. Let:

def a_cost(y_true, y_pred):
    # compute a_cost
    ...
    return a_cost

def b_cost(y_true, y_pred):
    # compute b_cost
    ...
    return b_cost

def c_cost(y_true, y_pred):
    # compute c_cost
    ...
    return c_cost

Now this as simple as adding these three functions as metrics:

model.compile(..., metrics=[a_cost, b_cost, c_cost])

Upvotes: 1

Related Questions