Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_every_iteration_model_updater_with_cost():
"""
Tests that the model updater can use a different attribute
from loop_state as the training targets
"""
class MockModel(IModel):
def optimize(self):
pass
def set_data(self, X: np.ndarray, Y: np.ndarray):
self._X = X
self._Y = Y
@property
def X(self):
return self._X
@property
def Y(self):
return self._Y
mock_model = MockModel()
def test_batch_loop_fails_without_gradients_implemented():
parameter_space = ParameterSpace([ContinuousParameter('x', 0, 1)])
model = mock.create_autospec(IModel)
base_acquisition = ExpectedImprovement(model)
batch_size = 10
with pytest.raises(ValueError):
BayesianOptimizationLoop(parameter_space, model, base_acquisition, batch_size)
def test_model_based_montecarlo_sensitivity(space):
model = mock.create_autospec(IModel)
model.predict.return_value = (0.1*np.ones((3, 2)), np.zeros((3, 2)))
sensitivity = MonteCarloSensitivity(model, space)
num_mc = 1
main_sample = np.zeros((3,3))
fixing_sample = np.zeros((3,3))
main_effects, total_effects, total_variance = sensitivity.compute_effects(main_sample=main_sample, fixing_sample=fixing_sample, num_monte_carlo_points=num_mc)
keys = space.parameter_names
assert(all(k in main_effects for k in keys))
assert(all(k in total_effects for k in keys))
expected_shape = (2,)
assert(all(v.shape == expected_shape for v in list(main_effects.values())))
def test_outer_loop_model_update(mock_next_point_calculator, mock_user_function):
""" Checks the model has the correct number of data points """
class MockModelUpdater(ModelUpdater):
def __init__(self, model):
self.model = model
def update(self, loop_state):
self.model.set_data(loop_state.X, loop_state.Y)
class MockModel(IModel):
@property
def X(self):
return self._X
@property
def Y(self):
return self._Y
def predict(self, x):
pass
def set_data(self, x, y):
self._X = x
self._Y = y
def optimize(self):
def test_local_penaliztion_at_batch_point():
# Test edge case where evaluating local penalization at a point already in the batch.
# This can lead to divide by zero errors if not done correctly.
np.random.seed(123)
model = MockModel()
lp = LocalPenalization(model)
x_batch = np.random.rand(5, 1)
lp.update_batches(x_batch, 1, -0.1)
val, grad = lp.evaluate_with_gradients(x_batch)
assert not np.any(np.isnan(grad))
class MockModel(IModel):
def predict(self, X):
return np.random.rand(X.shape[0], 1), np.random.rand(X.shape[0], 1)
def _check_grad(lp, tol, x0):
grad_error = check_grad(lambda x: lp.evaluate_with_gradients(x[None, :])[0],
lambda x: lp.evaluate_with_gradients(x[None, :])[1], x0)
assert np.all(grad_error < tol)
import numpy as np
import pytest
from emukit.core import ParameterSpace, ContinuousParameter
from emukit.model_wrappers import GPyModelWrapper
from emukit.core.acquisition import Acquisition
from emukit.core.interfaces import IModel
from emukit.core.loop.loop_state import create_loop_state
from emukit.core.optimization import AcquisitionOptimizer
from emukit.core.loop.candidate_point_calculators import GreedyBatchPointCalculator
from emukit.experimental_design.model_based.experimental_design_loop import ExperimentalDesignLoop
class MockModel(IModel):
def __init__(self):
self._X = np.zeros((1, 1))
self._Y = np.zeros((1, 1))
def set_data(self, X, Y):
self._X = X
self._Y = Y
def predict(self, X):
return np.zeros((X.shape[0], 1)), np.zeros((X.shape[0], 1))
@property
def X(self):
return self._X
@property
"""
base_dims_list = list(range(n_input_dims))
kernels = [base_kernel_class(n_input_dims, active_dims=base_dims_list, ARD=ARD, name='kern_fidelity_1')]
for i in range(1, n_fidelities):
fidelity_name = 'fidelity' + str(i + 1)
interaction_kernel = base_kernel_class(n_input_dims, active_dims=base_dims_list, ARD=ARD,
name='scale_kernel_' + fidelity_name)
scale_kernel = base_kernel_class(1, active_dims=[n_input_dims], name='previous_fidelity_' + fidelity_name)
bias_kernel = base_kernel_class(n_input_dims, active_dims=base_dims_list,
ARD=ARD, name='bias_kernel_' + fidelity_name)
kernels.append(interaction_kernel * scale_kernel + bias_kernel)
return kernels
class NonLinearMultiFidelityModel(IModel, IDifferentiable):
"""
Non-linear Model for multiple fidelities. This implementation of the model only handles 1-dimensional outputs.
The theory implies the training points should be nested such that any point in a higher fidelity exists in all lower
fidelities, in practice the model will work if this constraint is ignored.
"""
def __init__(self, X_init: np.ndarray, Y_init: np.ndarray, n_fidelities, kernels: List[GPy.kern.Kern],
n_samples=100, verbose=False, optimization_restarts=5) -> None:
"""
By default the noise at intermediate levels will be fixed to 1e-4.
:param X_init: Initial X values.
:param Y_init: Initial Y values.
:param n_fidelities: Number of fidelities in problem.
:param kernels: List of kernels for each GP model at each fidelity. The first kernel should take input of
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
import numpy as np
from typing import Tuple
from emukit.core.interfaces import IModel
from emukit.quadrature.kernels.quadrature_kernels import QuadratureKernel
class IBaseGaussianProcess(IModel):
"""
Interface for the quadrature base-GP model
An instance of this can be passed as 'base_gp' to an ApproximateWarpedGPSurrogate object.
If this GP is initialized with data, use the raw evaluations Y of the integrand and not transformed values.
"""
def __init__(self, kern: QuadratureKernel) -> None:
"""
If this GP is initialized with data X, Y, use the raw evaluations Y of the integrand and not transformed values
as this is a general class that can be used with various quadrature methods. The transformation will be
performed automatically when the quadrature method is initialized subsequently.
:param kern: a quadrature kernel
"""
self.kern = kern