How to explicitly broadcast a tensor to match another's shape in tensorflow?
EDIT: In all versions of TensorFlow since 0.12rc0, the code in the question works directly. TensorFlow will automatically stack tensors and Python numbers into a tensor argument. The solution below using tf.pack()
is only needed in versions prior to 0.12rc0. Note that tf.pack()
was renamed to tf.stack()
in TensorFlow 1.0.
Your solution is very close to working. You should replace the line:
C = tf.tile(C, [1,1,tf.shape(C)[2]])
...with the following:
C = tf.tile(C, tf.pack([1, 1, tf.shape(A)[2]]))
(The reason for the issue is that TensorFlow won't implicitly convert a list of tensors and Python literals into a tensor. tf.pack()
takes a list of tensors, so it will convert each of the elements in its input (1
, 1
, and tf.shape(C)[2]
) to a tensor. Since each element is a scalar, the result will be a vector.)
Here's a dirty hack:
import tensorflow as tf
def broadcast(tensor, shape):
return tensor + tf.zeros(shape, dtype=tensor.dtype)
A = tf.random_normal([20, 100, 10])
B = tf.random_normal([20, 100, 10])
C = tf.random_normal([20, 100, 1])
C = broadcast(C, A.shape)
D = tf.select(C, A, B)