def run_methods(train_points, train_targets, test_points, test_targets,
                model_parameters, optimizer_options, file_name, ind_num, title, show=False):
    method = 'means'

    print('Finding means...')
    means = KMeans(n_clusters=ind_num, n_init=1, max_iter=20)
    means.fit(train_points.T)
    inputs = means.cluster_centers_.T
    print('...found')

    for optimizer, color, opts in zip(['L-BFGS-B', 'Projected Newton'], ['-kx', '-mx'],
                                      optimizer_options):
        print('Optimizer', optimizer)
        model_covariance_obj = SquaredExponential(np.copy(model_parameters))
        new_gp = GPR(model_covariance_obj, method=method, optimizer=optimizer)
        res = new_gp.fit(train_points, train_targets, num_inputs=ind_num, optimizer_options=opts, inputs=inputs)
        name = optimizer
        metric = lambda w: new_gp.get_prediction_quality(w, train_points, train_targets, test_points, test_targets)
        x_lst, y_lst = res.plot_performance(metric, 'i', freq=1)
        plt.plot(x_lst, y_lst, color, label=name)

    plt.xlabel('Epoch')
    plt.ylabel('$R^2$-score on test data')
    plt.legend()
    plt.title(title)
    plt.savefig('../Plots/vi_variations/'+file_name + '.pgf')
    if show:
        plt.show()
def run_methods(train_points, train_targets, test_points, test_targets,
                model_parameters, optimizer_options, file_name, ind_num, title, show=False):

    print('Finding means...')
    means = KMeans(n_clusters=ind_num, n_init=1, max_iter=40)
    means.fit(train_points.T)
    inputs = means.cluster_centers_.T
    print('...found')

    method = 'svi'
    parametrization = 'cholesky'
    optimizer = 'L-BFGS-B'
    color = '-go'
    opts = optimizer_options[0]
    print('svi-L-BFGS-B')
    model_covariance_obj = SquaredExponential(np.copy(model_parameters))
    new_gp = GPR(model_covariance_obj, method=method, parametrization=parametrization)
    res = new_gp.fit(train_points, train_targets, num_inputs=ind_num, optimizer_options=opts, inputs=inputs)
    name = 'svi-L-BFGS-B'
    metric = lambda w: new_gp.get_prediction_quality(w, test_points, test_targets)
    x_lst, y_lst = res.plot_performance(metric, 'i', freq=1)
    plt.plot(x_lst, y_lst, color, label=name)

    print('vi-means')
    method = 'means'
    optimizer = 'L-BFGS-B'
    opt_options = optimizer_options[1]

    model_covariance_obj = SquaredExponential(np.copy(model_parameters))
    new_gp = GPR(model_covariance_obj, method=method, optimizer=optimizer)
    res = new_gp.fit(train_points, train_targets, num_inputs=ind_num, optimizer_options=opt_options, inputs=inputs)
    name = 'vi-means'
    metric = lambda w: new_gp.get_prediction_quality(w, train_points, train_targets, test_points, test_targets)
    x_lst, y_lst = res.plot_performance(metric, 'i', freq=1)
    plt.plot(x_lst, y_lst, '-kx', label=name)
    print(x_lst[-1])
    plt.xlabel('Epoch')
    plt.ylabel('$R^2$-score on test data')

    plt.legend()
    plt.title(title)
    plt.savefig('../Plots/vi_vs_svi/'+file_name + '.pgf')
    if show:
        plt.show()
def run_methods(train_points, train_targets, test_points, test_targets,
                model_parameters, optimizer_options, file_name, ind_num, title, show=False):

    print('Finding means...')
    means = KMeans(n_clusters=ind_num, n_init=1, max_iter=40)
    means.fit(train_points.T)
    inputs = means.cluster_centers_.T
    print('...found')

    # method = 'svi'
    # parametrization = 'natural'
    # # optimizer = 'L-BFGS-B'
    # color = '-yo'
    # opts = optimizer_options[0]
    # print('svi')
    # model_covariance_obj = SquaredExponential(np.copy(model_parameters))
    # new_gp = GPR(model_covariance_obj, method=method, parametrization=parametrization)
    # res = new_gp.fit(train_points, train_targets, num_inputs=ind_num, optimizer_options=opts, inputs=inputs)
    # name = 'svi-natural'
    # metric = lambda w: new_gp.get_prediction_quality(w, test_points, test_targets)
    # x_lst, y_lst = res.plot_performance(metric, 'i', freq=1)
    # plt.plot(x_lst, y_lst, color, label=name)

    print('vi-means')
    method = 'means'
    opt_options = optimizer_options[1]

    model_covariance_obj = SquaredExponential(np.copy(model_parameters))
    new_gp = GPR(model_covariance_obj, method=method)
    res = new_gp.fit(train_points, train_targets, num_inputs=ind_num, optimizer_options=opt_options, inputs=inputs)
    name = 'vi-means'
    metric = lambda w: new_gp.get_prediction_quality(w, train_points, train_targets, test_points, test_targets)
    x_lst, y_lst = res.plot_performance(metric, 'i', freq=1)
    plt.plot(x_lst, y_lst, '-kx', label=name)
    print(x_lst[-1])

    plt.ylabel('$R^2$-score on test data')


    plt.legend()
    plt.title(title)
    # plt.savefig('../Plots/vi_vs_svi/'+file_name + '.pgf')
    if show:
        plt.show()
Exemple #4
0
def run_methods(train_points, train_targets, test_points, test_targets,
                model_parameters, optimizer_options, file_name, ind_num, title, show=False):

    method = 'svi'
    parametrization = 'cholesky'

    means = KMeans(n_clusters=ind_num, n_init=3, max_iter=100, random_state=241)
    means.fit(train_points.T)
    inputs = means.cluster_centers_.T

    # for optimizer, color, opts in zip(['SAG', 'FG', 'L-BFGS-B'], ['-ro', '-bo', '-go'],
    #                                   optimizer_options[:-1]):
    #     print('Optimizer', optimizer)
    #     model_covariance_obj = SquaredExponential(np.copy(model_parameters))
    #     new_gp = GPR(model_covariance_obj, method=method, parametrization=parametrization, optimizer=optimizer)
    #     res = new_gp.fit(train_points, train_targets, num_inputs=ind_num, optimizer_options=opts, inputs=inputs)
    #     name = 'svi-' + optimizer
    #     metric = lambda w: new_gp.get_prediction_quality(w, test_points, test_targets)
    #     x_lst, y_lst = res.plot_performance(metric, 'i', freq=5)
    #     plt.plot(x_lst, y_lst, color, label=name)

    parametrization = 'natural'
    print('Natural parametrization')

    opt_options = optimizer_options[-1]

    model_covariance_obj = SquaredExponential(np.copy(model_parameters))
    new_gp = GPR(model_covariance_obj, method=method, parametrization=parametrization)
    res = new_gp.fit(train_points, train_targets, num_inputs=ind_num, optimizer_options=opt_options, inputs=inputs)
    name = 'svi-natural'
    metric = lambda w: new_gp.get_prediction_quality(w, test_points, test_targets)
    x_lst, y_lst = res.plot_performance(metric, 'i', freq=5)
    print(y_lst)
    plt.plot(x_lst, y_lst, '-yo', label=name)


    plt.xlabel('Epoch')
    plt.ylabel('$R^2$-score on test data')
    plt.legend()
    plt.title(title)
    # plt.savefig('../Plots/svi_variations/'+file_name + '.pgf')
    if show:
        plt.show()