-
Notifications
You must be signed in to change notification settings - Fork 5
docs: Add how-to for integrating with vw-estimators #50
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e4f59af
docs: Add how-to for integrating with vw-estimators
jackgerrits 3472d26
change input from file
jackgerrits de8481e
rename to event
jackgerrits 4745af8
add to env
jackgerrits 270b39b
Merge branch 'main' into cb_estimators
jackgerrits 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 |
|---|---|---|
|
|
@@ -17,4 +17,5 @@ dependencies: | |
| - pandas | ||
| - ipykernel | ||
| - matplotlib | ||
| - myst-nb | ||
| - myst-nb | ||
| - vw-estimators | ||
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 |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "attachments": {}, | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "# Using VW-Estimators\n", | ||
| "\n", | ||
| "`vw-estimators` is a library of off-policy estimators for various problems including contextual bandits. They can be used to evaluate target policies against a logged contextual bandit dataset. This library includes confidence bounds in addition to the estimators. In this example we process a trivial example dataset and feed the results into an IPS estimator and CressieRead confidence interval.\n", | ||
| "\n", | ||
| "`extract_label` is a function to translate how VW represents the contextual bandit label information into a more familiar form." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 15, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "from typing import List, Optional, Tuple\n", | ||
| "import vowpal_wabbit_next as vw\n", | ||
| "from estimators.bandits import ips, cressieread\n", | ||
| "\n", | ||
| "\n", | ||
| "# VW's labels contain extra info, and are associated with each example.\n", | ||
| "# This function extracts the logical CB label from the example list.\n", | ||
| "# Assumes examples have CBLabel typed labels.\n", | ||
| "def extract_label(examples: List[vw.Example]) -> Optional[Tuple[int, float, float]]:\n", | ||
| " first_is_shared = len(examples) > 0 and examples[0].get_label().shared\n", | ||
| " for i, example in enumerate(examples):\n", | ||
| " if (label := example.get_label().label) is not None:\n", | ||
| " _, cost, prob = label\n", | ||
| " return (i - (1 if first_is_shared else 0), cost, prob)\n", | ||
| " return None" | ||
| ] | ||
| }, | ||
| { | ||
| "attachments": {}, | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "We'll use the following trivial input for this example. There are two actions, each identified by a single feature. We're using a StringIO so we can treat this as if we were reading it from a file with a {py:class}`vowpal_wabbit_next.TextFormatReader`." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 16, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "import io\n", | ||
| "\n", | ||
| "input = io.StringIO(\n", | ||
| " \"\"\"shared | s\n", | ||
| "0:1:0.5 | a=0\n", | ||
| "| a=1\n", | ||
| "\n", | ||
| "shared | s\n", | ||
| "| a=0\n", | ||
| "1:0:0.5 | a=1\n", | ||
| "\n", | ||
| "shared | s\n", | ||
| "0:1:0.5 | a=0\n", | ||
| "| a=1\n", | ||
| "\n", | ||
| "shared | s\n", | ||
| "| a=0\n", | ||
| "1:0:0.5 | a=1\"\"\"\n", | ||
| ")" | ||
| ] | ||
| }, | ||
| { | ||
| "attachments": {}, | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "See comments for an explanation of the process." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 17, | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "name": "stdout", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "Estimate: -0.2625000001862645\n", | ||
| "Lower bound: -1.0\n", | ||
| "Upper bound: 0.3219763298424875\n" | ||
| ] | ||
| } | ||
| ], | ||
| "source": [ | ||
| "workspace = vw.Workspace([\"--cb_explore_adf\"])\n", | ||
| "estimator = ips.Estimator()\n", | ||
| "interval = cressieread.Interval(empirical_r_bounds=True)\n", | ||
| "\n", | ||
| "estimates = []\n", | ||
| "lower = []\n", | ||
| "upper = []\n", | ||
| "\n", | ||
| "with vw.TextFormatReader(workspace, input) as reader:\n", | ||
| " for event in reader:\n", | ||
| " logged_label = extract_label(event)\n", | ||
| "\n", | ||
| " # 1. Check if this event is labelled, if not skip it\n", | ||
| " if logged_label is None:\n", | ||
| " continue\n", | ||
| "\n", | ||
| " # 2. Predict and learn on the event\n", | ||
| " pmf = workspace.predict_then_learn_one(event)\n", | ||
| "\n", | ||
| " # 3. Extract the logged cost and the probability of choosing it according to the logged policy\n", | ||
| " logged_action_0_based, logged_cost, logged_prob = logged_label\n", | ||
| "\n", | ||
| " # 4. Get the probability of choosing the logged action according to the target policy\n", | ||
| " prediction_prob = next(x for i, x in pmf if i == logged_action_0_based)\n", | ||
| "\n", | ||
| " # 5. Feed these values into the estimator and confidence interval\n", | ||
| " # Note: These operate with rewards so we multiply cost by -1 to convert to reward\n", | ||
| " estimator.add_example(logged_prob, logged_cost * -1, prediction_prob)\n", | ||
| " interval.add_example(logged_prob, logged_cost * -1, prediction_prob)\n", | ||
| "\n", | ||
| "print(f\"Estimate: {estimator.get()}\")\n", | ||
| "bounds = interval.get()\n", | ||
| "print(f\"Lower bound: {bounds[0]}\")\n", | ||
| "print(f\"Upper bound: {bounds[1]}\")" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "pynextdev", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": { | ||
| "name": "ipython", | ||
| "version": 3 | ||
| }, | ||
| "file_extension": ".py", | ||
| "mimetype": "text/x-python", | ||
| "name": "python", | ||
| "nbconvert_exporter": "python", | ||
| "pygments_lexer": "ipython3", | ||
| "version": "3.10.8" | ||
| }, | ||
| "orig_nbformat": 4, | ||
| "vscode": { | ||
| "interpreter": { | ||
| "hash": "3c562d20ef5aa9e83e98cc981b6703965c3968967e80b00b9de18f40ae75cc1c" | ||
| } | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 2 | ||
| } | ||
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.
[nit] dict(pmf)[logged_action_0_based] (not sure which one is more readable)
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.
Oh interesting, I didn't know you could init a dictionary like that