`tfp.math.log1mexp` has a NaN gradient for small inputs (reaches `OrderedLogistic.log_prob`)
@aviralgarg05 đang làm issue này rồi.
Từ ngày 19/11/2026.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 86/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- python
- Lĩnh vực
- machine-learning
Hướng nghiên cứu
Bắt đầu từ tensorflow_probability/python/math/generic.py tại log1mexp, nơi một tf.where chọn giữa log(-expm1(-x)) và log1p(-exp(-x)). Tái hiện lỗi bằng đoạn mã trong issue, sau đó thêm một kiểm thử gradient trong generic_test.py. Hoàn thành khi gradient hữu hạn với x nhỏ và gradient của log_prob của OrderedLogistic tại loc=18 xấp xỉ 1.13e-07, đồng thời các giá trị forward không thay đổi.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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 :)
- Ngôn ngữ chính
- Jupyter Notebook
- Star
- 4.4k
- Fork
- 1.1k
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của tensorflow/probability
-
KL divergence between two `TransformedDistribution`s with a shared non-injective bijector (`AbsoluteValue`) returns the base KLCó thể đã có người làm @Andreas-Hum đã nhận 19 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
tensorflow/probability#2036 ·
-
`Weibull` and `Gumbel` `survival_function` / `log_survival_function` are 0 / -inf in the upper tailĐang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 70/100
tensorflow/probability#2042 ·
-
Chi2, Gamma, Beta and Binomial survival_function lose the upper tail because they fall back to 1 - cdfCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 54/100
tensorflow/probability#2040 · 1 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
tensorflow/probability#2034 ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
tensorflow/probability#2021 ·
Tất cả issue của tensorflow/probability
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
FluxML/Optimisers.jl#254 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
google-deepmind/sonnet#323 ·
-
train_test_split() silently drops rows when train_size + test_size < 1Có thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
huggingface/datasets#9098 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Module:Polygraphy
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
Maintainer thường phản hồi trong vòng 6 ngày
-
DoRA magnitude vectors are not loaded by Transformers load_adapter / from_pretrained(adapter_dir)Đang mởpost-v1
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
huggingface/peft#4208 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày