`tfp.math.log1mexp` has a NaN gradient for small inputs (reaches `OrderedLogistic.log_prob`)

Aperta Adatta ai principianti
#2,041 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

@aviralgarg05 ci sta già lavorando.

Dal 19/11/2026.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
86/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python

Direzione di ricerca

Inizia da tensorflow_probability/python/math/generic.py in log1mexp, dove un tf.where sceglie tra log(-expm1(-x)) e log1p(-exp(-x)). Riproduci il problema con lo snippet nell'issue, quindi aggiungi un test del gradiente in generic_test.py. È completato quando il gradiente è finito per piccoli x e il gradiente del log_prob di OrderedLogistic con loc=18 è circa 1.13e-07, mantenendo invariati i valori in avanti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

OrderedLogistic(...).log_prob gets a NaN gradient in float32 at loc = 18 (17 is still fine), and the NaN comes from tfp.math.log1mexp:

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

x = tf.constant([1e-9, 1e-7, 1e-3])
with tf.GradientTape() as tape:
  tape.watch(x)
  y = tfp.math.log1mexp(x)
print(tape.gradient(y, x).numpy())  # [nan 1.e+07 999.50006]  exact 1/expm1(x): [1e9, 1e7, 999.5]

loc = tf.Variable(18.)
with tf.GradientTape() as tape:
  lp = tfd.OrderedLogistic(cutpoints=[0., 1., 2.], loc=loc).log_prob(3)
print(tape.gradient(lp, loc).numpy())  # nan  exact sigmoid(-16) = 1.13e-07

log1mexp picks log(-expm1(-x)) or log1p(-exp(-x)) with one tf.where, so the gradient of the branch it didn't pick is still computed. For small x, exp(-x) rounds to 1, that branch's derivative is inf, and the tf.where backward turns 0 * inf into NaN. StoppingRatioLogistic, Kumaraswamy.log_cdf and Geometric.log_cdf go through the same path.

The double-where trick keeps the forward values as they are:

--- a/tensorflow_probability/python/math/generic.py
+++ b/tensorflow_probability/python/math/generic.py
@@ -703,10 +703,14 @@ def log1mexp(x, name=None):
     dtype = dtype_util.common_dtype([x], dtype_hint=tf.float32)
     x = tf.convert_to_tensor(x, dtype=dtype, name='x')
     x = tf.math.abs(x)
+    # This switching point is recommended in [1].
+    small = x < np.log(2)
+    # The branch not taken must not see small x: its derivative is infinite
+    # where exp(-x) rounds to 1, and would make the gradient NaN.
+    safe_x = tf.where(small, tf.ones_like(x), x)
     return tf.where(
-        # This switching point is recommended in [1].
-        x < np.log(2), tf.math.log(-tf.math.expm1(-x)),
-        tf.math.log1p(-tf.math.exp(-x)))
+        small, tf.math.log(-tf.math.expm1(-x)),
+        tf.math.log1p(-tf.math.exp(-safe_x)))

I ran into this while formalizing a few distribution formulas in Lean (TorchLean) and comparing them with TFP. 0.25.0 and the 2026-11-12 nightlies give the numbers above, on CPU. I have a gradient test for it in generic_test.py and can send the PR whenever you'd like :)

Lingua principale
Jupyter Notebook
Stelle
4.4k
Fork
1.1k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di tensorflow/probability

Tutte le issue di tensorflow/probability

Issue simili

Altre issue su Machine Learning

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.