I finally figured out how to do this using Tensorflow.
The idea is to define a placeholder as a Boolean mask, and then use numpy to pass the Boolean matrix to the Boolean mask at runtime. I am sharing my code below:
import tensorflow as tf
import numpy as np
n = 3
mask = tf.placeholder(tf.bool, shape=(n, n))
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. Output above code
[2, 3, 6]
. numpy Tensorflow, .