Ian Newson
Ian Newson

Reputation: 7949

tensorflow: logging custom loss function?

I've written a custom loss function as follows:

def distance_loss(y_actual, y_pred):
    return tf.math.sqrt(
            tf.math.add(
                tf.math.pow(
                    tf.math.subtract(y_actual[0], y_pred[0]),
                    tf.constant(2.0)
                ),
                tf.math.pow(
                    tf.math.subtract(y_actual[1], y_pred[1]),
                    tf.constant(2.0)
                )
            )
        )

However this is my first time doing this so I don't know how well (or if) this function is working.

Is there any way I can log the inputs and output of this function, as well as the sample it's being called on, so I can manually verify that it's working as intended?

Upvotes: 0

Views: 221

Answers (1)

AloneTogether
AloneTogether

Reputation: 26708

Use tf.print:

def distance_loss(y_actual, y_pred):
    x = tf.math.pow(
                    tf.math.subtract(y_actual[0], y_pred[0]),
                    tf.constant(2.0)
                )
    y =  tf.math.pow(
                    tf.math.subtract(y_actual[1], y_pred[1]),
                    tf.constant(2.0)
                )
    loss = tf.math.sqrt(tf.math.add(x, y))
    tf.print('First operation ->', x)
    tf.print('Second operation ->', y)
    tf.print('Loss ->', loss)
    return loss

and you will see the values when you call model.fit(*).

Upvotes: 1

Related Questions