Home > Enterprise >  tensorflow: logging custom loss function?
tensorflow: logging custom loss function?

Time:01-28

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?

CodePudding user response:

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(*).

  •  Tags:  
  • Related