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
Describe the bug
I am encountering an AttributeError: 'tuple' object has no attribute 'rank' when using tfp.layers.DenseVariational as the first layer following a tf.keras.Input layer in a Keras Functional API model. The error seems to occur during Keras's internal InputSpec compatibility check, where it expects a tf.TensorShape object but receives a Python tuple for the input shape from the DenseVariational layer.
To Reproduce
Steps to reproduce the behavior:
Environment: Python 3.11.9 on Windows 10.
Installed packages:
tensorflow==2.18.0
tensorflow-probability==0.24.0
numpy==1.26.4
Run the following minimal code snippet:
importtensorflowastfimporttensorflow_probabilityastfptfk=tf.kerastfkl=tf.keras.layerstfd=tfp.distributionsprint(f"TensorFlow Version: {tf.__version__}")
print(f"TensorFlow Probability Version: {tfp.__version__}")
print(f"NumPy Version: {np.__version__}")
input_feature_size=60num_classes=3num_train_samples_example=1000kl_divergence_weight=1.0/num_train_samples_exampletry:
inputs=tfk.Input(shape=(input_feature_size,), name="input_layer")
posterior_fn_constructor=tfp.layers.default_mean_field_normal_fn(is_singular=True)
prior_fn_constructor=tfp.layers.default_multivariate_normal_fnx=tfp.layers.DenseVariational(
units=64,
make_posterior_fn=posterior_fn_constructor,
make_prior_fn=prior_fn_constructor,
kl_weight=kl_divergence_weight,
activation='relu',
name="dv1"
)(inputs)
outputs=tfkl.Dense(units=num_classes, name="logits")(x)
model=tfk.Model(inputs=inputs, outputs=outputs, name="bnn_test_model")
model.summary()
print("Model created successfully.")
exceptExceptionase:
print(f"\nError during model creation: {e}")
importtracebacktraceback.print_exc()
Anunexpectederroroccurredinmain: 'tuple'objecthasnoattribute'rank'Traceback (mostrecentcalllast):
File"C:\Users\W10\PycharmProjects\eurusd5\eurusd5.py", line239, in<module>accuracies, true_labels, predicted_labels=walk_forward_tfp_bnn(
^^^^^^^^^^^^^^^^^^^^^File"C:\Users\W10\PycharmProjects\eurusd5\eurusd5.py", line151, inwalk_forward_tfp_bnnbnn_model=create_bnn_model(input_shape_for_model, num_classes, len(X_train_scaled))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"C:\Users\W10\PycharmProjects\eurusd5\eurusd5.py", line78, increate_bnn_modelx=tfp.layers.DenseVariational(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"C:\Users\W10\PycharmProjects\eurusd5\.venv\Lib\site-packages\tf_keras\src\utils\traceback_utils.py", line70, inerror_handlerraisee.with_traceback(filtered_tb) fromNoneFile"C:\Users\W10\PycharmProjects\eurusd5\.venv\Lib\site-packages\tf_keras\src\engine\input_spec.py", line251, inassert_input_compatibilityndim=x.shape.rank^^^^^^^^^^^^AttributeError: 'tuple'objecthasnoattribute'rank'SysteminformationOSPlatformandDistribution: Windows10TensorFlowinstalledfrom: pipTensorFlowversion: 2.18.0TensorFlowProbabilityversion: 0.24.0Pythonversion: 3.11.9NumPyversion: 1.26.4CUDA/cuDNNversion: N/A (CPUonly)
GPUmodelandmemory: N/A (CPUonly)
AdditionalcontextTheissueappearstobespecifictohowtfp.layers.DenseVariational (inTFP0.24.0) interactswiththeKerasFunctionalAPI's input shape handling when using TensorFlow 2.18.0, particularly for 1D vector inputs. Attempts to use batch_input_shape in tf.keras.Input or explicitly pass input_shape to DenseVariational did not resolve this specific "rank" error. The error occurs even when using TFP'sdefaultmake_posterior_fnandmake_prior_fn.
Thankyouforyourhelp!
Describe the bug
I am encountering an
AttributeError: 'tuple' object has no attribute 'rank'when usingtfp.layers.DenseVariationalas the first layer following atf.keras.Inputlayer in a Keras Functional API model. The error seems to occur during Keras's internalInputSpeccompatibility check, where it expects atf.TensorShapeobject but receives a Python tuple for the input shape from theDenseVariationallayer.To Reproduce
Steps to reproduce the behavior:
tensorflow==2.18.0tensorflow-probability==0.24.0numpy==1.26.4