tuner.py 4.71 KB
Newer Older
Deshui Yu's avatar
Deshui Yu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
# associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
# OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ==================================================================================================


import logging

xuehui's avatar
xuehui committed
24
import nni
25
from .recoverable import Recoverable
Deshui Yu's avatar
Deshui Yu committed
26
27
28
29

_logger = logging.getLogger(__name__)


30
class Tuner(Recoverable):
Deshui Yu's avatar
Deshui Yu committed
31
32
33
34
35
36
37
38
39
40
41
42
43
    # pylint: disable=no-self-use,unused-argument

    def generate_parameters(self, parameter_id):
        """Returns a set of trial (hyper-)parameters, as a serializable object.
        User code must override either this function or 'generate_multiple_parameters()'.
        parameter_id: int
        """
        raise NotImplementedError('Tuner: generate_parameters not implemented')

    def generate_multiple_parameters(self, parameter_id_list):
        """Returns multiple sets of trial (hyper-)parameters, as iterable of serializable objects.
        Call 'generate_parameters()' by 'count' times by default.
        User code must override either this function or 'generate_parameters()'.
44
45
        If there's no more trial, user should raise nni.NoMoreTrialError exception in generate_parameters().
        If so, this function will only return sets of trial (hyper-)parameters that have already been collected.
Deshui Yu's avatar
Deshui Yu committed
46
47
        parameter_id_list: list of int
        """
xuehui's avatar
xuehui committed
48
49
        result = []
        for parameter_id in parameter_id_list:
xuehui's avatar
xuehui committed
50
            try:
Yan Ni's avatar
Yan Ni committed
51
                _logger.debug("generating param for {}".format(parameter_id))
xuehui's avatar
xuehui committed
52
53
54
55
                res = self.generate_parameters(parameter_id)
            except nni.NoMoreTrialError:
                return result
            result.append(res)
xuehui's avatar
xuehui committed
56
        return result
Deshui Yu's avatar
Deshui Yu committed
57

58
    def receive_trial_result(self, parameter_id, parameters, value):
Deshui Yu's avatar
Deshui Yu committed
59
60
61
62
63
64
65
        """Invoked when a trial reports its final result. Must override.
        parameter_id: int
        parameters: object created by 'generate_parameters()'
        reward: object reported by trial
        """
        raise NotImplementedError('Tuner: receive_trial_result not implemented')

66
    def receive_customized_trial_result(self, parameter_id, parameters, value):
Deshui Yu's avatar
Deshui Yu committed
67
68
69
        """Invoked when a trial added by WebUI reports its final result. Do nothing by default.
        parameter_id: int
        parameters: object created by user
70
        value: object reported by trial
Deshui Yu's avatar
Deshui Yu committed
71
72
73
74
75
76
77
78
79
        """
        _logger.info('Customized trial job %s ignored by tuner', parameter_id)

    def update_search_space(self, search_space):
        """Update the search space of tuner. Must override.
        search_space: JSON object
        """
        raise NotImplementedError('Tuner: update_search_space not implemented')

80
    def load_checkpoint(self):
Deshui Yu's avatar
Deshui Yu committed
81
82
83
        """Load the checkpoint of tuner.
        path: checkpoint directory for tuner
        """
84
85
        checkpoin_path = self.get_checkpoint_path()
        _logger.info('Load checkpoint ignored by tuner, checkpoint path: %s' % checkpoin_path)
Deshui Yu's avatar
Deshui Yu committed
86

87
    def save_checkpoint(self):
Deshui Yu's avatar
Deshui Yu committed
88
89
90
        """Save the checkpoint of tuner.
        path: checkpoint directory for tuner
        """
91
92
        checkpoin_path = self.get_checkpoint_path()
        _logger.info('Save checkpoint ignored by tuner, checkpoint path: %s' % checkpoin_path)
Deshui Yu's avatar
Deshui Yu committed
93

94
95
    def _on_exit(self):
        pass
Deshui Yu's avatar
Deshui Yu committed
96

97
98
    def _on_error(self):
        pass
99
100
101
102
103
104
105
106
107

    def extract_scalar_reward(self, value, scalar_key='default'):
        if isinstance(value, float) or isinstance(value, int):
            reward = value
        elif isinstance(value, dict) and scalar_key in value and isinstance(value[scalar_key], (float, int)):
            reward = value[scalar_key]
        else:
            raise RuntimeError('Incorrect final result: the final result for %s should be float/int, or a dict which has a key named "default" whose value is float/int.' % str(self.__class__)) 
        return reward