How To Construct Square Of Pairwise Difference From A Vector In Tensorflow?
I have a 1D vector having N dimension in TensorFlow, how to construct sum of a pairwise squared difference? Example Input Vector [1,2,3] Output 6 Computed As (1-2)^2+(1-3)^2+(2-3)^
Solution 1:
For pair-wise difference, subtract the input
and the transpose of input
and take only the upper triangular part, like:
pair_diff = tf.matrix_band_part(a[...,None] -
tf.transpose(a[...,None]), 0, -1)
Then you can square and sum the differences.
Code:
a = tf.constant([1,2,3])
pair_diff = tf.matrix_band_part(a[...,None] -
tf.transpose(a[...,None]), 0, -1)
output = tf.reduce_sum(tf.square(pair_diff))
with tf.Session() as sess:
print(sess.run(output))
# 6
Post a Comment for "How To Construct Square Of Pairwise Difference From A Vector In Tensorflow?"