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:
- Assuming all parameters are equal, is there any difference in these two approaches?
- 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