Tensor form of braided tensor

I have a measurement variable a (1, 5) that I want to “alternate” as many times as the size of my mini-batch. For example, if the mini-batch size is 32, then I want to build a tensor c dimension (32, 5), where each row has values ​​that are the same as the original (1, 5) variable a .

But I only know the mini-batch size at runtime: this is the dimension size 0 placeholder b : tf.shape(b)[0]

Here is my code for building c:

 a = tf.Variable(np.random.uniform(size=(1,5))) b = tf.placeholder(shape=[None, 12], dtype=tf.float32) batch_size = tf.shape(b)[0] c = tf.tile(a, tf.pack([batch_size, 1])) 

This is normal. However, c.get_shape() returns (?,?). I do not understand why this is not coming back (?, 5).

This causes a problem later in my code when I build the matrix variable W with the number of columns c.get_shape()[1] , which I expect to return 5, not ?.

Any help would be greatly appreciated. Thanks.

+5
source share
1 answer

[ EDIT: This was fixed in commit at TensorFlow on August 10, 2016.]

This is a well-known limitation of the output of the TensorFlow form: when the multiples argument for tf.tile() is a calculated value (for example, the result of tf.pack() here, and its value is not trivially calculated at the time of plotting (in this case, since it depends on a tf.placeholder() , which does not matter until it is submitted), the current output of the form will raise its hands and announce that the form is unknown (but with the same rank as the input, a ).

The current workaround is to use Tensor.set_shape() , which allows you to program additional form information when you know more than form output ... For example, you can do:

 a = tf.Variable(np.random.uniform(size=(1, 5))) b = tf.placeholder(shape=[None, 12], dtype=tf.float32) batch_size = tf.shape(b)[0] c = tf.tile(a, tf.pack([batch_size, 1])) c.set_shape([None, a.get_shape()[1]]) # or `c.set_shape([None, 5])` 

However, we recently added some functions that allow the distribution of partially calculated values ​​that can be used as shapes, and this can be adapted to help with the form function for tf.tile() . I created a GitHub issue to track this, and I have a fix tested right now.

+3
source

All Articles