test_evolution_tuner.py 1.82 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
3
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

Lee's avatar
Lee committed
4
5
6
7
8
9
10
11
"""
test_evolution_tuner.py
"""

import numpy as np

from unittest import TestCase, main

RayMeng8's avatar
RayMeng8 committed
12
from nni.utils import json2space, json2parameter
Lee's avatar
Lee committed
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
56
57
58


class EvolutionTunerTestCase(TestCase):
    def test_json2space(self):
        """test for json2space
        """
        json_search_space = {
            "optimizer": {
                "_type": "choice",
                "_value": ["Adam", "SGD"]
            },
            "learning_rate": {
                "_type": "choice",
                "_value": [0.0001, 0.001, 0.002, 0.005, 0.01]
            }
        }
        search_space_instance = json2space(json_search_space)
        self.assertIn('root[optimizer]-choice', search_space_instance)
        self.assertIn('root[learning_rate]-choice', search_space_instance)

    def test_json2parameter(self):
        """test for json2parameter
        """
        json_search_space = {
            "optimizer":{
                "_type":"choice","_value":["Adam", "SGD"]
            },
            "learning_rate":{
                "_type":"choice",
                "_value":[0.0001, 0.001, 0.002, 0.005, 0.01]
            }
        }
        space = json2space(json_search_space)
        random_state = np.random.RandomState()
        is_rand = dict()
        for item in space:
            is_rand[item] = True
        search_space_instance = json2parameter(json_search_space, is_rand, random_state)
        self.assertIn(search_space_instance["optimizer"]["_index"], range(2))
        self.assertIn(search_space_instance["optimizer"]["_value"], ["Adam", "SGD"])
        self.assertIn(search_space_instance["learning_rate"]["_index"], range(5))
        self.assertIn(search_space_instance["learning_rate"]["_value"], [0.0001, 0.001, 0.002, 0.005, 0.01])


if __name__ == '__main__':
    main()