Skip to content

Commit ee5c5ee

Browse files
author
mvargas33
committed
Add performance test to choose between two methods for bilinear calc
1 parent dbe2a7a commit ee5c5ee

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

test_bilinear.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from metric_learn.oasis import OASIS
22
import numpy as np
33
from numpy.testing import assert_array_almost_equal
4+
from timeit import default_timer as timer
45

56

67
def test_toy_distance():
@@ -41,4 +42,52 @@ def test_toy_distance():
4142
assert_array_almost_equal(dists, [14, 14])
4243

4344

44-
test_toy_distance()
45+
def test_bilinar_properties():
46+
d = 100
47+
48+
u = np.random.rand(d)
49+
v = np.random.rand(d)
50+
51+
mixin = OASIS()
52+
mixin.fit([u, v], [0, 0]) # Dummy fit
53+
54+
dist1 = mixin.score_pairs([[u, u], [v, v], [u, v], [v, u]])
55+
56+
print(dist1)
57+
58+
59+
def test_performace():
60+
61+
features = int(1e4)
62+
samples = int(1e3)
63+
64+
a = [np.random.rand(features) for i in range(samples)]
65+
b = [np.random.rand(features) for i in range(samples)]
66+
pairs = np.array([(aa, bb) for aa, bb in zip(a, b)])
67+
components = np.identity(features)
68+
69+
def op_1(pairs, components):
70+
return np.diagonal(np.dot(
71+
np.dot(pairs[:, 0, :], components),
72+
pairs[:, 1, :].T))
73+
74+
def op_2(pairs, components):
75+
return np.array([np.dot(np.dot(u.T, components), v)
76+
for u, v in zip(pairs[:, 0, :], pairs[:, 1, :])])
77+
78+
# Test first method
79+
start = timer()
80+
op_1(pairs, components)
81+
end = timer()
82+
print(f'First method took {end - start}')
83+
84+
# Test second method
85+
start = timer()
86+
op_2(pairs, components)
87+
end = timer()
88+
print(f'Second method took {end - start}')
89+
90+
91+
# test_toy_distance()
92+
# test_bilinar_properties()
93+
test_performace()

0 commit comments

Comments
 (0)