Ejemplo n.º 1
0
def create_slice_1d(i: int, experiment: bopt.Experiment, resolution: int,
                    n_dims: int, x_slice: List[float],
                    model: GPy.models.GPRegression, sample: bopt.Sample,
                    show_marginal: int) -> Slice1D:
    param = experiment.hyperparameters[i]

    grid = param.range.grid(resolution)

    X_plot = np.zeros([resolution, n_dims], dtype=np.float32)

    for dim in range(n_dims):
        if dim == i:
            X_plot[:, dim] = grid
        else:
            X_plot[:, dim] = np.full([resolution],
                                     x_slice[dim],
                                     dtype=np.float32)

    X_plot_marginal = grid.reshape(-1, 1)

    if show_marginal == 1:
        others = experiment.predictive_samples_before(sample)
        X_m, Y_m = bopt.SampleCollection(others).to_xy()
        X_m = X_m[:, i].reshape(-1, 1)

        model = create_gp_for_data(experiment, [param], X_m, Y_m)

        mu, var = model.predict(X_plot_marginal)
    else:
        mu, var = model.predict(X_plot)

    mu = mu.reshape(-1)
    sigma = np.sqrt(var).reshape(-1)

    acq = bopt.ExpectedImprovement().raw_call(mu, sigma, model.Y.max())\
              .reshape(-1)

    other_samples: Dict[str, List[float]] = defaultdict(list)

    for other in experiment.predictive_samples_before(sample) + [sample]:
        other_x, other_y = other.to_xy()
        other_x = float(other_x.tolist()[i])

        if param.range.is_logscale():
            other_x = 10.0**other_x

        other_samples["x"].append(other_x)
        other_samples["y"].append(other_y)

    if param.range.is_logscale():
        x_slice_at = 10.0**x_slice[i]
        x = 10.0**grid
    else:
        x_slice_at = x_slice[i]
        x = grid

    return Slice1D(param, x.tolist(), x_slice_at, mu.tolist(), sigma.tolist(),
                   acq.tolist(), other_samples, model)
Ejemplo n.º 2
0
def predict(model: GPy.models.GPRegression, X: np.array) -> Tuple[np.array, np.array]:
    """Wrapper function for the prediction method of a GPy regression model.
    It return the standard deviation instead of the variance"""
    assert isinstance(
        model, GPy.models.GPRegression
    ), "This wrapper function is written for GPy.models.GPRegression"
    mu, var = model.predict(X)
    return mu, np.sqrt(var)
Ejemplo n.º 3
0
def test_gp(gpy_gp: GPy.models.GPRegression,
            data_X: np.ndarray,
            data_Y: np.ndarray,
            display_model: bool = False) -> float:
    """
    Evaluate the goodness of the model fit by RMSE value
    :param gpy_gp: the GPy regression model
    :param data_X: the query data points. Typically of x of validation data-set
    :param data_Y: the labeels. Typically of the y of validation data-set
    :param display_model: bool
    :return:
    """
    assert data_X.shape[0] == data_Y.shape[
        0], "Lengths of x and labels mismatch"
    assert data_X.shape[
        1] == gpy_gp.input_dim, "Dimension of x and the model dimension mismatch"
    mean_pred, var_pred = gpy_gp.predict(Xnew=data_X)
    rmse = np.sqrt(((mean_pred - np.squeeze(data_Y, -1))**2).mean())
    if display_model is True:
        print("Root Mean Squared Error (RMSE): ", str(rmse))
    return rmse
Ejemplo n.º 4
0
def create_slice_2d(i: int, j: int, experiment: bopt.Experiment,
                    resolution: int, n_dims: int, x_slice: List[float],
                    model: GPy.models.GPRegression, sample: bopt.Sample,
                    show_marginal: int) -> Slice2D:

    p1 = experiment.hyperparameters[i]
    p2 = experiment.hyperparameters[j]

    d1 = p1.range.grid(resolution)
    d2 = p2.range.grid(resolution)

    g1, g2 = np.meshgrid(d1, d2)

    gs = [0.0] * len(x_slice)
    gs[i] = g1
    gs[j] = g2

    for dim in range(len(x_slice)):
        if dim not in [i, j]:
            gs[dim] = np.full(g1.shape, x_slice[dim])

    grid = np.stack(gs, axis=-1)

    X_pred = grid.reshape(resolution * resolution, -1)

    if show_marginal == 1:
        others = experiment.predictive_samples_before(sample)
        X_m, Y_m = bopt.SampleCollection(others).to_xy()
        X_m = X_m[:, [i, j]].reshape(-1, 2)

        model = create_gp_for_data(experiment, [p1, p2], X_m, Y_m)

        mu, var = model.predict(X_pred[:, [i, j]])
    else:
        mu, var = model.predict(X_pred)

    mu = mu.reshape(-1)
    sigma = np.sqrt(var).reshape(-1)

    acq = bopt.ExpectedImprovement().raw_call(mu, sigma, model.Y.max())\
              .reshape(-1)

    mu = mu.reshape(resolution, resolution)
    sigma = sigma.reshape(resolution, resolution)
    acq = acq.reshape(resolution, resolution)

    other_samples: Dict[str, List[float]] = defaultdict(list)

    for other in experiment.predictive_samples_before(sample) + [sample]:
        other_x, other_y = other.to_xy()
        other_x1 = float(other_x.tolist()[i])
        other_x2 = float(other_x.tolist()[j])

        if p1.range.is_logscale():
            other_x1 = 10.0**other_x1

        if p2.range.is_logscale():
            other_x2 = 10.0**other_x2

        other_samples["x1"].append(other_x1)
        other_samples["x2"].append(other_x2)
        other_samples["y"].append(other_y)

    if p1.range.is_logscale():
        x1 = (10.0**d1).tolist()
        x1_slice_at = 10.0**x_slice[i]
    else:
        x1 = d1.tolist()
        x1_slice_at = x_slice[i]

    if p2.range.is_logscale():
        x2 = (10.0**d2).tolist()
        x2_slice_at = 10.0**x_slice[j]
    else:
        x2 = d2.tolist()
        x2_slice_at = x_slice[j]

    return Slice2D(p1, p2, x1, x2, x1_slice_at, x2_slice_at, mu.tolist(),
                   other_samples, model)