File size: 1,688 Bytes
9d61c9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import unittest

import torch

from training.loss.utils import sample_wise_min_max


class TestSampleWiseMinMax(unittest.TestCase):
    def test_sample_wise_min_max(self):
        # Test case 1: Test with batch size 1
        x = torch.tensor([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]])
        normalized = sample_wise_min_max(x)
        expected = torch.tensor(
            [[[0.0, 0.125, 0.25], [0.375, 0.5, 0.625], [0.75, 0.875, 1.0]]],
        )

        self.assertTrue(torch.allclose(normalized, expected))

        # Test case 2: Test with batch size 2
        x = torch.tensor(
            [
                [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                [[2, 4, 6], [8, 10, 12], [14, 16, 18]],
            ],
        )
        normalized = sample_wise_min_max(x)
        expected = torch.tensor(
            [
                [[0.0, 0.125, 0.25], [0.375, 0.5, 0.625], [0.75, 0.875, 1.0]],
                [[0.0, 0.125, 0.25], [0.375, 0.5, 0.625], [0.75, 0.875, 1.0]],
            ],
        )

        self.assertTrue(torch.allclose(normalized, expected))

        # Test case 3: Test with negative values
        x = torch.tensor(
            [
                [[-1, -2, -3], [-4, -5, -6], [-7, -8, -9]],
                [[-2, -4, -6], [-8, -10, -12], [-14, -16, -18]],
            ],
        )
        normalized = sample_wise_min_max(x)
        expected = torch.tensor(
            [
                [[1.0, 0.875, 0.75], [0.625, 0.5, 0.375], [0.25, 0.125, 0.0]],
                [[1.0, 0.875, 0.75], [0.625, 0.5, 0.375], [0.25, 0.125, 0.0]],
            ],
        )
        self.assertTrue(torch.allclose(normalized, expected))


if __name__ == "__main__":
    unittest.main()