Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot compute gradient of Weibull parameters on second iteration #1772

Open
Kornel opened this issue Nov 30, 2023 · 1 comment
Open

Cannot compute gradient of Weibull parameters on second iteration #1772

Kornel opened this issue Nov 30, 2023 · 1 comment

Comments

@Kornel
Copy link

Kornel commented Nov 30, 2023

I am trying to learn the parameters of a Weibull distribution, but after the first iteration the gradients turn None:

import tensorflow as tf
from tensorflow_probability import distributions as tfd

W = tfd.Weibull(concentration=1.5, scale=2.5)
data = W.sample(10_000)

concentration = tf.Variable(initial_value=1.0, dtype=tf.float32, name="W_c")
scale = tf.Variable(initial_value=3.0, dtype=tf.float32, name="W_s")

W_hat = tfd.Weibull(concentration=concentration, scale=scale)

for epoch in range(3):
    with tf.GradientTape() as tape:
        tape.watch(W_hat.trainable_variables)
        nll = -W_hat.log_prob(data)
    grads = tape.gradient(nll, W_hat.trainable_variables)
    
    print(grads)  # , W_hat.trainable_variables)

Output:

(<tf.Tensor: shape=(), dtype=float32, numpy=585.2751>, <tf.Tensor: shape=(), dtype=float32, numpy=797.33496>)
(None, None)
(None, None)

This does not happen to e.g. the Normal distribution. Please note no optimiser is applied here it's just the computation of the gradient of the nll w.r.t. to the parameters done twice. The versions I'm using:

python 3.8.13 (default, Sep  7 2022, 19:12:31)  [Clang 12.0.5 (clang-1205.0.22.11)]
tensorflow 2.13.0
tensorflow_probability 0.21.0

I reproduced this on both a M1 Mac and an Intel Xeon x86_64

@Kornel
Copy link
Author

Kornel commented Nov 30, 2023

p.s. I just discovered that doing data = W.sample(10_000).numpy() solves the issue. Why does this happen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant