[英]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.