繁体   English   中英

np.linalg.norm(ax) 和 np.linalg.norm(a) - np.linalg.norm(x) 有什么区别?

[英]what's the difference between np.linalg.norm(a-x) and np.linalg.norm(a) - np.linalg.norm(x)?

我正在尝试从图像中提取特征,然后将这些特征与来自数千张图像的一系列特征进行比较。

image = Image.open('C:/Users/Timmi/Desktop/test_image.jpg')

query = fe.extract(image)
print(np.shape(query))
print(np.linalg.norm(query))

dists = np.linalg.norm(features - query, axis=1)
print(dists)
print(np.shape(features))
dists2 = (np.linalg.norm(features, axis=1)) - (np.linalg.norm(query, 0))
print(dists2)

ids = np.argsort(dists)[:3]
scores = [(dists[id], img_paths[id]) for id in ids]
print(scores)

这是打印出来的

(4096,)
0.99999994
[0.96851873 1.0867099  1.054868   ... 1.2182273  1.2591194  1.2556202 ]
(31303, 4096)
[-1158. -1158. -1158. ... -1158. -1158. -1158.]
[(0.0, WindowsPath('static/img/image1.jpg')), (0.0, WindowsPath('static/img/image2.jpg')), (0.08249867, WindowsPath('static/img/image3.jpg'))]

我试图找到这两行之间的区别:

dists1 = np.linalg.norm(features - query, axis=1)

dists2 = (np.linalg.norm(features, axis=1)) - np.array(np.linalg.norm(query, 0))

对于向量x = (x1, x2, ..., xn) ,其欧几里得范数(在 numpy 中linalg.norm )定义为:

sqrt(x1 ** 2 + x2 ** 2 + ... + xn ** 2)

所以特征的范数features - query向量是:

sqrt((f1 - q1) ** 2 + (f2 - q2) ** 2 + ... + (fn - qn) ** 2)

features范数减去query范数是:

sqrt(f1 ** 2 + f2 ** 2 + ... + fn ** 2) - sqrt(q1 ** 2 + q2 ** 2 + ... + qn ** 2)

这是不同的数量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM