2017-06-19 81 views
0

我有一個n * m張量,它基本上代表了n維歐氏空間中的m個點。我想要計算每個連續點之間的兩兩歐式距離。Tensorflow - 矩陣中歐幾里德距離的點

也就是說,如果我的列向量的點a,b,c等,我想計算EUC(A,B),EUC(B,C)等

結果將是具有每對歐式距離的m-1長度1D張量。

任何人都知道誰可以在TensorFlow中執行此操作?

回答

0

好的,我想出了一些可行的方法。不過,讓我知道是否有人有更好的解決方案。

def pairwise_euclidean_distance (input_layer): 
    original_size = input_layer.get_shape().as_list() 

    subtrahend = tf.pad(input_layer, [[0, 0], [1, 0], [0, 0], [0, 0]]) 
    subtrahend = tf.slice(subtrahend, [0, 0, 0, 0], original_size) 

    distance = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(input_layer, subtrahend)), axis=[2,3])) 

    return distance 
相關問題