You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
importtensorflowastfimporttensorflow_probabilityastfpimporttf_kerasastfkimportnumpyasnptfd=tfp.distributionstfpl=tfp.layers# Generate synthetic datanp.random.seed(42)
x=np.random.randn(2000, 1).astype(np.float32)
true_w, true_b=2.0, -1.0y=true_w*x+true_b+np.random.randn(2000, 1).astype(np.float32) *0.5# Define the JointDistributionNamed modeldefmake_joint(x):
returntfd.JointDistributionNamed({
"weight": tfd.Normal(loc=0., scale=1.),
"bias": tfd.Normal(loc=0., scale=1.),
"obs": lambdaweight, bias: tfd.Independent(
tfd.Normal(loc=weight*x+bias, scale=0.5),
reinterpreted_batch_ndims=1
)
})
# Keras model using DistributionLambdainputs=tfk.Input(shape=(1,))
dense=tfk.layers.Dense(2)(inputs) # Not used for parameterization here, just as a placeholderdefposterior_fn(params):
# For demonstration, use fixed x from outer scopejd=make_joint(x)
returnjdoutputs=tfpl.DistributionLambda(posterior_fn)(dense)
model=tfk.Model(inputs=inputs, outputs=outputs)
# Custom loss: negative log-likelihood of observed y under the jointdefnll(y_true, y_pred):
# y_pred is a JointDistributionNamed, so evaluate log_prob of obsreturn-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:
---------------------------------------------------------------------------AttributeErrorTraceback (mostrecentcalllast)
[<ipython-input-1-e7f1317b9e9b>](https://localhost:8080/#) in <cell line: 0>()33returnjd34--->35outputs=tfpl.DistributionLambda(posterior_fn)(dense)
36model=tfk.Model(inputs=inputs, outputs=outputs)
373frames
[/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 Keras184# is losing the distribution handle for activity_regularizer.-->185value._tfp_distribution=distribution# pylint: disable=protected-access186# TODO(b/120153609): Keras is incorrectly presuming everything is a187# `tf.Tensor`. Closing this bug entails ensuring Keras only accessesAttributeError: Exceptionencounteredwhencallinglayer"distribution_lambda" (type DistributionLambda).
'dict'objecthasnoattribute'_tfp_distribution'Callargumentsreceivedbylayer"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.
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:
And I got this error:
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.The text was updated successfully, but these errors were encountered: