|
| 1 | +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import random |
| 16 | +import unittest |
| 17 | + |
| 18 | +import numpy as np |
| 19 | + |
| 20 | +from paddlenlp.metrics import MRR |
| 21 | +from tests.common_test import CommonTest |
| 22 | + |
| 23 | + |
| 24 | +class TestMRR(CommonTest): |
| 25 | + def setUp(self): |
| 26 | + self.distance = "cosine" |
| 27 | + self.mrr = MRR(distance=self.distance) |
| 28 | + self.label_num = 10 |
| 29 | + self.label_shape = (20,) |
| 30 | + self.embedding_shape = (20, 128) |
| 31 | + |
| 32 | + def get_random_case(self): |
| 33 | + labels = np.random.randint(0, self.label_num, size=self.label_shape).astype("int64") |
| 34 | + embeddings = np.random.uniform(0.1, 1.0, self.embedding_shape).astype("float64") |
| 35 | + all_distance = ["cityblock", "cosine", "euclidean", "l1", "l2", "manhattan"] |
| 36 | + distance = random.choice(all_distance) |
| 37 | + return labels, embeddings, distance, all_distance |
| 38 | + |
| 39 | + def get_true_mrr_case(self): |
| 40 | + labels = np.array([1, 2, 1]).astype("int64") |
| 41 | + embeddings = np.array( |
| 42 | + [ |
| 43 | + # cosine similarity: 1,2 => 0.991; 1,3=>0.851; 2,3=>0.912 |
| 44 | + [1.0, 2.0, 3.0], |
| 45 | + [1.0, 2.0, 4.0], |
| 46 | + [1.0, 100.0, 1000.0], |
| 47 | + ] |
| 48 | + ) |
| 49 | + distance = "cosine" |
| 50 | + true_mrr = (1.0 / 2 + 0 + 1.0 / 2) / 3 |
| 51 | + return labels, embeddings, distance, true_mrr |
| 52 | + |
| 53 | + def test_reset_distance(self): |
| 54 | + _, _, distance, _ = self.get_random_case() |
| 55 | + self.mrr.reset_distance(distance) |
| 56 | + self.check_output_equal(self.mrr.distance, distance) |
| 57 | + |
| 58 | + def test_compute_matrix_mrr(self): |
| 59 | + step = 100 |
| 60 | + for i in range(step): |
| 61 | + labels, embeddings, distance, _ = self.get_random_case() |
| 62 | + self.mrr.reset_distance(distance) |
| 63 | + self.mrr.compute_matrix_mrr(labels, embeddings) |
| 64 | + |
| 65 | + def test_compute_true_mrr(self): |
| 66 | + labels, embeddings, distance, true_mrr = self.get_true_mrr_case() |
| 67 | + self.mrr.reset_distance(distance) |
| 68 | + mrr = self.mrr.compute_matrix_mrr(labels, embeddings) |
| 69 | + self.check_output_equal(mrr, true_mrr) |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + unittest.main() |
0 commit comments