def run(): # set RNG seed np.random.seed(0) tf.set_random_seed(0) # set up func = CosineMixture() bounds = func.bounds presenter = BenchmarkInputPresenter(func) output = BenchmarkOutputPresenter(func.__class__.__name__) optimizer = DirectOptimizer(bounds) model = BinaryPreferenceModel() data = UniformPreferenceDict(2) acquirer = ExpectedImprovementAcquirer(data, model, optimizer) ex = PreferenceExperiment(acquirer, presenter, output) # add initial observation n_initial_samples = 3 for _ in range(n_initial_samples): x1 = sample_uniformly(bounds) x2 = sample_uniformly(bounds) data[x1, x2] = presenter.get_choice(x1, x2) # run experiment n_iter = 3 for i in range(n_iter): ex.run()
def test_run(self): # set RNG seed np.random.seed(0) tf.set_random_seed(0) # set up bounds = [ (-3, 6), (-3, 6), ] optimizer = DirectOptimizer(bounds) model = BinaryPreferenceModel() presenter = MockInputPresenter() output = MockOutputPresenter() data = UniformPreferenceDict(2) a = (0, 0) b = (1, 1) c = (2, 2) d = (3, 3) data[a, b] = presenter.get_choice(a, b) data[a, c] = presenter.get_choice(a, c) data[b, c] = presenter.get_choice(b, c) data[b, d] = presenter.get_choice(b, d) data[c, d] = presenter.get_choice(c, d) # construct experiment acquirer = ExpectedImprovementAcquirer(data, model, optimizer) ex = PreferenceExperiment( acquirer, presenter, output ) # verify data self.assertEqual(len(data), 5) self.assertTrue(verify_preferences(data, presenter)) # run experiment ex.run() # verify data self.assertEqual(len(data), 6) self.assertTrue(verify_preferences(data, presenter)) # run experiment ex.run() # verify data self.assertEqual(len(data), 7) self.assertTrue(verify_preferences(data, presenter))
def run_quadratic(): # set RNG seed np.random.seed(RNG_SEED) tf.set_random_seed(RNG_SEED) # set up bounds = [(-5, 5)] func = np.square input_presenter = ExampleInputPresenter(func) optimizer = GridSearchOptimizer(bounds) model = BinaryPreferenceModel( lengthscale=1.0, **CONFIG ) # initialization n_initial_observations = 3 data = UniformPreferenceDict(1) initial_observations = np.random.uniform( *bounds[0], size=n_initial_observations ).tolist() for a, b in itertools.combinations(initial_observations, 2): data[(a,), (b,)] = input_presenter.get_choice(a, b) # construct acquirer acquirer = ExpectedImprovementAcquirer(data, model, optimizer) output_file('1d_quadratic.html') n_gridpoints = 1000 grid = np.linspace(*bounds[0], num=n_gridpoints) x = grid[:, np.newaxis] n_iter = 5 plots = [] for i in range(n_iter): xn = acquirer.next xb = acquirer.best choice = input_presenter.get_choice(xn, xb) acquirer.update(xn, xb, choice) p = figure( title='learned (iteration {}/{})'.format(i + 1, n_iter), x_axis_label='x', y_axis_label='y', x_range=bounds[0], ) # plot mean +/- standard deviation mean = acquirer.model.mean(x) stddev = np.sqrt(acquirer.model.variance(x)) lower = mean - stddev upper = mean + stddev p.line( grid, mean, line_width=3, legend='mean', ) p.patch( np.concatenate([grid, grid[::-1]]), np.concatenate([lower, upper[::-1]]), alpha=0.5, line_width=1, legend='stddev' ) # plot data valuations = acquirer.valuations xs, ys = zip(*valuations) xs_flat = [e[0] for e in xs] p.asterisk( xs_flat, ys, size=20, color='black', legend='data' ) # plot query and incumbent point p.circle( xn, acquirer.model.mean([xn]), size=10, color='red', legend='query' ) p.circle( xb, acquirer.model.mean([xb]), size=10, color='green', legend='incumbent' ) # plot true valuation q = figure( title='true valuation', x_axis_label='x', y_axis_label='y', x_range=bounds[0], ) q.line( grid, func(grid), legend='valuation', line_width=3, ) # plot data q.asterisk( xs_flat, func(xs_flat), size=20, color='black', legend='data' ) # plot query and incumbent point q.circle( xn, func(xn), size=10, color='red', legend='query' ) q.circle( xb, func(xb), size=10, color='green', legend='incumbent' ) plots.append(row(p, q)) show(column(*plots))
def test_interface_with_grid_search_optimizer_ard_lengthscale(self): with self.test_session(): # set RNG seed np.random.seed(RNG_SEED) tf.set_random_seed(RNG_SEED) # set up bounds = [ (-3, 6), (-3, 6), ] optimizer = GridSearchOptimizer(bounds) model = BinaryPreferenceModel( ard=True, **CONFIG ) data = UniformPreferenceDict(2) a = (0, 0) b = (1, 0) c = (2, 0) d = (3, 0) data[a, b] = 1 data[a, c] = 1 data[b, c] = 1 data[b, d] = 1 data[c, d] = 1 ab = (0.5, 0) bc = (1.5, 0) cd = (2.5, 0) data[a, ab] = 1 data[ab, b] = 1 data[b, bc] = 1 data[bc, c] = 1 data[c, cd] = 1 data[cd, d] = 1 data[ab, bc] = 1 data[ab, cd] = 1 data[bc, cd] = 1 # construct acquirer acquirer = ExpectedImprovementAcquirer(data, model, optimizer) # test that `next` needs to be called before `best` with self.assertRaises(ValueError): acquirer.best # test that `next` needs to be called before `valuations` with self.assertRaises(ValueError): acquirer.valuations # test `next` a1, a2 = a b1, b2 = b xn = xn1, xn2 = acquirer.next eps = 1.0 self.assertTrue( (a1 - eps < xn1 < b1), ) self.assertTrue(np.allclose(xn, acquirer.next)) # test `best` best = acquirer.best self.assertTrue(np.allclose(a, best)) self.assertTrue(np.allclose(best, acquirer.best)) # test `valuations` valuations = acquirer.valuations x, f = zip(*valuations) self.assertEqual(len(x), len(data.preferences())) self.assertTrue(all(a < b for a, b in zip(x, x[1:]))) self.assertTrue(all(a > b for a, b in zip(f, f[1:]))) # test `lengthscale` s1, s2 = model.lengthscale self.assertTrue(s1 < s2) # test `update` e = (-1, 0) acquirer.update(e, a, 1) acquirer.update(e, ab, 1) acquirer.update(e, b, 1) acquirer.update(e, bc, 1) # test `next` l1, l2 = (x[0] for x in bounds) xn1, xn2 = acquirer.next self.assertTrue( (l1 <= xn1 < a1), ) # test `best` best = acquirer.best self.assertTrue(best < b) # test `valuations` valuations = acquirer.valuations x, f = zip(*valuations) self.assertEqual(len(x), len(data.preferences())) self.assertTrue(all(a < b for a, b in zip(x, x[1:]))) self.assertTrue(all(a > b for a, b in zip(f, f[1:]))) # test `lengthscale` s1, s2 = model.lengthscale self.assertTrue(s1 < s2)
def test_interface_with_direct_optimizer_scalar_lengthscale(self): with self.test_session(): # set RNG seed np.random.seed(RNG_SEED) tf.set_random_seed(RNG_SEED) # set up bounds = [ (-3, 6), (-3, 6), ] optimizer = DirectOptimizer(bounds) model = BinaryPreferenceModel( lengthscale=1.0, **CONFIG ) data = UniformPreferenceDict(2) a = (0, 0) b = (1, 1) c = (2, 2) d = (3, 3) data[a, b] = 1 data[a, c] = 1 data[b, c] = 1 data[b, d] = 1 data[c, d] = 1 # construct acquirer acquirer = ExpectedImprovementAcquirer(data, model, optimizer) # test that `next` needs to be called before `best` with self.assertRaises(ValueError): acquirer.best # test that `next` needs to be called before `valuations` with self.assertRaises(ValueError): acquirer.valuations # test `next` a1, a2 = a b1, b2 = b xn = xn1, xn2 = acquirer.next eps = 0.5 self.assertTrue( (a1 - eps < xn1 < b1) and (a2 - eps < xn2 < b2) ) self.assertTrue(np.allclose(xn, acquirer.next)) # test `best` best = acquirer.best self.assertTrue(np.allclose(a, best)) self.assertTrue(np.allclose(best, acquirer.best)) # test `valuations` valuations = acquirer.valuations x, f = zip(*valuations) self.assertEqual(len(x), len(data.preferences())) self.assertTrue(all(a < b for a, b in zip(x, x[1:]))) self.assertTrue(all(a > b for a, b in zip(f, f[1:]))) # test `update` e = (-1, -1) acquirer.update(e, a, 1) acquirer.update(e, b, 1) # test `next` l1, l2 = (x[0] for x in bounds) xn1, xn2 = acquirer.next self.assertTrue( (l1 < xn1 < a1) and (l2 < xn2 < a2), ) # test `best` best = acquirer.best self.assertTrue(np.allclose(e, best)) # test `valuations` valuations = acquirer.valuations x, f = zip(*valuations) self.assertEqual(len(x), len(data.preferences())) self.assertTrue(all(a < b for a, b in zip(x, x[1:]))) self.assertTrue(all(a > b for a, b in zip(f, f[1:])))
def test_interface_with_grid_search_optimizer_vector_lengthscale(self): with self.test_session(): # set RNG seed np.random.seed(RNG_SEED) tf.set_random_seed(RNG_SEED) # set up bounds = [ (-3, 6), (-3, 6), ] optimizer = GridSearchOptimizer(bounds) lengthscale = np.array([1, 10], np.float32) model = BinaryPreferenceModel(lengthscale=lengthscale, **CONFIG) data = UniformPreferenceDict(2) a = (0, 0) b = (1, 0) c = (2, 0) d = (3, 0) data[a, b] = 1 data[a, c] = 1 data[b, c] = 1 data[b, d] = 1 data[c, d] = 1 # construct acquirer acquirer = ExpectedImprovementAcquirer(data, model, optimizer) # test that `next` needs to be called before `best` with self.assertRaises(ValueError): acquirer.best # pylint: disable=pointless-statement # test that `next` needs to be called before `valuations` with self.assertRaises(ValueError): acquirer.valuations # pylint: disable=pointless-statement # test `next` a1, _ = a b1, _ = b xn = xn1, _ = acquirer.next eps = 0.5 self.assertTrue((a1 - eps < xn1 < b1)) self.assertTrue(np.allclose(xn, acquirer.next)) # test `best` best = acquirer.best self.assertTrue(np.allclose(a, best)) self.assertTrue(np.allclose(best, acquirer.best)) # test `valuations` valuations = acquirer.valuations x, f = zip(*valuations) self.assertEqual(len(x), len(data.preferences())) self.assertTrue(all(a < b for a, b in zip(x, x[1:]))) self.assertTrue(all(a > b for a, b in zip(f, f[1:]))) # test `lengthscale` self.assertAllClose(model.lengthscale, lengthscale) # test `update` e = (-1, 0) acquirer.update(e, a, 1) acquirer.update(e, b, 1) # test `next` l1, _ = (x[0] for x in bounds) xn1, _ = acquirer.next self.assertTrue((l1 < xn1 < a1)) # test `best` best = acquirer.best self.assertTrue(np.allclose(e, best)) # test `valuations` valuations = acquirer.valuations x, f = zip(*valuations) self.assertEqual(len(x), len(data.preferences())) self.assertTrue(all(a < b for a, b in zip(x, x[1:]))) self.assertTrue(all(a > b for a, b in zip(f, f[1:])))