src.slune.base

  1import abc 
  2
  3class BaseSearcher(metaclass=abc.ABCMeta):
  4    """ Base class for all Searchers. 
  5    
  6    This must be subclassed to create different Searcher classes.
  7    Please name your searcher class Searcher<SearcherName>
  8    Outlines a protocol for creating a search space and creating configurations from it.
  9    Methods document what they should do once implemented. 
 10
 11    """
 12    @abc.abstractmethod
 13    def __init__(self, *args, **kwargs):
 14        """ Initialises the searcher. """
 15
 16        pass
 17    
 18    @abc.abstractmethod
 19    def __len__(self, *args, **kwargs):
 20        """ Returns the number of configurations defined by the search space of the searcher. """
 21
 22        pass
 23    
 24    @abc.abstractmethod
 25    def next_tune(self, *args, **kwargs):
 26        """ Returns the next configuration to try. """
 27
 28        pass
 29
 30    @abc.abstractmethod
 31    def check_existing_runs(self, *args, **kwargs):
 32        """ Used to tell searcher to check if there are existing runs in storage.
 33
 34        If there are existing runs, the searcher should skip them 
 35        based on the number of runs we would like for each job.
 36        This may require a 'runs' attribute to be set in the searcher.
 37        It will probably also require access to a Saver object,
 38        so we can use it's saving protocol to check if there are existing runs.
 39        In this case is advised that this function takes a Saver object as an argument,
 40        and that the searcher is initialized with a 'runs' attribute.
 41
 42        """
 43
 44        pass
 45
 46    def __iter__(self):
 47        """ Makes the searcher iterable, so we can use it in a for loop.
 48        
 49        Feel free to override this method if needed.
 50
 51        """
 52
 53        return self
 54    
 55    def __next__(self):
 56        """ Makes the searcher iterable, so we can use it in a for loop.
 57
 58        Feel free to override this method if needed.
 59
 60        """
 61
 62        try:
 63            return self.next_tune()
 64        except:
 65            raise StopIteration
 66
 67class BaseLogger(metaclass=abc.ABCMeta):
 68    """ Base class for all Loggers. 
 69    
 70    This must be subclassed to implement different Logger classes.
 71    Please name your logger class Logger<LoggerName>.
 72    Outlines a protocol for logging metrics and reading from the logs.
 73    Methods document what they should do once implemented. 
 74
 75    """
 76    @abc.abstractmethod
 77    def __init__(self, *args, **kwargs):
 78        """ Initialises the logger. """
 79
 80        pass
 81    
 82    @abc.abstractmethod
 83    def log(self, *args, **kwargs):
 84        """ Logs the metric/s for the current hyperparameter configuration.
 85
 86        Should store metrics in some way so we can later save it using a Saver.
 87
 88        """
 89
 90        pass
 91    
 92    @abc.abstractmethod
 93    def read_log(self, *args, **kwargs):
 94        """ Returns value of a metric from the log based on a selection criteria. """
 95
 96        pass
 97
 98class BaseSaver(metaclass=abc.ABCMeta):
 99    """ Base class for all savers. 
100    
101    This must be subclassed to implement different Saver classes.
102    Please name your saver class Saver<SaverName>.
103    Outlines a protocol for saving and reading results to/from storage.
104    Methods document what they should do once implemented. 
105
106    """
107
108    @abc.abstractmethod
109    def __init__(self, logger_instance: BaseLogger, *args, **kwargs):
110        """ Initialises the saver.
111
112        Assigns the logger instance to self.logger and makes its methods accessible through self.log and self.read_log.
113
114        Args:
115            - logger_instance (BaseLogger): Instance of a logger class that inherits from BaseLogger.
116        
117        """
118
119        # Given a class that inherits from BaseLogger we make it accessible through self.logger and make its methods accessible through self.log and self.read_log
120        self.logger = logger_instance
121        self.log = self.logger.log
122        self.read_log = self.logger.read_log
123
124    @abc.abstractmethod
125    def save_collated(self, *args, **kwargs):
126        """ Saves the current results in logger to storage. """
127
128        pass
129    
130    @abc.abstractmethod
131    def read(self, *args, **kwargs):
132        """ Reads results from storage. """
133
134        pass
135
136    @abc.abstractmethod
137    def exists(self, *args, **kwargs):
138        """ Checks if results already exist in storage.
139         
140        Should return integer indicating the number of runs that exist in storage for the given parameters. 
141
142        """
143
144        pass
class BaseSearcher:
 4class BaseSearcher(metaclass=abc.ABCMeta):
 5    """ Base class for all Searchers. 
 6    
 7    This must be subclassed to create different Searcher classes.
 8    Please name your searcher class Searcher<SearcherName>
 9    Outlines a protocol for creating a search space and creating configurations from it.
10    Methods document what they should do once implemented. 
11
12    """
13    @abc.abstractmethod
14    def __init__(self, *args, **kwargs):
15        """ Initialises the searcher. """
16
17        pass
18    
19    @abc.abstractmethod
20    def __len__(self, *args, **kwargs):
21        """ Returns the number of configurations defined by the search space of the searcher. """
22
23        pass
24    
25    @abc.abstractmethod
26    def next_tune(self, *args, **kwargs):
27        """ Returns the next configuration to try. """
28
29        pass
30
31    @abc.abstractmethod
32    def check_existing_runs(self, *args, **kwargs):
33        """ Used to tell searcher to check if there are existing runs in storage.
34
35        If there are existing runs, the searcher should skip them 
36        based on the number of runs we would like for each job.
37        This may require a 'runs' attribute to be set in the searcher.
38        It will probably also require access to a Saver object,
39        so we can use it's saving protocol to check if there are existing runs.
40        In this case is advised that this function takes a Saver object as an argument,
41        and that the searcher is initialized with a 'runs' attribute.
42
43        """
44
45        pass
46
47    def __iter__(self):
48        """ Makes the searcher iterable, so we can use it in a for loop.
49        
50        Feel free to override this method if needed.
51
52        """
53
54        return self
55    
56    def __next__(self):
57        """ Makes the searcher iterable, so we can use it in a for loop.
58
59        Feel free to override this method if needed.
60
61        """
62
63        try:
64            return self.next_tune()
65        except:
66            raise StopIteration

