Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Something wrong with the creation of a Keras model with probabilistic layers. #1834

Open
gutilacerda opened this issue Aug 14, 2024 · 2 comments

Comments

@gutilacerda
Copy link

gutilacerda commented Aug 14, 2024

I am applying the use of Keras models that utilize probabilistic layers. The model below, which used to work normally, now presents the following error:

inputs = tf.keras.Input(shape=(X_train.shape[1],))
x = layers.Dense(500, activation='relu')(inputs)
x2 = layers.Dense(350, activation='relu')(x)
x3 = layers.Dense(250, activation='relu')(x2)
x4 = layers.Dense(150, activation='relu')(x3)
x5 = layers.Dense(50, activation='relu')(x4)
x6 = layers.Dense(15, activation='relu')(x5)
y_variational = tfpl.DenseVariational(4, make_prior_fn=prior, make_posterior_fn=posterior, kl_weight=1/X_train.shape[0], activation='tanh')(x6)

distribution_params = layers.Dense(units=216*2)(y_variational)
outputs = tfpl.IndependentNormal(216)(distribution_params)

PBNN3 = tf.keras.Model(inputs=inputs, outputs=outputs)


AttributeError Traceback (most recent call last)
in <cell line: 8>()
6 x5 = layers.Dense(50, activation='relu')(x4) #15
7 x6 = layers.Dense(15, activation='relu')(x5)
----> 8 y_variational = tfpl.DenseVariational(4, make_prior_fn=prior, make_posterior_fn=posterior, kl_weight=1/X_train.shape[0], activation='tanh')(x6)
9 #y_variational = layers.Dense(10, activation='relu')(x)
10 distribution_params = layers.Dense(units=216*2)(y_variational)

1 frames
/usr/local/lib/python3.10/dist-packages/tf_keras/src/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
249 )
250 if spec.min_ndim is not None:
--> 251 ndim = x.shape.rank
252 if ndim is not None and ndim < spec.min_ndim:
253 raise ValueError(

AttributeError: 'tuple' object has no attribute 'rank'

Something is not working with TensorFlow version 2.17.0 and TensorFlow Probability version 0.24. Still works for Tensorflow: 2.15.1 and TensorFlow Probability: 0.22.1.

Thank you!

@kittmiller25
Copy link

kittmiller25 commented Aug 25, 2024

I was having a similar issue and just switched all of my tf.keras calls to tf_keras and it solved it. I believe it is regarding the Keras 2 vs Keras 3 discrepancy in the 0.24 release notes.

https://github.com/tensorflow/probability/releases
NOTE: In TensorFlow 2.16+, tf.keras (and tf.initializers, tf.losses, and tf.optimizers) refers to Keras 3. TensorFlow Probability is not compatible with Keras 3 -- instead TFP is continuing to use Keras 2, which is now packaged as tf-keras and tf-keras-nightly and is imported as tf_keras. When using TensorFlow Probability with TensorFlow, you must explicitly install Keras 2 along with TensorFlow (or install tensorflow-probability[tf] or tfp-nightly[tf] to automatically install these dependencies.)

The one that wasn't mentioned but I think is the one that actually got my code working again was the Model itself so:
tf.keras.Model -> tf_keras.Model
I went wholesale and did it to all of my layers, activation functions, optimizers, etc. but I'm not sure if that was necessary

@jhn-nt
Copy link

jhn-nt commented Sep 3, 2024

is there a roadmap to make TFP support Keras 3?

Thanks for all the amazing work!
Giovanni

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants