Does Vowpal Wabbit's Python interface support a single call for model training/testing?

Viewed 300

I've been trying to develop a CB model using Vowpal Wabbit. Following all the online tutorials I could find, I started by training and testing in Python by looping over records:

vw = pyvw.vw("--cb_explore_adf")

for i in range(df.shape[0]):

        # format example into vw-friendly format
        new_line = to_vw_format_train(df.iloc[i])

        #vw learn from each line
        vw_line = vw.parse(new_line, pyvw.vw.lContextualBandit)
        vw.learn(vw_line)

# save model for future use
vw.save('vw.model')

However, I noticed that calling VW from the command line gives you a more succinct way of training/testing. For example, if I had all records in a VW-friendly format (vw_training_set.txt) I could run:

vw -d vw_training_set.txt --cb_explore_adf -p train_predictions.txt -f vw.model

My questions are:

  1. Assuming all parameters are equal, is there any difference in these two approaches?
  2. If not, is there a way to perform training in Python without explicitly looping over each example?

Edit: I have tried to use pyvw to run the following:

from vowpalwabbit import pyvw
import os

pywd = "directory"
os.chdir(pywd)

# testing built in functionality
test_records = """shared |User var:5
Action 1:-1:0.5 | treatment=1
| treatment=2
    
shared |User var:3
| treatment=1
Action 2:-2:0.5 | treatment=2
"""
    
vw_train_records = open(r"test_file.txt","w+")
vw_train_records.write(test_records)
vw_train_records.close()
    
vw = pyvw.vw("-d test_file.txt --cb_explore_adf -p train_predictions.txt")
vw.save('vw.model')
vw.finish()

The train_predictions file is created, but it is not populated. The vw.model file is also created, but it doesn't seem like its learned anything.

Edit 2: Updated example w/ vw.finish()

Edit 3: Updated example to include correct spacing

1 Answers
  1. Assuming all parameters are equal, is there any difference in these two approaches?

Yes I believe they should be equivalent, except for the -p train_predictions.txt. That isn't present in the Python snippet.

  1. not, is there a way to perform training in Python without explicitly looping over each example?

Not exactly, but it's a great suggestion and something we should look at adding.

You can kind of do this by leveraging the fact that currently (this may change in further versions) pyvw initializes the vw instance with the options passed to the constructor and it has logic to tell if a data file is passed to process it.

So this should work:

vw = pyvw.vw("-d vw_training_set.txt --cb_explore_adf -p train_predictions.txt")
vw.save('vw.model')
Related