python - Tensorflow Shape 必须为 1 级,但为 2 级

标签 python tensorflow

当我像这样声明变量时:

x = tf.Variable([len(_ELEMENT_LIST), 4], dtype=tf.float32)

我收到以下错误:

E0622 20:04:25.241938   21886 app.py:544] Top-level exception: Shape must be rank 1 but is rank 2 for 'input_layer/concat' (op: 'ConcatV2') with input shapes: [5], [5,1], [5,1], [].
E0622 20:04:25.252672   21886 app.py:545] Traceback (most recent call last):

当我这样做时:

x = tf.get_variable("x", [len(_ELEMENT_LIST), 4])

有效

我正在尝试使用 concat 计算张量。

tf.concat([
        x, features["y"],
        features["z"]
    ], 1)

最佳答案

x = tf.Variable([len(_ELEMENT_LIST), 4], dtype=tf.float32)

tf.Variable的第一个参数是变量的初始值,所以上面的语句中x是一个值为 [len(_ELEMENT_LIST), 4] 的变量,其形状等级为1。

x = tf.get_variable("x", [len(_ELEMENT_LIST), 4])

tf.get_variable的第二个参数是Variable的形状,所以Variable X的形状的等级是2。

关于python - Tensorflow Shape 必须为 1 级,但为 2 级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44711166/

相关文章:

python - 针对一堆 S3 文件运行 python 脚本

python - 张量板 : No dashboards active for current dataset

python - 如何在 tensorflow 中保存文本分类模型?

python - 卷积自动编码器的 Keras valueerror

python - __add__ 的对立面?

android - 错误 :tensorflow:Couldn't understand architecture name ''

python - pandas read_sql。如何使用日期字段的where子句进行查询

tensorflow - TensorFlow Saver、Exporter 和 Save Model 之间的关系

tensorflow - 通过finetuning训练全卷积网络时如何处理BatchNorm层?

python - 对制表符分隔的文件执行计算