Skip to content
Snippets Groups Projects

More efficient 'tell_many'

Merged Bas Nijholt requested to merge efficient_tell_many into master
All threads resolved!
Compare and Show latest version
2 files
+ 92
43
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -288,7 +288,7 @@ class Learner1D(BaseLearner):
self.update_losses(x, real=True)
# If the scale has increased enough, recompute all losses.
if self._scale[1] > self._oldscale[1]:
if self._scale[1] > 2 * self._oldscale[1]:
for interval in self.losses:
self.update_interpolated_loss_in_interval(*interval)
@@ -303,20 +303,25 @@ class Learner1D(BaseLearner):
self.update_neighbors(x, self.neighbors_combined)
self.update_losses(x, real=False)
def tell_many(self, xs, ys):
# if not (len(xs) > 0.5 * len(self.data) and len(xs) > 2):
# # Only run this more efficient method if there are
# # at least 2 points and the amount of points added are
# # at least half of the number of points already in 'data'.
# super().tell_many(xs, ys)
# return
def tell_many(self, xs, ys, *, force=False):
if not force and not (len(xs) > 0.5 * len(self.data) and len(xs) > 2):
# Only run this more efficient method if there are
# at least 2 points and the amount of points added are
# at least half of the number of points already in 'data'.
# These "magic numbers" are somewhat arbitrary.
super().tell_many(xs, ys)
return
# Add data points
for x, y in zip(xs, ys):
self.data[x] = y
self.pending_points.discard(x)
# Generate neighbors
# Get all data as numpy arrays
points = np.array(list(self.data.keys()))
values = np.array(list(self.data.values()))
# Generate neighbors
points_pending = np.array(list(self.pending_points))
points_combined = np.hstack([points_pending, points])
@@ -324,7 +329,6 @@ class Learner1D(BaseLearner):
self.neighbors_combined = _get_neighbors_from_list(points_combined)
# Update scale
values = np.array(list(self.data.values()))
self._bbox[0] = [points_combined.min(), points_combined.max()]
self._bbox[1] = [values.min(axis=0), values.max(axis=0)]
self._scale[0] = self._bbox[0][1] - self._bbox[0][0]
@@ -345,6 +349,7 @@ class Learner1D(BaseLearner):
# List with "real" intervals that have interpolated intervals inside
to_interpolate = []
self.losses_combined = {}
for ival in intervals_combined:
# If this interval exists in 'losses' then copy it otherwise
@@ -352,6 +357,9 @@ class Learner1D(BaseLearner):
if ival in self.losses:
self.losses_combined[ival] = self.losses[ival]
else:
# Set all losses to inf now, later they might be udpdated if the
# interval appears to be inside a real interval.
self.losses_combined[ival] = np.inf
x_left, x_right = ival
a, b = to_interpolate[-1] if to_interpolate else (None, None)
if b == x_left and (a, b) not in self.losses:
@@ -361,7 +369,10 @@ class Learner1D(BaseLearner):
to_interpolate.append((x_left, x_right))
for ival in to_interpolate:
self.update_interpolated_loss_in_interval(*ival)
if ival in self.losses:
# If this interval does not exist it should already
# have an inf loss.
self.update_interpolated_loss_in_interval(*ival)
def ask(self, n, tell_pending=True):
"""Return n points that are expected to maximally reduce the loss."""
Loading