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.
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.
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.
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With