Skip to content

Encoding

neps.optimizers.bayesian_optimization.kernels.encoding #

NASBOTDistance #

NASBOTDistance(
    node_name="op_name",
    include_op_list=None,
    exclude_op_list=None,
    lengthscale=3.0,
    normalize=True,
    max_size=None,
    **kwargs
)

Bases: GraphKernels

NASBOT OATMANN distance according to BANANAS paper

Source code in neps/optimizers/bayesian_optimization/kernels/encoding.py
def __init__(
    self,
    node_name="op_name",
    include_op_list=None,
    exclude_op_list=None,
    lengthscale=3.0,
    normalize=True,
    max_size=None,
    **kwargs,
):
    super().__init__(**kwargs)
    self.node_name = node_name
    self.include_op_list = include_op_list if include_op_list is not None else OPS
    self.exclude_op_list = exclude_op_list if exclude_op_list is not None else []
    self.normalize = normalize
    self.lengthscale = lengthscale
    self.max_size = max_size
    self._gram = None

forward_t #

forward_t(gr2, gr1: list = None)

Compute the derivative of the kernel function k(phi, phi) with respect to phi (the training point)

Source code in neps/optimizers/bayesian_optimization/kernels/graph_kernel.py
def forward_t(self, gr2, gr1: list = None):
    """
    Compute the derivative of the kernel function k(phi, phi*) with respect to phi* (the training point)
    """
    raise NotImplementedError(
        "The kernel gradient is not implemented for the graph kernel called!"
    )

PathDistance #

PathDistance(
    node_name="op_name",
    include_op_list=None,
    exclude_op_list=None,
    lengthscale=3.0,
    normalize=True,
    max_size=None,
    **kwargs
)

Bases: NASBOTDistance

Source code in neps/optimizers/bayesian_optimization/kernels/encoding.py
def __init__(
    self,
    node_name="op_name",
    include_op_list=None,
    exclude_op_list=None,
    lengthscale=3.0,
    normalize=True,
    max_size=None,
    **kwargs,
):
    super().__init__(**kwargs)
    self.node_name = node_name
    self.include_op_list = include_op_list if include_op_list is not None else OPS
    self.exclude_op_list = exclude_op_list if exclude_op_list is not None else []
    self.normalize = normalize
    self.lengthscale = lengthscale
    self.max_size = max_size
    self._gram = None

encode_paths #

encode_paths(g: Graph)

output one-hot encoding of paths

Source code in neps/optimizers/bayesian_optimization/kernels/encoding.py
def encode_paths(self, g: nx.Graph):
    """output one-hot encoding of paths"""
    if "~" in g.name:
        LONGEST_PATH_LENGTH = 3
        num_paths = sum(len(OPS_201) ** i for i in range(1, LONGEST_PATH_LENGTH + 1))
        path_indices = self.get_path_indices_201(g)
    elif "101" in g.name:
        num_paths = sum(len(OPS_EX) ** i for i in range(OP_SPOTS + 1))
        path_indices = self.get_path_indices(g)
    else:
        num_paths = sum(len(self.op_list) ** i for i in range(self.max_size - 1))
        path_indices = self.get_paths(g)
    path_encoding = np.zeros(num_paths)
    for index in path_indices:
        path_encoding[index] = 1
    return path_encoding

forward_t #

forward_t(gr2, gr1: list = None)

Compute the derivative of the kernel function k(phi, phi) with respect to phi (the training point)

Source code in neps/optimizers/bayesian_optimization/kernels/graph_kernel.py
def forward_t(self, gr2, gr1: list = None):
    """
    Compute the derivative of the kernel function k(phi, phi*) with respect to phi* (the training point)
    """
    raise NotImplementedError(
        "The kernel gradient is not implemented for the graph kernel called!"
    )

get_path_indices #

get_path_indices(g: Graph)

compute the index of each path There are 3^0 + ... + 3^5 paths total. (Paths can be length 0 to 5, and for each path, for each node, there are three choices for the operation.)

Source code in neps/optimizers/bayesian_optimization/kernels/encoding.py
def get_path_indices(self, g: nx.Graph):
    """
    compute the index of each path
    There are 3^0 + ... + 3^5 paths total.
    (Paths can be length 0 to 5, and for each path, for each node, there
    are three choices for the operation.)
    """
    paths = self.get_paths(g)
    mapping = {CONV3X3: 0, CONV1X1: 1, MAXPOOL3X3: 2}
    path_indices = []

    for path in paths:
        index = 0
        for i in range(NUM_VERTICES - 1):
            if i == len(path):
                path_indices.append(index)
                break
            else:
                index += len(OPS_EX) ** i * (mapping[path[i]] + 1)

    return tuple(path_indices)

get_path_indices_201 #

get_path_indices_201(g: Graph)

compute the index of each path

Source code in neps/optimizers/bayesian_optimization/kernels/encoding.py
def get_path_indices_201(self, g: nx.Graph):
    """
    compute the index of each path
    """
    paths = self.get_paths_201(g)
    path_indices = []
    NUM_OPS = len(OPS_201)
    for i, path in enumerate(paths):
        if i == 0:
            index = 0
        elif i in [1, 2]:
            index = NUM_OPS
        else:
            index = NUM_OPS + NUM_OPS**2
        for j, op in enumerate(path):
            index += OPS_201.index(op) * NUM_OPS**j
        path_indices.append(index)

    return tuple(path_indices)

get_paths #

get_paths(g: Graph)

return all paths from input to output

Source code in neps/optimizers/bayesian_optimization/kernels/encoding.py
def get_paths(self, g: nx.Graph):
    """
    return all paths from input to output
    """
    paths: list = []
    matrix = nx.to_numpy_array(g)
    ops: list = []
    for _, attr in g.nodes(data=True):
        ops.append(attr[self.node_name])
    for j in range(0, NUM_VERTICES):
        if matrix[0][j]:
            paths.append([[]])
        else:
            paths.append([])

    # create paths sequentially
    for i in range(1, NUM_VERTICES - 1):
        for j in range(1, NUM_VERTICES):
            if matrix[i][j]:
                for path in paths[i]:
                    paths[j].append([*path, ops[i]])
    return paths[-1]

get_paths_201 staticmethod #

get_paths_201(g: Graph)

return all paths from input to output

Source code in neps/optimizers/bayesian_optimization/kernels/encoding.py
@staticmethod
def get_paths_201(g: nx.Graph):
    """
    return all paths from input to output
    """
    path_blueprints = [[3], [0, 4], [1, 5], [0, 2, 5]]
    ops = get_op_list(g.name)
    paths = []
    for blueprint in path_blueprints:
        paths.append([ops[node] for node in blueprint])

    return paths