0

I have the following simple code in TensorFlow:

a = tf.placeholder(dtype = tf.float64, shape = (3, None))
b = tf.Variable(dtype = tf.float64, initial_value = np.random.randn(5, 3))
c = tf.matmul(b, a)
size = tf.shape(a)
t1, t2 = size

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    res = sess.run(size, feed_dict = {a: np.random.randn(3, 4)})

t1

but it does not work. I want to have the shape of the tensor, a.shape works fine but the point is that it gets None for the second dimension. I searched and got that for knowing its value I have to use tf.shape(a), but now the problem is that I searched and figured out that python does have any idea what is in the tensor object. I just want to retrieve the values in two variables. The point is that I have to use this code in a more complicated code and these sizes are the marginal part of a larger computation. Is there anyway to get those numbers as integers without running the session for them separately?

I have to say that I know the following variant of my code wroks:

a = tf.placeholder(dtype = tf.float64, shape = (3, None))
b = tf.Variable(dtype = tf.float64, initial_value = np.random.randn(5, 3))
c = tf.matmul(b, a)
size = a.shape.as_list()
print(type(size))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    res = sess.run(c, feed_dict = {a: np.random.randn(3, 4)})
    print(res)
    print(size)

but It gets None as the second element of shape. Consequently I have to use tf.shape. Those folks who insist that my question is duplicate, I ran the suggestion here and get the following result which still consists of None:

(3, ?)

  • @MateenUlhaq I've seen that. My question is not about tf.shape(). Tensor is not iteratable. I want to get a tuple of shape that be integer. as_list can not be called using tf.shape() anymore. – user9422652 Feb 28 '18 at 08:21
  • @MateenUlhaq Also I've seen [here](https://stackoverflow.com/questions/38666040/tensorflow-attributeerror-tensor-object-has-no-attribute-shape) but does not answer my question. – user9422652 Feb 28 '18 at 08:22
  • Possible duplicate of [tf.shape() get wrong shape in tensorflow](https://stackoverflow.com/questions/37085430/tf-shape-get-wrong-shape-in-tensorflow) – Jonas Adler Feb 28 '18 at 10:30

1 Answers1

0

Does this help? It's not very general but since I don't know what exactly you're trying to achieve, it might be enough.

a = tf.placeholder(dtype = tf.float64, shape = (3, None))
b = tf.Variable(dtype = tf.float64, initial_value = np.random.randn(5, 3))
c = tf.matmul(b, a)
size = tf.shape(a)
t1 = size[0]
t2 = size[1]

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    res = sess.run([t1, t2], feed_dict = {a: np.random.randn(3, 4)})
    print(res)

Alternative:

a = tf.placeholder(dtype = tf.float64, shape = (3, None))
b = tf.Variable(dtype = tf.float64, initial_value = np.random.randn(5, 3))
c = tf.matmul(b, a)
size = tf.shape(a)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    t1, t2 = sess.run(size, feed_dict = {a: np.random.randn(3, 4)})
print(t1, t2)
xdurch0
  • 9,905
  • 4
  • 32
  • 38
  • I want something like this but without sess.run. is that possible? – user9422652 Feb 28 '18 at 10:54
  • What do you mean? By definition the second dimension of the placeholder `a` is unknown. It is only known when a specific value is fed into the placeholder (i.e. during `session.run`). After the `run` call, any connection to whatever you fed in is lost. The shape of `a` will still be (3, None). – xdurch0 Feb 28 '18 at 11:56
  • Isn't it possible to run size, and t1 and t0 get the values? – user9422652 Feb 28 '18 at 12:04
  • I've added an alternative formulation to the answer. In that version, t1 and t2 hold the dimensions of the placeholder you put through `session.run`. – xdurch0 Feb 28 '18 at 13:47