Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically tile a tensor depending on the batch size

I have a 1D tensor a that I want to stack/pack/tile into a 2D tensor like y=[a, a, a]. If I knew how many times I wanted it repeated, I could use tf.tile along with reshape.

But I don't because the size is dependent on the batch size. The placeholder value is None which isn't a valid input. I know for tf.slice one can input -1 and let tensorflow figure it out, but I don't see how tensorflow could infer the correct size. I do have a tensor x that would be equal in shape to y, but I don't see a tile_like function.

Any suggestions?

like image 655
Nimitz14 Avatar asked Jan 29 '17 16:01

Nimitz14


1 Answers

You can use tf.shape to find out the runtime shape of a tensor, and use it as the basis for the argument to tf.tile:

import tensorflow as tf
import numpy as np

x = tf.placeholder(tf.float32, shape=[None, 3])

y = tf.tile([2, 3], tf.shape(x)[0:1])

sess = tf.Session()
print(sess.run(y, feed_dict={x: np.zeros([11, 3])}))

I verified this code works with the Tensorflow 1.0 release candidiate. Hope that helps!

like image 102
Peter Hawkins Avatar answered Sep 29 '22 10:09

Peter Hawkins