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

Add missing __len__ impl to Mixture selector #172

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AndrejOrsula
Copy link

@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]

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