Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion nevergrad/parametrization/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,17 @@ def bounds(self) -> tp.Tuple[tp.Optional[np.ndarray], tp.Optional[np.ndarray]]:
forwardable = _datalayers.ForwardableOperation.filter_from(self)
forwardable = [x for x in forwardable if x._layer_index > bound_layers[-1]._layer_index]
for f in forwardable:
bounds = tuple(None if b is None else f.forward(b) for b in bounds) # type: ignore
lower, upper = (None if b is None else f.forward(b) for b in bounds)
# decreasing operations (eg: multiplication by a negative number) swap the bounds
if lower is not None and upper is not None:
lower, upper = np.minimum(lower, upper), np.maximum(lower, upper)
elif lower is not None or upper is not None: # compare with the missing bound sent to infinity
k = 0 if upper is None else 1
with np.errstate(all="ignore"):
other = f.forward(np.full_like(bounds[k], np.inf if k == 0 else -np.inf, dtype=float))
if np.all(lower > other if k == 0 else upper < other):
lower, upper = upper, lower
bounds = (lower, upper)
return bounds

@property
Expand Down
21 changes: 21 additions & 0 deletions nevergrad/parametrization/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ def test_bound_estimation() -> None:
assert param.bounds == (-35, 65) # type: ignore


@pytest.mark.parametrize( # type: ignore
"param,expected",
[
(-ng.p.Scalar(lower=1, upper=5), (-5, -1)),
(ng.p.Scalar(lower=1, upper=5) * -1, (-5, -1)),
(10 / ng.p.Scalar(lower=1, upper=10), (1, 10)),
(0.5 ** ng.p.Scalar(lower=-3, upper=-1), (2, 8)),
(-ng.p.Scalar(init=3, lower=1), (None, -1)),
],
)
def test_bound_estimation_decreasing_operation(
param: ng.p.Scalar, expected: tp.Tuple[tp.Optional[int], tp.Optional[int]]
) -> None:
assert param.bounds == expected # type: ignore
param = param.copy().set_integer_casting()
lower = -10 if expected[0] is None else expected[0]
for val in range(lower, expected[1] + 1): # type: ignore
param.value = val
assert param.value == val


def test_softmax_layer() -> None:
param = ng.p.Array(shape=(4, 3))
param.random_state.seed(12)
Expand Down