Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 5 additions & 5 deletions adaptive/learner/learnerND.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,27 +513,27 @@ def _recompute_all_losses(self):
@property
def _scale(self):
# get the output scale
return self._max_value - self._min_value
return np.max(self._max_value - self._min_value)

def _update_range(self, new_output):
if self._min_value is None or self._max_value is None:
# this is the first point, nothing to do, just set the range
self._min_value = np.array(new_output)
self._max_value = np.array(new_output)
self._old_scale = self._scale
self._old_scale = self._scale or 1
return False

# if range in one or more directions is doubled, then update all losses
self._min_value = np.minimum(self._min_value, new_output)
self._max_value = np.maximum(self._max_value, new_output)

scale_multiplier = 1 / self._scale
scale_multiplier = 1 / (self._scale or 1)
if isinstance(scale_multiplier, float):
scale_multiplier = np.array([scale_multiplier], dtype=float)

# the maximum absolute value that is in the range. Because this is the
# largest number, this also has the largest absolute numerical error.
max_absolute_value_in_range = np.max(np.abs([self._min_value, self._max_value]), axis=0)
max_absolute_value_in_range = np.max(np.abs([self._min_value, self._max_value]))
# since a float has a relative error of 1e-15, the absolute error is the value * 1e-15
abs_err = 1e-15 * max_absolute_value_in_range
# when scaling the floats, the error gets increased.
Expand All @@ -546,7 +546,7 @@ def _update_range(self, new_output):

self._output_multiplier = scale_multiplier

scale_factor = np.max(np.nan_to_num(self._scale / self._old_scale))
scale_factor = np.max(self._scale / self._old_scale)
if scale_factor > self._recompute_losses_factor:
self._old_scale = self._scale
self._recompute_all_losses()
Expand Down
12 changes: 12 additions & 0 deletions adaptive/tests/test_learnernd.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-

import numpy as np
import scipy.spatial

from adaptive.learner import LearnerND
Expand Down Expand Up @@ -38,3 +39,14 @@ def test_interior_vs_bbox_gives_same_result():
simple(learner, goal=lambda l: l.loss() < 0.1)

assert learner.data == control.data


def test_vector_return_with_a_flat_layer():
f = generate_random_parametrization(ring_of_fire)
g = generate_random_parametrization(ring_of_fire)
h1 = lambda xy: np.array([f(xy), g(xy)])
h2 = lambda xy: np.array([f(xy), 0*g(xy)])
h3 = lambda xy: np.array([0*f(xy), g(xy)])
for function in [h1, h2, h3]:
learner = LearnerND(function, bounds=[(-1, 1), (-1, 1)])
simple(learner, goal=lambda l: l.loss() < 0.1)