Vowpal Wabbit的Python接口是否支持对模型训练/测试的单个调用?

2024-05-23 19:40:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试使用Vowpal Wabbit开发CB模型。在我能找到的所有在线教程之后,我通过循环记录开始了Python的培训和测试:

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')

但是,我注意到,从命令行调用VW可以为您提供更简洁的培训/测试方法。例如,如果我有大众友好格式(VW_training_set.txt)的所有记录,我可以运行:

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

我的问题是:

  1. 假设所有参数都相等,这两种方法有什么区别吗
  2. 如果没有,是否有一种方法可以在不显式循环每个示例的情况下使用Python执行培训

编辑:我已尝试使用pyvw运行以下程序:

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()

列车运行预测文件已创建,但未填充。还创建了vw.model文件,但它似乎没有学到任何东西

编辑2:更新示例w/vw.finish()

编辑3:更新示例以包含正确的间距


Tags: testtxtformat示例modelsavelinetrain
1条回答
网友
1楼 · 发布于 2024-05-23 19:40:39
  1. Assuming all parameters are equal, is there any difference in these two approaches?

是的,我认为它们应该是等价的,除了-p train_predictions.txt。这在Python代码段中不存在

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

不完全是,但这是一个很好的建议,我们应该考虑添加一些内容

您可以利用以下事实来实现这一点:当前(这可能会在进一步的版本中发生变化)pyvw使用传递给构造函数的选项初始化vw实例,并且它有逻辑来判断是否传递了数据文件来处理它

因此,这应该是可行的:

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

相关问题 更多 >