AttributeError: 'tuple' object has no attribute 'rank' with tfp.layers.DenseVariational and Keras Input (TF 2.18.0, TFP 0.24.0)
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 42/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- numpy, python
调研方向
使用 tfp.layers.DenseVariational 重现最小的 Functional API 示例,然后检查 tf_keras/src/engine/input_spec.py 中的兼容性检查,以及 traceback 中所指的 DenseVariational 调用路径。比较到达 x.shape.rank 的输入形状处理方式;当模型可以创建并完成摘要,且不会出现 tuple-rank AttributeError 时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
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!
- 主要语言
- Jupyter Notebook
- 星标
- 4.4k
- 派生
- 1.1k
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 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 一周以上 新手友好度 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 ·