Esempio n. 1
0
def _pairs_of_shapes(
    *, excluding_ranks: Container[int] = ()
) -> FrozenSet[Tuple[ShapeLike, ShapeLike]]:
    shapes = various_shapes(excluding_ranks=excluding_ranks)
    return frozenset(itertools.product(shapes, shapes))
Esempio n. 2
0
    space = Integers(3)
    with pytest.raises(ValueError):
        space**exponent


def test_search_space___pow___multiplies_correct_number_of_search_spaces(
) -> None:
    assert (Integers(5)**7).limit == 5**7


def _points_in_2D_search_space() -> tf.Tensor:
    return tf.constant([[-1.0, 0.4], [-1.0, 0.6], [0.0, 0.4], [0.0, 0.6],
                        [1.0, 0.4], [1.0, 0.6]])


@pytest.mark.parametrize("shape", various_shapes(excluding_ranks=[2]))
def test_discrete_search_space_raises_for_invalid_shapes(
        shape: ShapeLike) -> None:
    with pytest.raises(ValueError):
        DiscreteSearchSpace(tf.random.uniform(shape))


def test_discrete_search_space_points() -> None:
    space = DiscreteSearchSpace(_points_in_2D_search_space())
    npt.assert_array_equal(space.points, _points_in_2D_search_space())


@pytest.mark.parametrize("point", list(_points_in_2D_search_space()))
def test_discrete_search_space_contains_all_its_points(
        point: tf.Tensor) -> None:
    assert point in DiscreteSearchSpace(_points_in_2D_search_space())
Esempio n. 3
0
        return t + u

    assert add(tf.constant(1.), tf.constant(2.)) == tf.constant(3.)


@pytest.mark.parametrize('apply', [True, False])
@pytest.mark.parametrize('kwargs', [{}, {'autograph': False}])
def test_jit_compiles_function(apply: bool, kwargs: Any) -> None:
    @jit(apply, **kwargs)
    def one() -> tf.Tensor:
        return tf.constant(0)

    tf_function_type = type(tf.function(lambda x: x))
    assert isinstance(one, tf_function_type) == apply


@pytest.mark.parametrize('this_shape', various_shapes())
@pytest.mark.parametrize('that_shape', various_shapes())
def test_shapes_equal(this_shape: ShapeLike, that_shape: ShapeLike) -> None:
    assert shapes_equal(tf.ones(this_shape),
                        tf.ones(that_shape)) == (this_shape == that_shape)


@pytest.mark.parametrize(
    't, expected',
    [(tf.constant(0), np.array(0)),
     (np.arange(12).reshape(3, -1), np.arange(12).reshape(3, -1)),
     (tf.reshape(tf.range(12), [3, -1]), np.arange(12).reshape(3, -1))])
def test_to_numpy(t: TensorType, expected: np.ndarray) -> None:
    npt.assert_array_equal(to_numpy(t), expected)
Esempio n. 4
0
)
def test_probability_of_feasibility(threshold: float, at: tf.Tensor, expected: float) -> None:
    actual = probability_of_feasibility(QuadraticMeanAndRBFKernel(), threshold, at)
    npt.assert_allclose(actual, expected, rtol=1e-4)


@pytest.mark.parametrize("at", [tf.constant([[0.0]]), tf.constant([[-3.4]]), tf.constant([[0.2]])])
@pytest.mark.parametrize("threshold", [-2.3, 0.2])
def test_probability_of_feasibility_builder_builds_pof(threshold: float, at: tf.Tensor) -> None:
    builder = ProbabilityOfFeasibility(threshold)
    acq = builder.prepare_acquisition_function(zero_dataset(), QuadraticMeanAndRBFKernel())
    expected = probability_of_feasibility(QuadraticMeanAndRBFKernel(), threshold, at)
    npt.assert_allclose(acq(at), expected)


@pytest.mark.parametrize("shape", various_shapes() - {()})
def test_probability_of_feasibility_raises_on_non_scalar_threshold(shape: ShapeLike) -> None:
    threshold = tf.ones(shape)
    with pytest.raises(ValueError):
        probability_of_feasibility(QuadraticMeanAndRBFKernel(), threshold, tf.constant([[0.0]]))


@pytest.mark.parametrize("shape", [[], [0], [2]])
def test_probability_of_feasibility_raises_on_incorrect_at_shape(shape: ShapeLike) -> None:
    at = tf.ones(shape)
    with pytest.raises(ValueError):
        probability_of_feasibility(QuadraticMeanAndRBFKernel(), 0.0, at)


@pytest.mark.parametrize("shape", various_shapes() - {()})
def test_probability_of_feasibility_builder_raises_on_non_scalar_threshold(
Esempio n. 5
0
@pytest.mark.parametrize(
    'at', [tf.constant([[0.0]]),
           tf.constant([[-3.4]]),
           tf.constant([[0.2]])])
@pytest.mark.parametrize('threshold', [-2.3, 0.2])
def test_probability_of_feasibility_builder_builds_pof(threshold: float,
                                                       at: tf.Tensor) -> None:
    builder = ProbabilityOfFeasibility(threshold)
    acq = builder.prepare_acquisition_function(zero_dataset(),
                                               QuadraticWithUnitVariance())
    expected = probability_of_feasibility(QuadraticWithUnitVariance(),
                                          threshold, at)
    npt.assert_allclose(acq(at), expected)


@pytest.mark.parametrize('shape', various_shapes() - {()})
def test_probability_of_feasibility_raises_on_non_scalar_threshold(
        shape: ShapeLike) -> None:
    threshold = tf.ones(shape)
    with pytest.raises(ValueError):
        probability_of_feasibility(QuadraticWithUnitVariance(), threshold,
                                   tf.constant([[0.0]]))


@pytest.mark.parametrize('shape', [[], [0], [2]])
def test_probability_of_feasibility_raises_on_incorrect_at_shape(
        shape: ShapeLike) -> None:
    at = tf.ones(shape)
    with pytest.raises(ValueError):
        probability_of_feasibility(QuadraticWithUnitVariance(), 0.0, at)