Add a simple Categorical space

This commit is contained in:
D-X-Y
2021-03-17 12:48:43 +00:00
parent 95e304495f
commit d09cb2fe65
5 changed files with 97 additions and 2 deletions

View File

@@ -1 +1,7 @@
#
#####################################################
# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2021.01 #
#####################################################
# Define complex searc space for AutoDL #
#####################################################
from .basic_space import Categorical

33
lib/spaces/basic_space.py Normal file
View File

@@ -0,0 +1,33 @@
#####################################################
# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2021.01 #
#####################################################
import abc
import random
class Space(metaclass=abc.ABCMeta):
@abc.abstractmethod
def random(self):
raise NotImplementedError
@abc.abstractmethod
def __repr__(self):
raise NotImplementedError
class Categorical(Space):
def __init__(self, *data):
self._candidates = [*data]
def __getitem__(self, index):
return self._candidates[index]
def __len__(self):
return len(self._candidates)
def __repr__(self):
return "{name:}(candidates={cs:})".format(name=self.__class__.__name__, cs=self._candidates)
def random(self):
return random.choice(self._candidates)