# How to explicitly broadcast a tensor to match another's shape in tensorflow?

I have three tensors, `A, B and C` in tensorflow, `A` and `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)[2]`) into the shape list.

``import tensorflow as tfA = 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)[2]])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)[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.)