keras JointDistribution minimal example not working
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 30/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- jupyter-notebook, python
調査の方向性
リンクされた Colab を JointDistributionNamed の例を使って再現し、traceback に示されている DistributionLambda 呼び出しの箇所で tensorflow_probability/python/layers/distribution_layer.py を調べてください。期待される 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 ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
tensorflow/probability#2034 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
tensorflow/probability#2021 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 30/100
tensorflow/probability#2015 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 48/100
tensorflow/probability#2013 ·
tensorflow/probability の issue をすべて見る
似ている issue
-
[Bug]: DoubleMLRDDData accepts a score_col that is no data column (as in its docstring example); the running variable then becomes a covariate and RDFlex fails対応中かも @vrjdev が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
DoubleML/doubleml-for-py#455 ·
メンテナーはふだん 1 日以内に返信
-
Install page: spectarf's CUDA engine fails with PyTorch for CUDA 13.0 (nvvmError: ERROR_INVALID_IR)対応中かも @lukepolson が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
PyTomography/PyTomography#435 ·
メンテナーはふだん 1 日以内に返信
-
beginner friendly
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
aws-samples/sample-per-model-bedrock#821 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
instadeepai/InstaNovo-FM#50 ·