david-cortes / contextualbandits

Python implementations of contextual bandits algorithms
http://contextual-bandits.readthedocs.io
BSD 2-Clause "Simplified" License
750 stars 148 forks source link

Process evaluateRejectionSampling in batches #30

Closed alexmill closed 4 years ago

alexmill commented 4 years ago

This commit modifies evaluateRejectionSampling so that, rather than iterating one-by-one through the dataset, rejections and rewards are evaluated in batches with numpy.

I haven't fully tested this with partial_fit methods or for situations when nchoices>2, but it works great for the policies I've tried. It's reduced the runtime for me by 100-fold. The benefits of batch processing obviously scale with the size of the batch, but improvements are noticeable even for small batches.

I might suggest adding tqdm as a dependency and adding a progress bar to big loops like this. In this case, one could replace

for i in range(X.shape[0]//batch_size+1):

with

for i in tqdm(range(X.shape[0]//batch_size+1), desc="Batches"):

This works great on my local version.

No pressure to merge, but this took me 10 minutes to write so I thought I'd make a pull request and share with the world (while I wait for my batches to run...). Cheers.

david-cortes commented 4 years ago

Thanks, merged. I'll change the docs too to reflect what it does. That progress bar looks good, but is there some way of making it an optional dependency?

alexmill commented 4 years ago

@david-cortes What is the minimum Python version required for this package? There is a way to only import a package if already installed, but the method for doing so depend on Python version.

david-cortes commented 4 years ago

There's no minimum required version, but I try to keep it as compatible as possible.