Error in HamiltonianMonteCarlo when using gather in log_prob function
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 30/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- python, tensorflow
调研方向
从提供的 sample_hmc 和 logprob 复现开始,重点关注 tfp.mcmc.sample_chain、HamiltonianMonteCarlo 以及 @tf.function 边界。比较 tf.gather 和仅归约这两种情况,然后验证报告的结构错误已得到解决,同时保持图执行启用,并且采样仍会返回样本和接受值。
由索引模型根据 Issue 内容生成。
描述
I am trying to run a Hamiltonian MCMC on a target distribution whose implementation involves a call to tf.gather. The following code:
import tensorflow as tf
import tensorflow_probability as tfp
def sample_hmc(
target_log_prob_fn,
current_state,
num_results=1000,
num_burnin_steps=500,
adaptation_frac=0.8,
num_leapfrog_steps=3,
step_size=1.,
):
hmc = tfp.mcmc.SimpleStepSizeAdaptation(
tfp.mcmc.HamiltonianMonteCarlo(
target_log_prob_fn=target_log_prob_fn,
num_leapfrog_steps=num_leapfrog_steps,
step_size=step_size,
),
num_adaptation_steps=int(num_burnin_steps * adaptation_frac))
@tf.function
def run_chain():
samples, is_accepted = tfp.mcmc.sample_chain(
num_results=num_results,
num_burnin_steps=num_burnin_steps,
current_state=current_state,
kernel=hmc,
trace_fn=lambda _, pkr: pkr.inner_results.is_accepted,
)
return samples, is_accepted
return run_chain()
def logprob(alpha):
indices = tf.constant([2, 0, 1], dtype=tf.int32)
return -tf.math.reduce_sum(tf.gather(alpha**2, indices))
# return -tf.math.reduce_sum(alpha**2)
alpha = tf.constant([1.0, 1.0, 1.0])
sample_hmc(
logprob,
current_state=alpha,
num_results=10,
num_burnin_steps=5,
)
raises ValueError: The two structures don't have the same nested structure. followed by a very long and (to me) cryptic message. Replacing the return statement in the logprob function with the commented line gets rid of the error. The error seems to appear whenever the result of logprob contains a tf.gather subexpression.
The error also disappears when I remove the @tf.function decorator from the definition of run_chain. However, this comes at a huge performance cost.
How can I efficiently sample from a distribution whose log-probability involves a tf.gather expression?
- 主要语言
- Jupyter Notebook
- 星标
- 4.4k
- 派生
- 1.1k
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
tensorflow/probability 的其他 Issue
-
KL divergence between two `TransformedDistribution`s with a shared non-injective bijector (`AbsoluteValue`) returns the base KL可能已有人在做 @Andreas-Hum 于 8 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
tensorflow/probability#2036 ·
-
难度 4/5 3-5 天 新手友好度 54/100
tensorflow/probability#2040 ·
-
难度 3/5 1-2 天 新手友好度 68/100
tensorflow/probability#2034 ·
-
难度 5/5 一周以上 新手友好度 35/100
tensorflow/probability#2021 ·
-
难度 4/5 3-5 天 新手友好度 30/100
tensorflow/probability#2015 ·
查看 tensorflow/probability 的全部 Issue
相似的 Issue
-
bug good first issue
难度 2/5 1-3 小时 新手友好度 84/100
Selectus2/trainers-rb#29 ·
-
test_issue1702 hardware test fails in a full `-m gpu` run: it reads a process-wide pinned-memory counter that test_issue1174 leaves non-zero可能已有人在做 @jafarovvvvvvv 今天认领。 未关闭bug claimed difficulty:2
难度 2/5 1-3 小时 新手友好度 75/100
MakazhanAlpamys/Soup#2680 · 2 条评论 ·
维护者通常 1 天内回复
-
training
难度 2/5 1-3 小时 新手友好度 72/100
tinaudio/synth-setter#3758 ·
维护者通常 1 天内回复
-
bot-triaged module: correctness (silent) module: data module: error checking triage review
难度 2/5 1-3 小时 新手友好度 71/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
vllm-project/vllm-ascend#21392 · 1 条评论 ·
维护者通常 1 天内回复