keras JointDistribution minimal example not working
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 30/100
- Issue 類型
- 缺陷
- 描述清晰度
- 基本清楚
- 活躍度
- 停滯
- 技術堆疊
- jupyter-notebook, python
研究方向
使用 JointDistributionNamed 範例重現連結的 Colab,並檢查 tensorflow_probability/python/layers/distribution_layer.py 中 traceback 所示的 DistributionLambda 呼叫。比較預期的 distribution 回傳值與觀察到的 dict 錯誤,並確認是否需要一條可正常運作的 model.fit 路徑,或者需要記錄一項已文件化的限制。
由索引模型根據 Issue 內容生成。
描述
Hi,
I am trying to train a keras model using a mix of JointDistribution and other layers. My goal is to be able to build the model, compile and use model.fit on data.
Somehow, I keep getting errors and can't get it working.
I could not find a minimal example with it running and I think I am missing something.
Here is the minimal example I tried:
import tensorflow as tf
import tensorflow_probability as tfp
import tf_keras as tfk
import numpy as np
tfd = tfp.distributions
tfpl = tfp.layers
# Generate synthetic data
np.random.seed(42)
x = np.random.randn(2000, 1).astype(np.float32)
true_w, true_b = 2.0, -1.0
y = true_w * x + true_b + np.random.randn(2000, 1).astype(np.float32) * 0.5
# Define the JointDistributionNamed model
def make_joint(x):
return tfd.JointDistributionNamed({
"weight": tfd.Normal(loc=0., scale=1.),
"bias": tfd.Normal(loc=0., scale=1.),
"obs": lambda weight, bias: tfd.Independent(
tfd.Normal(loc=weight * x + bias, scale=0.5),
reinterpreted_batch_ndims=1
)
})
# Keras model using DistributionLambda
inputs = tfk.Input(shape=(1,))
dense = tfk.layers.Dense(2)(inputs) # Not used for parameterization here, just as a placeholder
def posterior_fn(params):
# For demonstration, use fixed x from outer scope
jd = make_joint(x)
return jd
outputs = tfpl.DistributionLambda(posterior_fn)(dense)
model = tfk.Model(inputs=inputs, outputs=outputs)
# Custom loss: negative log-likelihood of observed y under the joint
def nll(y_true, y_pred):
# y_pred is a JointDistributionNamed, so evaluate log_prob of obs
return -y_pred.log_prob({"obs": y_true})
model.compile(optimizer=tf.optimizers.Adam(0.01), loss=nll)
model.fit(x, y, epochs=5, batch_size=32)
And I got this error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
[<ipython-input-1-e7f1317b9e9b>](https://localhost:8080/#) in <cell line: 0>()
33 return jd
34
---> 35 outputs = tfpl.DistributionLambda(posterior_fn)(dense)
36 model = tfk.Model(inputs=inputs, outputs=outputs)
37
3 frames
[/usr/local/lib/python3.11/dist-packages/tensorflow_probability/python/layers/distribution_layer.py](https://localhost:8080/#) in _fn(*fargs, **fkwargs)
183 # TODO(b/126056144): Remove silent handle once we identify how/why Keras
184 # is losing the distribution handle for activity_regularizer.
--> 185 value._tfp_distribution = distribution # pylint: disable=protected-access
186 # TODO(b/120153609): Keras is incorrectly presuming everything is a
187 # `tf.Tensor`. Closing this bug entails ensuring Keras only accesses
AttributeError: Exception encountered when calling layer "distribution_lambda" (type DistributionLambda).
'dict' object has no attribute '_tfp_distribution'
Call arguments received by layer "distribution_lambda" (type DistributionLambda):
• inputs=tf.Tensor(shape=(None, 2), dtype=float32)
• args=<class 'inspect._empty'>
• kwargs={'training': 'None'}
I am posting it here because I feel this might be a bug.
You can reproduce it via this colab link
Please note I also tried with tfd.JointDistributionSequential and no luck either.
- 主要語言
- 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 於 9 天前認領。 未關閉
難度 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
-
OODKnowledgeMetric never parses answer letters and divides by zero when all answers are refusals可能已有人在做 @davrens101 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 82/100
stanford-crfm/helm#4379 ·
-
難度 2/5 1-3 小時 新手友好度 72/100
JdeRobot/PerceptionMetrics#680 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 76/100
TuringLang/Turing.jl#2917 ·
維護者通常 1 天內回覆
-
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 天內回覆