Python TensorFlow:AttributeError:“张量”对象没有“形状”属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38666040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TensorFlow: AttributeError: 'Tensor' object has no attribute 'shape'
提问by Omar Shehab
I have the following code which uses TensorFlow. After I reshape a list, it says
我有以下使用 TensorFlow 的代码。在我重塑列表后,它说
AttributeError: 'Tensor' object has no attribute 'shape'
AttributeError: 'Tensor' 对象没有属性 'shape'
when I try to print its shape.
当我尝试打印它的形状时。
# Get the shape of the training data.
print "train_data.shape: " + str(train_data.shape)
train_data = tf.reshape(train_data, [400, 1])
print "train_data.shape: " + str(train_data.shape)
train_size,num_features = train_data.shape
Output:
输出:
train_data.shape: (400,) Traceback (most recent call last): File "", line 1, in File "/home/shehab/Downloads/tools/python/pycharm-edu-2.0.4/helpers/pydev/pydev_import_hook.py", line 21, in do_import module = self._system_import(name, *args, **kwargs) File "/home/shehab/Dropbox/py-projects/try-tf/logistic_regression.py", line 77, in print "train_data.shape: " + str(train_data.shape) AttributeError: 'Tensor' object has no attribute 'shape'
train_data.shape: (400,) 回溯(最近一次调用):文件“”,第 1 行,文件“/home/shehab/Downloads/tools/python/pycharm-edu-2.0.4/helpers/pydev/pydev_import_hook .py”,第 21 行,在 do_import 模块 = self._system_import(name, *args, **kwargs) 文件“/home/shehab/Dropbox/py-projects/try-tf/logistic_regression.py”,第 77 行,在打印“train_data.shape:” + str(train_data.shape) AttributeError: 'Tensor' 对象没有属性 'shape'
Could anyone please tell me what I am missing?
谁能告诉我我错过了什么?
回答by mrry
UPDATE:Since TensorFlow 1.0, tf.Tensor
now has a tf.Tensor.shape
property, which returns the same value as tf.Tensor.get_shape()
.
更新:从 TensorFlow 1.0 开始,tf.Tensor
现在有一个tf.Tensor.shape
属性,它返回与tf.Tensor.get_shape()
.
Indeed, in versions prior to TensorFlow 1.0 tf.Tensor
doesn't have a .shape
property. You should use the Tensor.get_shape()
method instead:
实际上,在 TensorFlow 1.0 之前的版本tf.Tensor
中没有.shape
属性。您应该改用该Tensor.get_shape()
方法:
train_data = tf.reshape(train_data, [400, 1])
print "train_data.shape: " + str(train_data.get_shape())
Note that in general you might not be able to get the actual shape of the result of a TensorFlow operation. In some cases, the shape will be a computed value that depends on running the computation to find its value; and it may even vary from one run to the next (e.g. the shape of tf.unique()
). In that case, the result of get_shape()
for some dimensions may be None
(or "?"
).
请注意,通常您可能无法获得 TensorFlow 操作结果的实际形状。在某些情况下,形状将是一个计算值,它依赖于运行计算来找到它的值;它甚至可能会因一次运行而异(例如 的形状tf.unique()
)。在这种情况下,get_shape()
某些维度的结果可能是None
(或"?"
)。
回答by Sahil Unagar
import tensorflow as tf
and replace train_data.shape
with tf.Session.run(tf.rank(train_data))
并替换train_data.shape
为tf.Session.run(tf.rank(train_data))
回答by rosefun
Use tf.shape(tensor)
, or tf.get_shape(tensor)
.
使用tf.shape(tensor)
, 或tf.get_shape(tensor)
。