VowpalWabbit / coba

Contextual bandit benchmarking
https://coba-docs.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
48 stars 19 forks source link

learners/vowpal.py", line 133, in _prep_namespaces: TypeError: 'list' object cannot be interpreted as an integer #21

Closed arielf closed 1 year ago

arielf commented 1 year ago

Mark,

Thanks so much for fixing the exception in matmul in coba-6.2.0

Sorry to bother again! Nothing is urgent, just want to report.

I added some code to try more learners and I'm hitting a new exception (works fine with python 3.8) Seems like in 3.9 python type checking became stricter.

The exception:

% ./cobaex.py
2023-01-08 14:58:59 -- Processing chunk...
2023-01-08 14:58:59 --   * Recording Learner 0 parameters... (0.0 seconds) (completed)
2023-01-08 14:58:59 --   * Recording Learner 1 parameters... (0.0 seconds) (completed)
2023-01-08 14:58:59 --   * Recording Learner 2 parameters... (0.0 seconds) (completed)
2023-01-08 14:58:59 --   * Recording Learner 3 parameters... (0.0 seconds) (completed)
2023-01-08 14:58:59 --   * Recording Learner 4 parameters... (0.0 seconds) (completed)
2023-01-08 14:58:59 --   * Recording Environment 0 statistics... (0.0 seconds) (completed)
2023-01-08 14:58:59 --   * Evaluating Learner 0 on Environment 0... (0.08 seconds) (completed)
2023-01-08 14:59:00 --   * Evaluating Learner 1 on Environment 0... (0.18 seconds) (completed)
2023-01-08 14:59:00 --   * Evaluating Learner 2 on Environment 0... (0.0 seconds) (exception)
2023-01-08 14:59:00 --   * Unexpected exception:

  File "~/.VENV/3.9/lib/python3.9/site-packages/coba/experiments/process.py", line 172, in filter
    row = list(item.task.process(lrn, finalizer.filter(interactions)))
  File "~/.VENV/3.9/lib/python3.9/site-packages/coba/experiments/tasks.py", line 170, in process
    action,prob,info = predict(context, actions)
  File "~/.VENV/3.9/lib/python3.9/site-packages/coba/learners/primitives.py", line 130, in predict
    pred      = self._safe_predict(context,actions)
  File "~/.VENV/3.9/lib/python3.9/site-packages/coba/learners/primitives.py", line 228, in _safe_predict
    return self._learner.predict(context,actions)
  File "~/.VENV/3.9/lib/python3.9/site-packages/coba/learners/vowpal.py", line 291, in predict
    probs = self._vw.predict(self._vw.make_examples(context, adfs, None))
  File "~/.VENV/3.9/lib/python3.9/site-packages/coba/learners/vowpal.py", line 107, in make_examples
    vw_shared  = dict(self._prep_namespaces(shared))
  File "~/.VENV/3.9/lib/python3.9/site-packages/coba/learners/vowpal.py", line 133, in _prep_namespaces
    feats = feats.items() if isinstance(feats, Sparse) else enumerate(feats,self._get_namespace_keys(ns,len(feats)))

  TypeError: 'list' object cannot be interpreted as an integer

Traceback (most recent call last):
  File ".../cobaex.py", line 25, in <module>
    result.plot_learners()
  File "~/.VENV/3.9/lib/python3.9/site-packages/coba/experiments/results.py", line 981, in plot_learners
    rows = FilterPlottingData().filter(list(interactions), x, y)
  File "~/.VENV/3.9/lib/python3.9/site-packages/coba/experiments/results.py", line 588, in filter
    if not rows: raise CobaException("This result doesn't contain any evaluation data to plot.")
coba.exceptions.CobaException: This result doesn't contain any evaluation data to plot.

To reproduce, run the following script

Note: need to use python 3.9 or later env (3.8 runs fine)

#!/usr/bin/env python3

from coba.environments import Environments
from coba.learners.vowpal import (
    VowpalSoftmaxLearner,
    VowpalBagLearner,
    VowpalOffPolicyLearner
)
from coba.experiments  import Experiment

env = Environments.from_linear_synthetic(500, seed=5)
lrn = [
    VowpalSoftmaxLearner(),
    VowpalBagLearner(bag=30),
    VowpalOffPolicyLearner(),
]

result = Experiment(env, lrn).evaluate()
result.plot_learners()
mrucker commented 1 year ago

You're fine. Please bug away. I'm sorry you're hitting these issues :(. I just released a patch to fix this bug in coba 6.2.1. Please let me know if the patch doesn't fix your problem.

If you care to know, this was a funny one. It was caused by the fact that you're running an old version of VW (I know this because of the versions you sent earlier today). With the release of VW 9.2 there were some improvements made to the Python interface to speed up communication between Python and VW. To take advantage of the VW upgrade Coba was modified and there are now two sets of code now for interacting with VW (depending on the VW version that is installed). The older version doesn't get hit as much these days.

mrucker commented 1 year ago

@arielf hey also, if you find yourself wanting to use coba for a bigger project feel free to reach out to me on here if you have any questions. I've tried hard to keep up Coba documentation but it really isn't great right now. The example notebooks only cover a small portion of the functionality in coba, and the read the docs is very wrong at this point (I hope to fix it after my ICML papers are done.)

arielf commented 1 year ago

Confirming the 6.2.1 fixed the issue.

Yes, I was on vw 9.1.0 from early 2022. I just built 9.7.0.

Also, thanks for the kind offer for help. Appreciated.