-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix problem when learner has no more points for BalancingLearner #214
Open
basnijholt
wants to merge
5
commits into
main
Choose a base branch
from
fix_balancing_learner_and_sequence_learner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
884674a
fix problem when learner has no more points for BalancingLearner, clo…
basnijholt 2fedb39
fix for strategy="loss" and "npoints"
basnijholt 091d817
add a failing test
basnijholt 8a798f7
make test_asking_more_points_than_available pass
basnijholt 94645b3
rename _to_select -> _ask_all_learners
basnijholt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,20 +119,27 @@ def strategy(self, strategy): | |
' strategy="npoints", or strategy="cycle" is implemented.' | ||
) | ||
|
||
def _to_select(self, total_points): | ||
to_select = [] | ||
for index, learner in enumerate(self.learners): | ||
# Take the points from the cache | ||
if index not in self._ask_cache: | ||
self._ask_cache[index] = learner.ask(n=1, tell_pending=False) | ||
points, loss_improvements = self._ask_cache[index] | ||
if not points: # cannot ask for more points | ||
return to_select | ||
to_select.append( | ||
((index, points[0]), (loss_improvements[0], -total_points[index])) | ||
) | ||
return to_select | ||
|
||
def _ask_and_tell_based_on_loss_improvements(self, n): | ||
selected = [] # tuples ((learner_index, point), loss_improvement) | ||
total_points = [l.npoints + len(l.pending_points) for l in self.learners] | ||
for _ in range(n): | ||
to_select = [] | ||
for index, learner in enumerate(self.learners): | ||
# Take the points from the cache | ||
if index not in self._ask_cache: | ||
self._ask_cache[index] = learner.ask(n=1, tell_pending=False) | ||
points, loss_improvements = self._ask_cache[index] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here one would need a double break, but because that doesn't exist I make it into a function. |
||
to_select.append( | ||
((index, points[0]), (loss_improvements[0], -total_points[index])) | ||
) | ||
|
||
to_select = self._to_select(total_points) | ||
if not to_select: # cannot ask for more points | ||
break | ||
# Choose the optimal improvement. | ||
(index, point), (loss_improvement, _) = max(to_select, key=itemgetter(1)) | ||
total_points[index] += 1 | ||
|
@@ -156,7 +163,8 @@ def _ask_and_tell_based_on_loss(self, n): | |
if index not in self._ask_cache: | ||
self._ask_cache[index] = self.learners[index].ask(n=1) | ||
points, loss_improvements = self._ask_cache[index] | ||
|
||
if not points: # cannot ask for more points | ||
break | ||
selected.append(((index, points[0]), loss_improvements[0])) | ||
self.tell_pending((index, points[0])) | ||
|
||
|
@@ -172,6 +180,8 @@ def _ask_and_tell_based_on_npoints(self, n): | |
if index not in self._ask_cache: | ||
self._ask_cache[index] = self.learners[index].ask(n=1) | ||
points, loss_improvements = self._ask_cache[index] | ||
if not points: # cannot ask for more points | ||
break | ||
total_points[index] += 1 | ||
selected.append(((index, points[0]), loss_improvements[0])) | ||
self.tell_pending((index, points[0])) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a couple of things I don't understand here:
return
should be acontinue
; just because learneri
could not give any more points does not mean that no other learners can give any!learner.ask(1)
is guaranteed to return a point. At the moment there is no way for a learner to indicate that it has "no more points". If a learner returns no points then it is in violation of the API and other stuff is liable to break