Back to Vowpal Wabbit

Python Basics

python/docs/source/examples/basics.ipynb

9.11.24.6 KB
Original Source

Python Basics

This notebook demonstrates the most basic capabilities of the vowpalwabit Python interface.

Any application using the Python package needs to begin by importing {mod}vowpalwabbit.

python
import vowpalwabbit

One we've imported vowpalwabbit, we can initialize VW either by passing a command line string (e.g., "--quiet -q ab --l2 0.01") or, in a more python-friendly manner, providing those as named arguments. Here we do the latter.

python
vw = vowpalwabbit.Workspace(quiet=True, q="ab", l2=0.01)

VW objects can do a lot, but the most important thing the can do is create examples and train/predict on those examples.

One way to create an example is to pass a string. This is the equivalent of a string in a VW file. For instance:

python
ex = vw.example("1 |a two features |b more features here")

Documentation can be looked at with help or you can look it up in the {meth}generated docs <vowpalwabbit.Workspace.learn>.

python
help(vw.learn)

Let's run that learn function and get a prediction:

python
vw.learn(ex)
print(f"current prediction = {ex.get_updated_prediction()}")

Here, get_updated_prediction retrieves the prediction made internally during learning. The "updated" aspect means "if I were to make a prediction on this example after this call to learn, what would that prediction be?"

Okay, so the prediction isn't quite where we want it yet. Let's learn a few more times and then print the prediction.

python
for _ in range(4):
    vw.learn(ex)

print(f"current prediction = {ex.get_updated_prediction()}")

This is now quite a bit closer to what is desired.

Now let's create a new example another form of example creation: python dictionaries. Here, you must provide a dictionary that maps namespaces (eg, 'a' and 'b') to lists of features. Features can either be strings (eg "foo"), or pairs of string/floats (eg ("foo", 0.5)). We'll create an example that's similar, but not identical to, the previous example to see how well VW has generalized.

Note that in this setup there is no label provided, which means that this will be considered a test example.

python
ex2 = vw.example({"a": ["features"], "b": ["more", "features", "there"]})

Given this example, we execute learn. But since it's a test example (no label), this will only make a prediction!

python
vw.learn(ex2)
print(f"current prediction = {ex2.get_updated_prediction()}")

Because this is a test example, we can get the raw prediction with get_simplelabel_prediction(). This is simplelabel because it's a regression problem. If we were doing, for instance, One-Against-All multiclass prediction, we would use get_multiclass_prediction, etc.

This prediction is only about half of what we want, but we're also missing a number of features.

Let's now give this example a label and train on it a few times:

python
ex2.set_label_string("-2.0")
for _ in range(5):
    vw.learn(ex2)

print(f"current prediction = {ex2.get_updated_prediction()}")

Now we can go back and see how this has affected the prediction behavior on the original example ex. We do this first by removing the label and then calling learn to make a prediction.

python
ex.set_label_string("")
vw.learn(ex)
print(f"current prediction = {ex.get_updated_prediction()}")

Clearly this has had an impact on the prediction for the first example. Let's put the label back and then iterate between learning on ex and ex2:

python
ex.set_label_string("1")
for i in range(10):
    vw.learn(ex)
    vw.learn(ex2)
    print(f"ex prediction = {ex.get_updated_prediction()}")
    print(f"ex2 prediction = {ex2.get_updated_prediction()}")

After a handful of updates, we can see that the prediction for ex is going back toward 1.0 and for ex2 back toward -2.0.

Now that we're done, it's safest to tell VW that we're done with these examples and that it can garbage collect them. (This should happen by default when they pass out of scope per Python's build in garbage collector, but that may not run soon enough if you're manipulating large numbers of examples at once!)

python
vw.finish_example(ex)
vw.finish_example(ex2)

Finally, when we're done with VW entirely, or perhaps want to start up a new VW instance, it's good behavior to close out any old ones. This is especially important if we wanted to save a model to disk: calling vw.finish() tells it to write the file. You can add f='mymodel' to the initialization line of the vw object if you want to play around with this!

python
vw.finish()

This is the end of the intro. For more, check out the docs for the main module {mod}vowpalwabbit.