TensorFlow:AttributeError:'Tensor'对象没有属性'shape' [英] TensorFlow: AttributeError: 'Tensor' object has no attribute 'shape'

查看:162
本文介绍了TensorFlow:AttributeError:'Tensor'对象没有属性'shape'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下使用TensorFlow的代码.重塑列表后,它会显示

I have the following code which uses TensorFlow. After I reshape a list, it says

AttributeError:张量"对象没有属性"shape"

AttributeError: 'Tensor' object has no attribute '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

输出:

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(名称,* args,** kwargs)文件"/home/shehab/Dropbox/py-projects/try-tf/logistic_regression.py",行 77,在 打印"train_data.shape:" + str(train_data.shape)AttributeError:'Tensor'对象没有属性'shape'

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'

谁能告诉我我想念的东西吗?

Could anyone please tell me what I am missing?

推荐答案

更新:从TensorFlow 1.0开始,tf.Tensor现在具有

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没有.shape属性.您应该改用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:

train_data = tf.reshape(train_data, [400, 1])
print "train_data.shape: " + str(train_data.get_shape())

请注意,通常您可能无法获得TensorFlow操作结果的实际形状.在某些情况下,形状将是一个计算值,具体取决于运行计算以找到其值.甚至可能从一次运行到下一次运行都不同(例如,).

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:AttributeError:'Tensor'对象没有属性'shape'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