Ich habe ein KNN Klassifikations-Projekt, das zum Vergleich die euklidische Distanz mit Tensorflow berechnen muss.Euklidischer Abstand in Tensorflow, Matrix umwandeln
Der ursprüngliche Code ohne tensorflow ist wie folgt:
def euclidean_distance(self,x1, x2):
distance = 0.0
for i in range(len(x1)):
distance += pow(x1[i] - x2[i], 2)
print(distance)
return math.sqrt(distance)
und mit tensorflow ist wie folgt:
distance = 0.0
for i in range(len(x1)):
distance = tf.negative(tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(x1, x2)))))
return distance
Ist das richtig? Aus diesem Code wurde Distanz zum Tensor, und ich brauche eine Methode, um diesen Tensor in eine normale Matrix umzuwandeln.
Jede Hilfe wird geschätzt, danke!
wenn im Versuch Abstand zu drucken, wie es Tensor war ("Sqrt_2457: 0", die Form =(), dtype = float32) Tensor ("Sqrt_2458: 0", shape =(), dtype = float32) aber nichts passiert, wenn ich versuche, nd_distance wie deinen Code zu drucken, irgendeinen Vorschlag? – Elucist
Was sind die Werte von x1 und x2? Sind sie auch Tensoren? – Mitiku
nein, es ist Matrix, so https://s1.postimg.org/o2x1vdypr/postimage.png es ist Feature-Vektor-Matrix, nach tf-idf – Elucist