'Convert tensor of (row, column) coordinates to boolean mask in TensorFlow

I have an array of 2D coordinates, from which I need to obtain a boolean mask with a known shape, where elements whose index is in the coordinates array is True.

For example, if I had an indices tensor which contains [[0, 0], [1, 1], [2, 2], [0, 1], [1, 2]] and a given shape of (5, 5) I need to get a matrix that is like so:

[[ True,  True, False, False, False],
 [False,  True,  True, False, False],
 [False, False,  True, False, False],
 [False, False, False, False, False],
 [False, False, False, False, False]]

In Numpy I'd do it like so:

idx = np.array([[0, 0], [1, 1], [2, 2], [0, 1], [1, 2]])
bool_mat = np.zeros(shape=(5, 5), dtype=np.bool)
bool_mat[idx[:, 0], idx[:, 1]] = True

However, in TensorFlow you can't assign tensor items like this.

How can I express an equivalent computation in TensorFlow?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source