This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
[ff-2352] Add optimality gap to bandit logging #50
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
|
|
@@ -74,6 +74,7 @@ class BanditEvaluation: | |
| action_score: float | ||
| action_weight: float | ||
| gamma: float | ||
| optimality_gap: float | ||
|
|
||
|
|
||
| @dataclass | ||
|
|
@@ -89,14 +90,7 @@ def null_evaluation( | |
| flag_key: str, subject_key: str, subject_attributes: Attributes, gamma: float | ||
| ): | ||
| return BanditEvaluation( | ||
| flag_key, | ||
| subject_key, | ||
| subject_attributes, | ||
| None, | ||
| None, | ||
| 0.0, | ||
| 0.0, | ||
| gamma, | ||
| flag_key, subject_key, subject_attributes, None, None, 0.0, 0.0, gamma, 0.0 | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -129,9 +123,17 @@ def evaluate_bandit( | |
| bandit_model.action_probability_floor, | ||
| ) | ||
|
|
||
| selected_idx, selected_action = self.select_action( | ||
| flag_key, subject_key, action_weights | ||
| selected_action = self.select_action(flag_key, subject_key, action_weights) | ||
| selected_idx = next( | ||
| idx | ||
| for idx, action_context in enumerate(actions_with_contexts) | ||
| if action_context.action_key == selected_action | ||
| ) | ||
|
|
||
| optimality_gap = ( | ||
| max(score for _, score in action_scores) - action_scores[selected_idx][1] | ||
| ) | ||
|
|
||
| return BanditEvaluation( | ||
| flag_key, | ||
| subject_key, | ||
|
|
@@ -141,6 +143,7 @@ def evaluate_bandit( | |
| action_scores[selected_idx][1], | ||
| action_weights[selected_idx][1], | ||
| bandit_model.gamma, | ||
| optimality_gap, | ||
| ) | ||
|
|
||
| def score_actions( | ||
|
|
@@ -192,7 +195,7 @@ def weigh_actions( | |
| weights.append((best_action, remaining_weight)) | ||
| return weights | ||
|
|
||
| def select_action(self, flag_key, subject_key, action_weights) -> Tuple[int, str]: | ||
| def select_action(self, flag_key, subject_key, action_weights) -> str: | ||
|
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. Was this function incorrect before the change, or did you prefer having the
Author
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. Yes the index corresponded to the sorted list, rather than the original list, so returning it was useless and caused the bug. |
||
| # deterministic ordering | ||
| sorted_action_weights = sorted( | ||
| action_weights, | ||
|
|
@@ -209,10 +212,10 @@ def select_action(self, flag_key, subject_key, action_weights) -> Tuple[int, str | |
| cumulative_weight = 0.0 | ||
| shard_value = shard / self.total_shards | ||
|
|
||
| for idx, (action_key, weight) in enumerate(sorted_action_weights): | ||
| for action_key, weight in sorted_action_weights: | ||
| cumulative_weight += weight | ||
| if cumulative_weight > shard_value: | ||
| return idx, action_key | ||
| return action_key | ||
|
|
||
| # If no action is selected, return the last action (fallback) | ||
| raise BanditEvaluationError( | ||
|
|
||
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| __version__ = "3.1.3" | ||
| __version__ = "3.1.4" |
This file contains hidden or 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
This file contains hidden or 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
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.
👍 the formatting change tripped me up at first; I thought some parameters had been re-ordered. No, this just adds 0.0 as the optimality gap (last parameter)