AttributeError: 'tuple' object has no attribute 'rank' with tfp.layers.DenseVariational and Keras Input (TF 2.18.0, TFP 0.24.0)
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 42/100
- Type d'issue
- Bug
- Clarté
- Plutôt claire
- Activité
- À l'abandon
- Stack technique
- numpy, python
- Domaine
- machine-learning
Piste de recherche
Reproduisez l’exemple minimal de Functional API avec tfp.layers.DenseVariational, puis examinez la vérification de compatibilité dans tf_keras/src/engine/input_spec.py ainsi que le chemin d’appel de DenseVariational indiqué dans le traceback. Comparez la gestion de la forme d’entrée qui atteint x.shape.rank ; c’est terminé lorsque le modèle peut être créé et résumé sans l’AttributeError de tuple-rank.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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.0tensorflow-probability==0.24.0numpy==1.26.4
- Run the following minimal code snippet:
import tensorflow as tf
import tensorflow_probability as tfp
tfk = tf.keras
tfkl = tf.keras.layers
tfd = tfp.distributions
print(f"TensorFlow Version: {tf.__version__}")
print(f"TensorFlow Probability Version: {tfp.__version__}")
print(f"NumPy Version: {np.__version__}")
input_feature_size = 60
num_classes = 3
num_train_samples_example = 1000
kl_divergence_weight = 1.0 / num_train_samples_example
try:
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_fn
x = 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.")
except Exception as e:
print(f"\nError during model creation: {e}")
import traceback
traceback.print_exc()
An unexpected error occurred in main: 'tuple' object has no attribute 'rank'
Traceback (most recent call last):
File "C:\Users\W10\PycharmProjects\eurusd5\eurusd5.py", line 239, in <module>
accuracies, true_labels, predicted_labels = walk_forward_tfp_bnn(
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\W10\PycharmProjects\eurusd5\eurusd5.py", line 151, in walk_forward_tfp_bnn
bnn_model = create_bnn_model(input_shape_for_model, num_classes, len(X_train_scaled))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\W10\PycharmProjects\eurusd5\eurusd5.py", line 78, in create_bnn_model
x = tfp.layers.DenseVariational(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\W10\PycharmProjects\eurusd5\.venv\Lib\site-packages\tf_keras\src\utils\traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\W10\PycharmProjects\eurusd5\.venv\Lib\site-packages\tf_keras\src\engine\input_spec.py", line 251, in assert_input_compatibility
ndim = x.shape.rank
^^^^^^^^^^^^
AttributeError: 'tuple' object has no attribute 'rank'
System information
OS Platform and Distribution: Windows 10
TensorFlow installed from: pip
TensorFlow version: 2.18.0
TensorFlow Probability version: 0.24.0
Python version: 3.11.9
NumPy version: 1.26.4
CUDA/cuDNN version: N/A (CPU only)
GPU model and memory: N/A (CPU only)
Additional context
The issue appears to be specific to how tfp.layers.DenseVariational (in TFP 0.24.0) interacts with the Keras Functional API'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's default make_posterior_fn and make_prior_fn.
Thank you for your help!
- Langage dominant
- Jupyter Notebook
- Étoiles
- 4.4k
- Forks
- 1.1k
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Aucun modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de tensorflow/probability
-
KL divergence between two `TransformedDistribution`s with a shared non-injective bijector (`AbsoluteValue`) returns the base KLPeut-être pris @Andreas-Hum l’a pris il y a 8 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
tensorflow/probability#2036 ·
-
Difficulté 4/5 3-5 jours Accessibilité débutants 54/100
tensorflow/probability#2040 ·
-
Difficulté 3/5 1-2 jours Accessibilité débutants 68/100
tensorflow/probability#2034 ·
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 35/100
tensorflow/probability#2021 ·
-
Difficulté 4/5 3-5 jours Accessibilité débutants 30/100
tensorflow/probability#2015 ·
Toutes les issues de tensorflow/probability
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
JdeRobot/PerceptionMetrics#680 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
TuringLang/Turing.jl#2917 ·
Les mainteneurs répondent en général sous 1 jour
-
bug good first issue
Difficulté 2/5 1-3 heures Accessibilité débutants 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-zeroPeut-être pris @jafarovvvvvvv l’a pris aujourd’hui. Ouvertebug claimed difficulty:2
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
MakazhanAlpamys/Soup#2680 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
training
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
tinaudio/synth-setter#3758 ·
Les mainteneurs répondent en général sous 1 jour