--
You received this message because you are subscribed to the Google Groups "TensorFlow Probability" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tfprobabilit...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/tfprobability/b61490a5-a61c-47f6-ac49-fe7928e6d648%40tensorflow.org.
def unormalized_log_prob(**x): x['x'] = data return joint_distribution_named.log_prob(x)
def clip_gradients_dict(fn, clip_value):
def wrapper(**args):
@tf.custom_gradient
def grad_wrapper(**flat_args):
with tf.GradientTape() as tape:
tape.watch(flat_args)
ret = fn(**tf.nest.pack_sequence_as(args, flat_args))
def grad_fn(*dy):
flat_grads = tape.gradient(ret, flat_args, output_gradients=dy)
flat_grads = tf.nest.map_structure(lambda g: tf.where(
tf.math.is_finite(g), g, tf.zeros_like(g)), flat_grads)
return tf.clip_by_global_norm(flat_grads, clip_value)[0]
return ret, grad_fn
return grad_wrapper(**tf.nest.flatten(args))
return wrapper
(sorry, I sneaked in handling of NaN gradients as well in there, you can delete that line if you don't need that)
On Fri, Sep 13, 2019 at 11:07 AM Pavel Sountsov <si...@google.com> wrote:
You can use custom_gradient to implement arbitrary gradient-post processing:def clip_gradients(fn, clip_value):def wrapper(*args):@tf.custom_gradientdef grad_wrapper(*flat_args):with tf.GradientTape() as tape:tape.watch(flat_args)ret = fn(*tf.nest.pack_sequence_as(args, flat_args))def grad_fn(*dy):flat_grads = tape.gradient(ret, flat_args, output_gradients=dy)flat_grads = tf.nest.map_structure(lambda g: tf.where(tf.math.is_finite(g), g, tf.zeros_like(g)), flat_grads)return tf.clip_by_global_norm(flat_grads, clip_value)[0]return ret, grad_fnreturn grad_wrapper(*tf.nest.flatten(args))return wrapperclipped_target_log_prob_fn = clip_gradients(target_log_prob_fn, clip_value=3.)
On Fri, Sep 13, 2019 at 10:45 AM Josh Chang <josh.col...@gmail.com> wrote:
To clarify, this would be for TF 2--
On Friday, September 13, 2019 at 1:45:23 PM UTC-4, Josh Chang wrote:Is there a easy way to clip gradients within HMC?
You received this message because you are subscribed to the Google Groups "TensorFlow Probability" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tfprob...@tensorflow.org.
To unsubscribe from this group and stop receiving emails from it, send an email to tfprobabilit...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/tfprobability/62a2b4ce-fa47-4325-aed5-2b6ba49c473d%40tensorflow.org.