How to check NaN in gradients in Tensorflow when updating?

后端 未结 2 1956
借酒劲吻你
借酒劲吻你 2021-01-03 04:38

All,

When you train a large model with large amount samples, some samples may be cause NaN gradient when parameter updating.

And I want to find these samples

相关标签:
2条回答
  • 2021-01-03 04:55

    You could use tf.is_nan in combination with tf.cond to only execute the rest of your code if the loss is not NaN.

    0 讨论(0)
  • 2021-01-03 05:13

    You can check whether your gradients have NaN by tf.check_numerics:

    grad_check = tf.check_numerics(clipped_gradients)
    with tf.control_dependencies([grad_check]):
      self.optimizer = opt.apply_gradients(zip(clipped_gradients, params))
    

    The grad_check would throw InvalidArgument if clipped_gradients is NaN or infinity.

    The tf.control_dependencies makes sure that the grad_check is evaluated before applying the gradients.

    Also see tf.add_check_numerics_ops().

    0 讨论(0)
提交回复
热议问题