简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM