Created
June 16, 2020 07:20
-
-
Save MJafarMashhadi/2bafe5c596fecdd3332174fd3c0bb91f to your computer and use it in GitHub Desktop.
A tensorflow layer that prints a warning mesage if there is a `nan` or `inf` in a layer's output.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@tf.function | |
def has_nan(X): | |
return tf.math.reduce_any(tf.math.is_nan(X)) | |
@tf.function | |
def has_inf(X): | |
return tf.math.reduce_any(tf.math.is_inf(X)) | |
def check_nan(name): | |
@tf.function | |
def _inner(X): | |
if has_nan(X) or has_inf(X): | |
tf.print('nan/inf in', name,'->',X,'\n') | |
return X | |
return _inner | |
... | |
X = tf.keras.layers.Lambda(check_nan('layer name'))(X) | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment