tensorflow / tensorflow/probability

NaN gradients of RelaxedBernoulli samples for p near 0 and 1 in recent TFP versions

Open
#1,393 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
4.4k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

I'm encountering some numerical issues when evaluating the gradients of a RelaxedBerrnoulli, with parameter p close to either 0 or 1 (close but not super close). Here is a minimal example:

import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions
tfb = tfp.bijectors

l = 0.99*tf.ones(shape=[],dtype=tf.float32)

with tf.GradientTape() as tape:
  tape.watch(l)
  p = tfd.RelaxedBernoulli(temperature=0.01, probs=l).sample()
dp_dl = tape.gradient(p,l)
print(p, dp_dl)

which returns NaN for the gradient in TFP v0.13 and TF 2.5. Note that I also see the same thing if I directly try to get the gradient of a Transformed Logistic disribution, so if there is a problem it's probably not at the level of RelaxedBernoulli.

What is interesting is that this used to work perfectly fine in previous TFP version (at least in v0.9). For comparison I made these two notebooks with different TFP versions:

Looking at the history of both Logistic and RelaxedBernoulli I really don't see an obvious reason for this change in behavior, so would appreciate any hints/thoughts on what might cause these numerical issues :-)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the minimal TensorFlow Probability example from the issue with the stated TensorFlow and TFP versions, then compare it with the linked working notebook. Inspect the RelaxedBernoulli and transformed Logistic gradient paths to identify the numerical regression. Done means gradients for probabilities near 0 and 1 are finite and a regression case covers the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, tensorflow
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.