"ValueError: No gradients provided for any variable" when training a bijector

1,052 views
Skip to first unread message

Cyber punk

unread,
May 5, 2022, 11:05:12 PM5/5/22
to TensorFlow Probability

I'm training a bijector using tensorflow-probability.

I'm following a tutorial. The same codes from the tutorial can be found at (the relevant part starts from the line "Training the bijector"): https://rpubs.com/bacti/tensorflow-probabilistic

My code is slightly different to the above. But even if I copy the same code from the above, it still causes the same error.

My codes is here (runnable). It's basically the same to the relevant part in the tutorial's codes: https://colab.research.google.com/drive/1tlA5l2ZGRIiR19HM-LHGKs-G2Rjxk6zF?usp=sharing#scrollTo=BW9Kmg3z0dzC

The training loop in my codes caused the error:

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-13-3bf0f105e1c4> in <module>() 18 grads = tape.gradient(loss, trainable_dist.trainable_variables) 19 print('grads', grads) ---> 20 opt.apply_gradients(zip(grads, trainable_dist.trainable_variables)) 21 22 # print(loss) 1 frames /usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/utils.py in filter_empty_gradients(grads_and_vars) 71 if not filtered: 72 variable = ([v.name for _, v in grads_and_vars],) ---> 73 raise ValueError(f"No gradients provided for any variable: {variable}. " 74 f"Provided `grads_and_vars` is {grads_and_vars}.") 75 if vars_with_empty_grads: 

 ValueError: No gradients provided for any variable: (['a:0', 'b:0'],). Provided `grads_and_vars` is ((None, <tf.Variable 'a:0' shape=() dtype=float32, numpy=0.25>), (None, <tf.Variable 'b:0' shape=() dtype=float32, numpy=-0.1>)).


The loss was computed, but the grads can not be computed in the line grads = tape.gradient(loss, trainable_dist.trainable_variables). They are (None, None). Why? What is the cause of it? I can't find any problem in my codes.

Srinivas Vasudevan

unread,
May 6, 2022, 12:06:00 AM5/6/22
to Cyber punk, TensorFlow Probability
I've changed the `tf.convert_to_tensor` to `tensor_util.convert_nonref_to_tensor`  in the constructor, and that seems to get everything to train.

What was happening before is that the parameters to your bijector were being converted to Tensors. That means that if `a` and `b` were variables, at the time you construct the bijector, they will be stored as Tensors, and you will lose any reference to the original variable.
Our internal `tensor_util.convert_nonref_to_tensor` is a wrapper around `tf.convert_to_tensor` that does a few things, one of which is identifies if the passed in argument is a `Variable`, and if so doesn't modify it.

--
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/5f495e2b-d272-48d9-b792-59591aab5a7dn%40tensorflow.org.

Cyber punk

unread,
May 6, 2022, 3:20:34 AM5/6/22
to TensorFlow Probability, Srinivas Vasudevan, TensorFlow Probability, Cyber punk
Thanks! It works now. What you said makes sense to me. However I couldn't find much info about `tensor_util.convert_nonref_to_tensor` in Google except the source code on github.
Thanks a lot :)

Reply all
Reply to author
Forward
0 new messages