I finally figured out how to do that using Tensorflow.
The idea is to define a placeholder as the boolean mask and then use numpy to pass a boolean matrix to the boolean mask in the runtime. I share my code below:
import tensorflow as tf
import numpy as np
# The matrix has size n-by-n
n = 3
# define a boolean mask as a placeholder
mask = tf.placeholder(tf.bool, shape=(n, n))
# A is the matrix
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
npmask = np.triu(np.ones((n, n), dtype=np.bool_), 1)
A_upper_triangular = tf.boolean_mask(A, mask)
print(sess.run(A_upper_triangular, feed_dict={mask: npmask}))
My Python version is 3.6 and my Tensorflow version is 0.12.0rc1. The output of the above code is
[2, 3, 6]
This method can be further generalized. We can use numpy to construct any kind of mask and then pass the mask to the Tensorflow to extract the part of the tensor of interest.