Binary mask in Tensorflow

Currently, Tensorflow does not support numpy-like assignment.

Here is a couple of workarounds:

tf.Variable

tf.Tensor cannot be changed, but tf.Variable can.

a = tf.constant([[1,2,3,4,5],[6,7,8,9,10]])

mask = tf.Variable(tf.ones_like(a, dtype=tf.int32))
mask = mask[0,1::2]
mask = tf.assign(mask, tf.zeros_like(mask))
# mask = [[1,0,1,0,1],[1,1,1,1,1]]

tf.InteractiveSession()
tf.global_variables_initializer().run()
print(mask.eval())

tf.sparse_to_dense()

indices = tf.range(1, 5, 2)
indices = tf.stack([tf.zeros_like(indices), indices], axis=1)
# indices = [[0,1],[0,3]]
mask = tf.sparse_to_dense(indices, a.shape, sparse_values=0, default_value=1)
# mask = [[1,0,1,0,1],[1,1,1,1,1]]

tf.InteractiveSession()
print(mask.eval())