Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TensorFlow concat a variable-sized placeholder with a vector

Let's say I have a placeholder

ph_input = tf.placeholder(dtype=tf.int32, [None, 1])

and a vector

h = tf.zeros([1,2], dtype=tf.int32)

In this example h is filled with zeros for simplicity but in the actual case it will be changed by other variables and will have different values.

I want to efficiently do a concat on ph_input and h on dimension 1 and get a new tensor with shape [None, 1+2]. Unfortunately concat needs all input tensors to have the same shape except the concat_dim, which my example does not meet.

I was considering expand h to the same shape as the data that feed to ph_input but am not exactly sure how to do that with the placeholder itself. If I get the shape from the input data directly then I guess it is not necessary to use the placeholder.

like image 256
bxshi Avatar asked Mar 16 '16 16:03

bxshi


People also ask

How do you initialize a TensorFlow variable in a matrix?

First, remember that you can use the TensorFlow eye functionality to easily create a square identity matrix. We create a 5x5 identity matrix with a data type of float32 and assign it to the Python variable identity matrix. So we used tf. eye, give it a size of 5, and the data type is float32.

What is placeholder in TensorFlow?

A placeholder is simply a variable that we will assign data to at a later date. It allows us to create our operations and build our computation graph, without needing the data. In TensorFlow terminology, we then feed data into the graph through these placeholders.


1 Answers

The most general solution is to use the tf.shape() op to get the run-time size of the placeholder, and the tf.tile() op to expand h to the appropriate size:

ph_input = tf.placeholder(dtype=tf.int32, shape=[None, 1])
h = tf.zeros([1, 2], dtype=tf.int32)  # ...or some other tensor of shape [1, 2]

# Get the number of rows in the fed value at run-time.
ph_num_rows = tf.shape(ph_input)[0]

# Makes a `ph_num_rows x 2` matrix, by tiling `h` along the row dimension.
h_tiled = tf.tile(h, tf.pack([ph_num_rows, 1]))

result = tf.concat(1, [ph_input, h_tiled])
like image 192
mrry Avatar answered Oct 12 '22 21:10

mrry