TensorFlow:AttributeError:“张量"对象没有“形状"属性 [英] TensorFlow: AttributeError: 'Tensor' object has no attribute 'shape'

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

问题描述

我有以下使用 TensorFlow 的代码.在我重塑列表后,它说

<块引用>

AttributeError: 'Tensor' 对象没有属性 'shape'

当我尝试打印它的形状时.

# 获取训练数据的形状.打印train_data.shape:" + str(train_data.shape)train_data = tf.reshape(train_data, [400, 1])打印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(name, *args, **kwargs) 文件/home/shehab/Dropbox/py-projects/try-tf/logistic_regression.py",行77,在打印train_data.shape:" + str(train_data.shape) AttributeError: 'Tensor' 对象没有属性 'shape'

谁能告诉我我错过了什么?

解决方案

更新: 从 TensorFlow 1.0 开始,tf.Tensor 现在有了 tf.Tensor.shape 属性,返回与 tf.Tensor.get_shape().><小时>

确实,在 TensorFlow 1.0 之前的版本中,tf.Tensor 没有 .shape 属性.您应该改用 Tensor.get_shape() 方法:

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

请注意,通常您可能无法获得 TensorFlow 操作结果的实际形状.在某些情况下,形状将是一个计算值,它依赖于运行计算来找到它的值;它甚至可能因一次运行而异(例如 tf.unique()).在这种情况下,某些维度的 get_shape() 的结果可能是 None(或 "?").

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

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

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'

Could anyone please tell me what I am missing?

解决方案

UPDATE: Since TensorFlow 1.0, tf.Tensor now has a tf.Tensor.shape property, which returns the same value as 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:

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

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