Skip to content

Add missing __len__ impl to Mixture selector#172

Open
AndrejOrsula wants to merge 1 commit intodanijar:mainfrom
AndrejOrsula:fix_mixture_selector
Open

Add missing __len__ impl to Mixture selector#172
AndrejOrsula wants to merge 1 commit intodanijar:mainfrom
AndrejOrsula:fix_mixture_selector

Conversation

@AndrejOrsula
Copy link
Copy Markdown

@AndrejOrsula AndrejOrsula commented Jan 19, 2025

First off, thanks for the great work!

Issue Description

Sampling with the Mixture selector results in an error due to missing __len__() implementation.

Traceback (most recent call last):
  ...
  File "dreamerv3/embodied/core/replay.py", line 123, in <lambda>
    limiters.wait(lambda: len(self.sampler), message)
TypeError: object of type 'Mixture' has no len()

limiters.wait(lambda: len(self.sampler), message)

PR Considerations

The implemented __len__() simply dispatches the call to the first selector. Reasoning:

  • At least one selector is guaranteed to exist after Mixture.__init__():

def __init__(self, selectors, fractions, seed=0):
assert set(selectors.keys()) == set(fractions.keys())
assert sum(fractions.values()) == 1, fractions
for key, frac in list(fractions.items()):
if not frac:
selectors.pop(key)
fractions.pop(key)
keys = sorted(selectors.keys())
self.selectors = [selectors[key] for key in keys]

  • The output of len() for each selector is identical because they have the same items:

def __setitem__(self, key, stepids):
for selector in self.selectors:
selector[key] = stepids
def __delitem__(self, key):
for selector in self.selectors:
del selector[key]

Signed-off-by: Andrej Orsula <orsula.andrej@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant