forked from cnzeki/DeepLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistance.py
More file actions
36 lines (25 loc) · 899 Bytes
/
Copy pathdistance.py
File metadata and controls
36 lines (25 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# -*- coding:utf-8 -*-
import math
import numpy as np
def cosine_similarity_slow(v1, v2):
# compute cosine similarity of v1 to v2: (v1 dot v2)/{||v1||*||v2||)
sumxx, sumxy, sumyy = 0, 0, 0
for i in range(len(v1)):
x = v1[i]
y = v2[i]
sumxx += x * x
sumyy += y * y
sumxy += x * y
return sumxy / math.sqrt(sumxx * sumyy)
def cosine_similarity(v1, v2):
cosV12 = np.dot(v1, v2)/(np.linalg.norm(v1)*np.linalg.norm(v2))
return cosV12
def cosine_distance(v1, v2):
return 1 - cosine_similarity(v1, v2)
def L2_distance(v1, v2):
return np.sqrt(np.sum(np.square(v1 - v2)))
def SSD_distance(v1, v2):
return np.sum(np.square(v1 - v2))
def get_distance(dist_type):
loss_map = {'cosine':cosine_distance, 'L2':L2_distance, 'SSD':SSD_distance}
return loss_map[dist_type]