2

I have a tensor, X of shape (T, n, k).

If I knew the shape beforehand, it is easy to reshape, tf.reshape(X, (T * n, k)) where T, n, k are ints, not tensors. But is there a way to do this if I don't know the shapes. It seems that getting the shapes like shape = tf.shape(X) and reshaping doesn't work. That is,

tf.reshape(X, (tf.shape[0] * tf.shape[1], tf.shape[2]))

Any ideas? In my application, T and k are known before runtime but n is only known at runtime.

Hooked
  • 84,485
  • 43
  • 192
  • 261
AsianYayaToure
  • 153
  • 1
  • 2
  • 11

2 Answers2

5

Take a look at this:

import tensorflow as tf

a, b, c = 2, 3, 4
x = tf.Variable(tf.random_normal([a, b, c], mean=0.0, stddev=1.0, dtype=tf.float32))
s = tf.shape(x)

init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
v1, v2, v3 = sess.run(s)
y = tf.reshape(x, [v1 * v2, v3])
shape = tf.shape(y)

print sess.run(y)
print sess.run(shape)

I am getting the shape of the variable after it's initialization and then use it later. Also take a look at this answer, as it deals with a similar thing.

Community
  • 1
  • 1
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • While I think this would work, I''m worried that it adds overhead to the computation. In Theano, `reshape()` accepts symbolic variables, I was wondering if there was a solution like that, maybe by concatenating the separate scalars into a new Tensor or something like this. – AsianYayaToure Nov 14 '15 at 07:01
  • @AsianYayaToure this clearly works because I tested it :-). I would not worry about the overhead at this stage. Make sure that your model works as expected and predicts what you want it to predict. – Salvador Dali Nov 14 '15 at 07:13
  • Actually, this doesn't work. In your example, the shape of `x` is known at graph building time. You've explicitly set `a, b, c = 2, 3, 4`. Try this: x = tf.placeholder(tf.int32, [100, None, 3]) and do the same. – AsianYayaToure Nov 14 '15 at 16:16
  • 1
    @AsianYayaToure I got you. You should have told that you use `tf.placeholder`. I will write a new answer, which works with placeholder. – Salvador Dali Nov 15 '15 at 06:59
2

Now that you told that you use placeholders to populate data, it started to make sense. Here is an example of how can you reshape your data in this case:

import tensorflow as tf
import numpy as np
data = np.random.rand(2, 3, 4)

x = tf.placeholder("float", None)
s = tf.shape(x)

sess = tf.Session()
shape_original = sess.run(s, feed_dict={x: data})

x_ = tf.reshape(x, [shape_original[0] * shape_original[1], shape_original[2]])
s_ = tf.shape(x_)

shape_now = sess.run(s_, feed_dict={x: data})
print 'Original\t', shape_original
print 'Now\t\t\t', shape_now

sess.close()
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753