24

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?

Hooked
88.9k46 gold badges197 silver badges272 bronze badges
asked Jul 29, 2016 at 19:14

3 Answers 3

30

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 "?").

answered Jul 29, 2016 at 19:21
Sign up to request clarification or add additional context in comments.

5 Comments

Then how 'print "train_data.shape: " + str(train_data.shape)' is outputting 'train_data.shape: (400,)'?
I assume the first train_data.shape is a NumPy array. After you reassign the result of tf.reshape() to train_data its type is a tf.Tensor.
In APIr.1.0 there is shape attribute for tf.Tensor: tensorflow.org/api_docs/python/tf/Tensor#shape
@tuned Thanks for pointing that out! I've updated the answer to make that clear.
@mrry calling Tensorobject.shape for placeholders would return None as the dimension, I saw somewhere you answered calling tf.shape may help, but it will return a tensor. Isn't there any way that tf.shape returns list of integers? I have to mention that I don't want to run eval or run.
2
import tensorflow as tf

and replace train_data.shape with tf.Session.run(tf.rank(train_data))

David Ongaro
4,0991 gold badge29 silver badges38 bronze badges
answered Jul 19, 2018 at 17:28

Comments

0

Use tf.shape(tensor), or tf.get_shape(tensor).

answered Mar 20, 2019 at 9:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.