Base class for all Searchers.

This must be subclassed to create different Searcher classes. Please name your searcher class Searcher Outlines a protocol for creating a search space and creating configurations from it. Methods document what they should do once implemented.

@abc.abstractmethod
BaseSearcher(*args, **kwargs)
13    @abc.abstractmethod
14    def __init__(self, *args, **kwargs):
15        """ Initialises the searcher. """
16
17        pass

Initialises the searcher.

@abc.abstractmethod
def next_tune(self, *args, **kwargs):
25    @abc.abstractmethod
26    def next_tune(self, *args, **kwargs):
27        """ Returns the next configuration to try. """
28
29        pass

Returns the next configuration to try.

@abc.abstractmethod
def check_existing_runs(self, *args, **kwargs):
31    @abc.abstractmethod
32    def check_existing_runs(self, *args, **kwargs):
33        """ Used to tell searcher to check if there are existing runs in storage.
34
35        If there are existing runs, the searcher should skip them 
36        based on the number of runs we would like for each job.
37        This may require a 'runs' attribute to be set in the searcher.
38        It will probably also require access to a Saver object,
39        so we can use it's saving protocol to check if there are existing runs.
40        In this case is advised that this function takes a Saver object as an argument,
41        and that the searcher is initialized with a 'runs' attribute.
42
43        """
44
45        pass

Used to tell searcher to check if there are existing runs in storage.

If there are existing runs, the searcher should skip them based on the number of runs we would like for each job. This may require a 'runs' attribute to be set in the searcher. It will probably also require access to a Saver object, so we can use it's saving protocol to check if there are existing runs. In this case is advised that this function takes a Saver object as an argument, and that the searcher is initialized with a 'runs' attribute.

class BaseLogger:
68class BaseLogger(metaclass=abc.ABCMeta):
69    """ Base class for all Loggers. 
70    
71    This must be subclassed to implement different Logger classes.
72    Please name your logger class Logger<LoggerName>.
73    Outlines a protocol for logging metrics and reading from the logs.
74    Methods document what they should do once implemented. 
75
76    """
77    @abc.abstractmethod
78    def __init__(self, *args, **kwargs):
79        """ Initialises the logger. """
80
81        pass
82    
83    @abc.abstractmethod
84    def log(self, *args, **kwargs):
85        """ Logs the metric/s for the current hyperparameter configuration.
86
87        Should store metrics in some way so we can later save it using a Saver.
88
89        """
90
91        pass
92    
93    @abc.abstractmethod
94    def read_log(self, *args, **kwargs):
95        """ Returns value of a metric from the log based on a selection criteria. """
96
97        pass

Base class for all Loggers.

This must be subclassed to implement different Logger classes. Please name your logger class Logger. Outlines a protocol for logging metrics and reading from the logs. Methods document what they should do once implemented.

