python - tf.shape() 在 tensorflow 中得到错误的形状

标签 python python-3.x tensorflow tensor

我这样定义一个张量:

x = tf.get_variable("x", [100])

但是当我尝试打印张量的形状时:

打印(tf.shape(x))

我得到Tensor("Shape:0", shape=(1,), dtype=int32),为什么输出的结果不应该是shape=(100)

最佳答案

tf.shape(input, name=None)返回一个表示输入形状的一维整数张量。

您正在寻找:返回 x 变量的 TensorShapex.get_shape()

更新:由于这个答案,我写了一篇文章来阐明 Tensorflow 中的动态/静态形状:https://pgaleone.eu/tensorflow/2018/07/28/understanding-tensorflow-tensors-shape-static-dynamic/

关于python - tf.shape() 在 tensorflow 中得到错误的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37085430/

相关文章:

python - rpyc: root.getmodule ("module_name")和手动返回模块引用之间的区别?

python-3.x - StratifiedKFold vs StratifiedShuffleSplit vs StratifiedKFold + Shuffle

python - flask +异步

python - 使用相同的图在 TensorFlow 中显示训练和验证的准确性

python - 构造函数(Python)之外的方法中的实例变量 - 为什么以及如何?

Python 字典数组 - 每个值一行

tensorflow - Model.fit() 是否将整个训练数据集上传到 GPU?

tensorflow - Keras - 卡住模型,然后添加可训练层

python - 不断收到未定义的全局名称错误

python-3.x - pandas 自定义排序多级索引