I have three tensors,
A, B and C in tensorflow,
B are both of shape
(m, n, r),
C is a binary tensor of shape
(m, n, 1).
I want to select elements from either A or B based on the value of
C. The obvious tool is
tf.select, however that does not have broadcasting semantics, so I need to first explicitly broadcast
C to the same shape as A and B.
This would be my first attempt at how to do this, but it doesn't like me mixing a tensor (
tf.shape(A)) into the shape list.
import tensorflow as tf
A = tf.random_normal([20, 100, 10])
B = tf.random_normal([20, 100, 10])
C = tf.random_normal([20, 100, 1])
C = tf.greater_equal(C, tf.zeros_like(C))
C = tf.tile(C, [1,1,tf.shape(A)])
D = tf.select(C, A, B)
What's the correct approach here?
Your solution is very close to working. You should replace the line:
C = tf.tile(C, [1,1,tf.shape(C)])
...with the following:
C = tf.tile(C, tf.pack([1, 1, tf.shape(A)]))
(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 (
tf.shape(C)) to a tensor. Since each element is a scalar, the result will be a vector.)