File size: 1,356 Bytes
079c32c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import easydict
import numpy
import pytest

from ding.utils.normalizer_helper import DatasetNormalizer


# TODO(nyz): fix unittest bugs
@pytest.mark.tmp
class TestNormalizerHelper:

    def test_normalizer(self):
        x = numpy.random.randn(10)
        mean = x.mean()
        std = x.std()
        mins = x.min()
        maxs = x.max()
        normalizer = DatasetNormalizer({'test': x}, 'GaussianNormalizer', 10)
        test = numpy.random.randn(1)
        normal_test = normalizer.normalize(test, 'test')
        unnormal_test = normalizer.unnormalize(normal_test, 'test')
        assert unnormal_test == test
        assert normal_test == (test - mean) / std

        normalizer = DatasetNormalizer({'test': x}, 'LimitsNormalizer', 10)
        test = numpy.random.randn(1)
        normal_test1 = (test - mins) / (maxs - mins)
        normal_test1 = 2 * normal_test1 - 1
        normal_test = normalizer.normalize(test, 'test')
        unnormal_test = normalizer.unnormalize(normal_test, 'test')
        assert unnormal_test == test
        assert normal_test == normal_test1

        normalizer = DatasetNormalizer({'test': x}, 'CDFNormalizer', 10)
        test = numpy.random.randn(1)
        normal_test = normalizer.normalize(test, 'test')
        unnormal_test = normalizer.unnormalize(normal_test, 'test')
        assert unnormal_test == test