作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个knn分类项目,需要用tensorflow计算欧几里得距离,以便比较。
没有tensorflow的原代码是这样的:
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)
在 tensorflow 中是这样的:
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
这样对吗?由于该代码距离变成了张量,我需要一种将该张量转换为普通矩阵的方法。
感谢任何帮助,谢谢!
最佳答案
为了得到 nd array(matrix) 你需要像 blow 一样运行图形
session=tf.Session()
nd_distance=session.run(distance)
您必须将代码更改为
......
......
distance = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(x1, x2))))
nd_distance=session.run(distance)
print (nd_distance)
return nd_distance
我不认为需要 tf.negative
函数和 for loop
关于python-3.x - Tensorflow中的欧氏距离,转换矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45454354/
我是一名优秀的程序员,十分优秀!