|
1 | 1 | from metric_learn.oasis import OASIS
|
2 | 2 | import numpy as np
|
3 | 3 | from numpy.testing import assert_array_almost_equal
|
| 4 | +from timeit import default_timer as timer |
4 | 5 |
|
5 | 6 |
|
6 | 7 | def test_toy_distance():
|
@@ -41,4 +42,52 @@ def test_toy_distance():
|
41 | 42 | assert_array_almost_equal(dists, [14, 14])
|
42 | 43 |
|
43 | 44 |
|
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