@abc.abstractmethod
BaseLogger(*args, **kwargs)
77    @abc.abstractmethod
78    def __init__(self, *args, **kwargs):
79        """ Initialises the logger. """
80
81        pass

Initialises the logger.

@abc.abstractmethod
def log(self, *args, **kwargs):
83    @abc.abstractmethod
84    def log(self, *args, **kwargs):
85        """ Logs the metric/s for the current hyperparameter configuration.
86
87        Should store metrics in some way so we can later save it using a Saver.
88
89        """
90
91        pass

Logs the metric/s for the current hyperparameter configuration.

Should store metrics in some way so we can later save it using a Saver.

@abc.abstractmethod
def read_log(self, *args, **kwargs):
93    @abc.abstractmethod
94    def read_log(self, *args, **kwargs):
95        """ Returns value of a metric from the log based on a selection criteria. """
96
97        pass

Returns value of a metric from the log based on a selection criteria.

class BaseSaver:
 99class BaseSaver(metaclass=abc.ABCMeta):
100    """ Base class for all savers. 
101    
102    This must be subclassed to implement different Saver classes.
103    Please name your saver class Saver<SaverName>.
104    Outlines a protocol for saving and reading results to/from storage.
105    Methods document what they should do once implemented. 
106
107    """
108
109    @abc.abstractmethod
110    def __init__(self, logger_instance: BaseLogger, *args, **kwargs):
111        """ Initialises the saver.
112
113        Assigns the logger instance to self.logger and makes its methods accessible through self.log and self.read_log.
114
115        Args:
116            - logger_instance (BaseLogger): Instance of a logger class that inherits from BaseLogger.
117        
118        """
119
120        # Given a class that inherits from BaseLogger we make it accessible through self.logger and make its methods accessible through self.log and self.read_log
121        self.logger = logger_instance
122        self.log = self.logger.log
123        self.read_log = self.logger.read_log
124
125    @abc.abstractmethod
126    def save_collated(self, *args, **kwargs):
127        """ Saves the current results in logger to storage. """
128
129        pass
130    
131    @abc.abstractmethod
132    def read(self, *args, **kwargs):
133        """ Reads results from storage. """
134
135        pass
136
137    @abc.abstractmethod
138    def exists(self, *args, **kwargs):
139        """ Checks if results already exist in storage.
140         
141        Should return integer indicating the number of runs that exist in storage for the given parameters. 
142
143        """
144
145        pass

Base class for all savers.

This must be subclassed to implement different Saver classes. Please name your saver class Saver. Outlines a protocol for saving and reading results to/from storage. Methods document what they should do once implemented.

@abc.abstractmethod
BaseSaver(logger_instance: BaseLogger, *args, **kwargs)
109    @abc.abstractmethod
110    def __init__(self, logger_instance: BaseLogger, *args, **kwargs):
111        """ Initialises the saver.
112
113        Assigns the logger instance to self.logger and makes its methods accessible through self.log and self.read_log.
114
115        Args:
116            - logger_instance (BaseLogger): Instance of a logger class that inherits from BaseLogger.
117        
118        """
119
120        # Given a class that inherits from BaseLogger we make it accessible through self.logger and make its methods accessible through self.log and self.read_log
121        self.logger = logger_instance
122        self.log = self.logger.log
123        self.read_log = self.logger.read_log

Initialises the saver.

Assigns the logger instance to self.logger and makes its methods accessible through self.log and self.read_log.

Arguments:
  • - logger_instance (BaseLogger): Instance of a logger class that inherits from BaseLogger.
logger
log
read_log
@abc.abstractmethod
def save_collated(self, *args, **kwargs):
125    @abc.abstractmethod
126    def save_collated(self, *args, **kwargs):
127        """ Saves the current results in logger to storage. """
128
129        pass

Saves the current results in logger to storage.

@abc.abstractmethod
def read(self, *args, **kwargs):
131    @abc.abstractmethod
132    def read(self, *args, **kwargs):
133        """ Reads results from storage. """
134
135        pass

Reads results from storage.

@abc.abstractmethod
def exists(self, *args, **kwargs):
137    @abc.abstractmethod
138    def exists(self, *args, **kwargs):
139        """ Checks if results already exist in storage.
140         
141        Should return integer indicating the number of runs that exist in storage for the given parameters. 
142
143        """
144
145        pass

Checks if results already exist in storage.

Should return integer indicating the number of runs that exist in storage for the given parameters.