close

[Solved] TensorFlow: AttributeError: ‘Tensor’ object has no attribute ‘shape’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error TensorFlow: AttributeError: ‘Tensor’ object has no attribute ‘shape’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How TensorFlow: AttributeError: ‘Tensor’ object has no attribute ‘shape’ Error Occurs?

Today I get the following error TensorFlow: AttributeError: ‘Tensor’ object has no attribute ‘shape’ in python.

How To Solve TensorFlow: AttributeError: ‘Tensor’ object has no attribute ‘shape’ Error ?

  1. How To Solve TensorFlow: AttributeError: 'Tensor' object has no attribute 'shape' Error ?

    To Solve TensorFlow: AttributeError: 'Tensor' object has no attribute 'shape' Error import tensorflow as tf
    and replace train_data.shape with tf.Session.run(tf.rank(train_data))

  2. TensorFlow: AttributeError: 'Tensor' object has no attribute 'shape'

    To Solve TensorFlow: AttributeError: 'Tensor' object has no attribute 'shape' Error import tensorflow as tf
    and replace train_data.shape with tf.Session.run(tf.rank(train_data))

Solution 1

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

Solution 2

import tensorflow as tf

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

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read