Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accelerated discrete sampling by ~3-5x times #90

Merged
merged 5 commits into from
Nov 3, 2023

Conversation

jrzkaminski
Copy link
Collaborator

Accelerated discrete sampling using cumulative distribution and numpy function. It could be further used to make vector predictions instead of iterating over samples.

@jrzkaminski jrzkaminski added the enhancement New feature or request label Nov 1, 2023
@jrzkaminski jrzkaminski self-assigned this Nov 1, 2023
@jrzkaminski
Copy link
Collaborator Author

I've also made an itertools version of the same algorithm, it is even faster, but does not accept vectors or matrices.

def choose(self, node_info: Dict[str, Union[float, str]], pvals: List[str]) -> str:
        vals = node_info["vals"]
        dist = self.get_dist(node_info, pvals)

        cumulative_dist = list(accumulate(dist))

        rand = random.random()
        rindex = next((i for i, ubound in enumerate(cumulative_dist) if rand < ubound), len(vals) - 1)

        return vals[rindex]

@jrzkaminski jrzkaminski marked this pull request as ready for review November 1, 2023 16:12
@codecov-commenter
Copy link

Codecov Report

Merging #90 (0f9c34c) into master (f234a9f) will decrease coverage by 0.09%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #90      +/-   ##
==========================================
- Coverage   65.03%   64.95%   -0.09%     
==========================================
  Files          43       43              
  Lines        2903     2896       -7     
==========================================
- Hits         1888     1881       -7     
  Misses       1015     1015              
Files Coverage Δ
bamt/nodes/discrete_node.py 96.29% <100.00%> (-0.43%) ⬇️

@jrzkaminski jrzkaminski merged commit 8b6fedb into aimclub:master Nov 3, 2023
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants