-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
test: [RLOS2023] add test for cb with continous action #4630
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f8d0f61
test: add test for slate
michiboo 51c7045
test: test cleanup and slate test update
michiboo 4e8002e
test: minor cleanup and change assert_loss function to equal instead …
michiboo f50ca3e
test: add test for cb with continous action
michiboo b4aa040
Merge branch 'rlos2023/test' into cb_continuous
michiboo 6be1aef
modify blocker testcase
michiboo 76cb195
Merge branch 'cb_continuous' of https://github.com/michiboo/vowpal_wa…
michiboo 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
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import random | ||
import os | ||
from test_helper import get_function_object | ||
|
||
script_directory = os.path.dirname(os.path.realpath(__file__)) | ||
random.seed(10) | ||
|
||
|
||
def random_number_items(items): | ||
num_items_to_select = random.randint(1, len(items)) | ||
return random.sample(items, num_items_to_select) | ||
|
||
|
||
def generate_cb_data( | ||
num_examples, | ||
num_features, | ||
action_range, | ||
reward_function, | ||
logging_policy, | ||
context_name=["1"], | ||
): | ||
num_actions = int(abs(action_range[1] - action_range[0])) | ||
dataFile = f"cb_cont_test_{num_examples}_{num_actions}_{num_features}.txt" | ||
|
||
reward_function_obj = get_function_object( | ||
"cb_cont.reward_functions", reward_function["name"] | ||
) | ||
logging_policy_obj = get_function_object( | ||
"cb_cont.logging_policies", logging_policy["name"] | ||
) | ||
features = [f"feature{index}" for index in range(1, num_features + 1)] | ||
with open(os.path.join(script_directory, dataFile), "w") as f: | ||
for _ in range(num_examples): | ||
no_context = len(context_name) | ||
if no_context > 1: | ||
context = random.randint(1, no_context) | ||
else: | ||
context = 1 | ||
|
||
def return_cost_probability(chosen_action, context): | ||
cost = -reward_function_obj( | ||
chosen_action, context, **reward_function["params"] | ||
) | ||
if "params" not in logging_policy: | ||
logging_policy["params"] = {} | ||
logging_policy["params"]["chosen_action"] = chosen_action | ||
logging_policy["params"]["num_actions"] = num_actions | ||
probability = logging_policy_obj(**logging_policy["params"]) | ||
return cost, probability | ||
|
||
chosen_action = round(random.uniform(0, num_actions), 2) | ||
cost, probability = return_cost_probability(chosen_action, context) | ||
if no_context == 1: | ||
f.write( | ||
f'ca {chosen_action}:{cost}:{probability} | {" ".join(random_number_items(features))}\n' | ||
) | ||
else: | ||
f.write( | ||
f'ca {chosen_action}:{cost}:{probability} | {"s_" + context_name[context-1]} {" ".join(random_number_items(features))}\n' | ||
) | ||
f.write("\n") | ||
return os.path.join(script_directory, dataFile) |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
def constant_probability(chosen_action, **kwargs): | ||
return 1 | ||
|
||
|
||
def even_probability(chosen_action, **kwargs): | ||
num_actions = kwargs["num_actions"] | ||
return round(1 / num_actions, 2) |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
def fixed_reward(chosen_action, context, **kwargs): | ||
return 1 | ||
|
||
|
||
def piecewise_constant(chosen_action, context, **kwargs): | ||
reward = kwargs["reward"] | ||
return reward[int(chosen_action) - 1] | ||
|
||
|
||
def fixed_reward_two_action(chosen_action, context, **kwargs): | ||
if context == 1 and chosen_action >= 2: | ||
return 1 | ||
elif context == 2 and chosen_action < 2 and chosen_action >= 1: | ||
return 0 | ||
elif context == 1 and chosen_action < 1 and chosen_action >= 1: | ||
return 0 | ||
elif context == 2 and chosen_action < 1: | ||
return 1 | ||
return 1 |
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 |
---|---|---|
@@ -0,0 +1,189 @@ | ||
[ | ||
{ | ||
"test_name": "cb_two_action", | ||
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. cb -> ca |
||
"data_func": { | ||
"name": "generate_cb_data", | ||
"params": { | ||
"num_examples": 100, | ||
"num_features": 1, | ||
"action_range": [ | ||
0, | ||
2 | ||
], | ||
"reward_function": { | ||
"name": "piecewise_constant", | ||
"params": { | ||
"reward": [ | ||
ataymano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
1, | ||
0 | ||
] | ||
} | ||
}, | ||
"logging_policy": { | ||
"name": "even_probability", | ||
"params": {} | ||
} | ||
} | ||
}, | ||
"assert_functions": [ | ||
{ | ||
"name": "assert_loss", | ||
"params": { | ||
"expected_loss": -1, | ||
"decimal": 1 | ||
} | ||
}, | ||
{ | ||
"name": "assert_prediction", | ||
"params": { | ||
"expected_value": [ | ||
1, | ||
0 | ||
], | ||
"threshold": 0.8 | ||
} | ||
} | ||
], | ||
"grids": { | ||
"cb": { | ||
"#base": [ | ||
"--cats 2 --min_value 0 --max_value 2 --bandwidth 1" | ||
] | ||
}, | ||
"epsilon": { | ||
"--epsilon": [ | ||
0.1, | ||
0.2, | ||
0.3 | ||
] | ||
} | ||
}, | ||
"grids_expression": "cb * (epsilon)", | ||
"output": [ | ||
"--readable_model", | ||
"-p" | ||
] | ||
}, | ||
{ | ||
"test_name": "cb_two_action_diff_context", | ||
"data_func": { | ||
"name": "generate_cb_data", | ||
"params": { | ||
"num_examples": 100, | ||
"num_features": 2, | ||
"action_range": [ | ||
0, | ||
2 | ||
], | ||
"reward_function": { | ||
"name": "fixed_reward_two_action", | ||
"params": {} | ||
}, | ||
"logging_policy": { | ||
"name": "even_probability", | ||
"params": {} | ||
}, | ||
"context_name": [ | ||
"1", | ||
"2" | ||
] | ||
} | ||
}, | ||
"assert_functions": [ | ||
{ | ||
"name": "assert_loss", | ||
"params": { | ||
"expected_loss": -0.8, | ||
"decimal": 1 | ||
} | ||
}, | ||
{ | ||
"name": "assert_prediction", | ||
"params": { | ||
"expected_value": [ | ||
0.975, | ||
0.025 | ||
], | ||
"threshold": 0.1, | ||
"atol": 0.1, | ||
"rtol": 0.1 | ||
} | ||
} | ||
], | ||
"grids": { | ||
"cb": { | ||
"#base": [ | ||
"--cats 2 --min_value 0 --max_value 2 --bandwidth 1" | ||
] | ||
}, | ||
"epsilon": { | ||
"--epsilon": [ | ||
0.1, | ||
0.2, | ||
0.3 | ||
] | ||
} | ||
}, | ||
"grids_expression": "cb * (epsilon)", | ||
"output": [ | ||
"--readable_model", | ||
"-p" | ||
] | ||
}, | ||
{ | ||
"test_name": "cb_one_action", | ||
"data_func": { | ||
"name": "generate_cb_data", | ||
"params": { | ||
"num_examples": 10, | ||
"num_features": 1, | ||
"action_range": [ | ||
0, | ||
1 | ||
], | ||
"reward_function": { | ||
"name": "fixed_reward", | ||
"params": {} | ||
}, | ||
"logging_policy": { | ||
"name": "even_probability" | ||
} | ||
} | ||
}, | ||
"assert_functions": [ | ||
{ | ||
"name": "assert_loss", | ||
"params": { | ||
"expected_loss": -1 | ||
} | ||
}, | ||
{ | ||
"name": "assert_prediction", | ||
"params": { | ||
"expected_value": [0,1], | ||
"threshold": 0.1 | ||
} | ||
} | ||
], | ||
"grids": { | ||
"g0": { | ||
"#base": [ | ||
"--cats 2 --min_value 0 --max_value 1 --bandwidth 1" | ||
] | ||
}, | ||
"g1": { | ||
"--cb_type": [ | ||
"ips", | ||
"mtr", | ||
"dr", | ||
"dm" | ||
] | ||
} | ||
}, | ||
"grids_expression": "g0 * g1", | ||
"output": [ | ||
"--readable_model", | ||
"-p" | ||
] | ||
} | ||
] |
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
Oops, something went wrong.
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.
context = context_names[context - 1] after that? (or using np.random.choice)