示例#1
0
class TestEIF2D(unittest.TestCase):
    def setUp(self):
        self.graph2d = graph2d
        print(self.graph2d.Y)
        lids2d = np.random.choice(graph2d.X.shape[0], 10, replace=False)
        uids = np.delete(np.arange(self.graph2d.Y.shape[0]), lids2d)
        self.ym = np.argmax(self.graph2d.Y[uids])
        Yl2d = graph2d.Y[lids2d]
        graph2d.Y = np.zeros((graph2d.Y.shape[0]))
        graph2d.update(lids2d, Yl2d)
        self.lp = HMN(self.graph2d)

    def test_compute(self):
        self.lp.label_propagate(self.graph2d)
        print(self.graph2d.Y)
        eif = EIF(self.graph2d, self.lp)
        uids = np.delete(np.arange(self.graph2d.Y.shape[0]), self.graph2d.lids)
        a = []
        for id_test in uids:
            a.append(eif.compute(int(id_test)))
        print("a:", a)
        ympred = a[self.ym]
        print("max y prediction", ympred)
        rank = sorted(a, reverse=True).index(ympred)
        print("rank:", rank)
        assert len(a) == uids.shape[0]
示例#2
0
 def setUp(self):
     self.graph1d = graph1d
     lids1d = np.random.choice(graph1d.X.shape[0], 10, replace=False)
     uids = np.delete(np.arange(self.graph1d.Y.shape[0]), lids1d)
     self.ym = np.argmax(self.graph1d.Y[uids])
     Yl1d = graph1d.Y[lids1d]
     graph1d.Y = np.zeros((graph1d.Y.shape[0]))
     graph1d.update(lids1d, Yl1d)
     self.lp = HMN(self.graph1d)
    def test_hmn_2d(self):
        hmn = HMN(self.graph2d)
        YL = self.graph2d.Y[self.graph2d.lids]
        hmn.label_propagate(self.graph2d)

        assert np.array_equal(self.graph2d.Y[self.graph2d.lids], YL)
        #print("True 2d: ", self.trueY2d[:20])
        #print("Label propagation: ", self.graph2d.Y[:20])
        print("hmn mse", sum((self.trueY2d - self.graph2d.Y)**2))
示例#4
0
    def setUp(self):
        self.true1dY = graph1d.Y
        self.true2dY = graph2d.Y
        graph1d.Y = np.zeros((graph1d.Y.shape[0])).reshape(graph1d.Y.shape[0], -1)
        graph2d.Y = np.zeros((graph2d.Y.shape[0])).reshape(graph2d.Y.shape[0], -1)
        lp1 = HMN(graph1d)
        lp2 = HMN(graph2d)
        af1d = EIF(graph1d, lp1)
        af2d = EIF(graph2d, lp2)
        maximizer1d = RandomSampling(af1d)
        maximizer2d = RandomSampling(af2d)
        self.graph1d = graph1d
        self.graph2d = graph2d


        self.solver1d = GBOPT(self.graph1d, objective_function1d, lp1,
                              af1d, maximizer1d,
                              init_random_choice)

        self.solver2d = GBOPT(self.graph2d, objective_function2d, lp2,
                              af2d, maximizer2d,
                              init_random_choice)
示例#5
0
def gb_opt(graph,
           objective_function,
           label_prop_alg="hmn",
           acquisition_func="eif",
           maximize_func="random",
           n_samples=-1,
           num_iterations=1000,
           initial_design="random",
           n_init=10,
           lids_init=None,
           Y_init=None,
           rng=None,
           output_path=None):
    """
    General interface for graph-based semi-supervised learning for global
    black box optimization problems.

    Parameters
    ----------
    graph: GraphObject
        The graph defined by features and labels of nodes, and an adjacency matrix.
    objective_func:
        Function handle of for the objective function.
    label_prop_alg: LabelPropagationAlgObject
        The label propagation algorithm for graph-based semi-supervised learning.
    acquisition_func: BaseAcquisitionFunctionObject
        The acquisition function which will be maximized.
    maximize_func: BaseMaximizerObject
        The optimizers that maximize the acquisition function.
    n_samples: int
        The number of points to evaluate for maximize_func.
        -1 is evaluating all the points on the graph.
    initial_design: function
        Function that returns some points which will be evaluated before the optimization loop is started.
        This allows to initialize the graph.
    initial_points: int
        Defines the number of initial points that are evaluated before the actual optimization.
    lids_init: np.ndarray(l,)
        Indices of initial points to warm start label propagation.
    Y_init: np.ndarray(l,1)
        Function values of the already initial points.
    output_path: string
        Specifies the path where the intermediate output after each iteration will be saved.
        If None no output will be saved to disk.
    rng: np.random.RandomState
        Random number generator

    Returns
    -------
        dict with all results
    """

    assert n_init <= num_iterations, "Number of initial design point has to be <= than the number of iterations."
    assert lids_init is None or lids_init in np.arange(
        graph.X.shape[0]), "The initial points has to be in the sample pool."

    if rng is None:
        rng = np.random.RandomState(np.random.randint(0, 10000))

    if label_prop_alg == "hmn":
        lp = HMN(graph)
    else:
        raise ValueError(
            "'{}' is not a valid label propagation algorithm".format(
                label_prop_alg))

    if acquisition_func == "eif":
        af = EIF(graph, lp)
    elif acquisition_func == "random":
        af = Random()
    else:
        raise ValueError("'{}' is not a valid acquisition function".format(
            acquisition_func))

    if maximize_func == "random":
        max_func = RandomSampling(af, n_samples=n_samples, rng=rng)
    else:
        raise ValueError("'{}' is not a valid function to maximize the "
                         "acquisition function".format(maximize_func))

    if initial_design == "random":
        init_design = init_random_choice
    else:
        raise ValueError("'{}' is not a valid function to design the "
                         "initial points".format(initial_design))

    gbo = GBOPT(graph,
                objective_function,
                lp,
                af,
                max_func,
                initial_design=init_design,
                initial_points=n_init,
                rng=rng,
                output_path=output_path)

    x_best, f_max = gbo.run(num_iterations, lids_init, Y_init)

    results = dict()
    results["x_opt"] = x_best
    results["f_opt"] = f_max.tolist()
    # Best x along training
    results["incumbents"] = [inc for inc in gbo.incumbents]
    results["incumbents_values"] = [val for val in gbo.incumbents_values]
    results["runtime"] = gbo.runtime
    results["overhead"] = gbo.time_overhead
    results["labeled_indices"] = gbo.graph.lids.tolist()
    results["X"] = [x.tolist() for x in gbo.graph.X[gbo.graph.lids]]
    results["y"] = [y for y in gbo.graph.Y[gbo.graph.lids]]
    results["predictions"] = gbo.predictions

    return results
示例#6
0
 def __init__(self):
     self.graph2d = graph2d
     lp = HMN(self.graph2d)
     super(DemoAcquisitionFunction2d, self).__init__(self.graph2d, lp)