Skip to content

Commit

Permalink
Don't raise error on len of empty Batch
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Panchenko committed Apr 2, 2024
1 parent bf0d632 commit e327caa
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions tianshou/data/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,16 +870,17 @@ def __len__(self) -> int:
"""Return len(self)."""
lens = []
for obj in self.__dict__.values():
# TODO: causes inconsistent behavior to batch with empty batches
# and batch with empty sequences of other type. Remove, but only after
# Buffer and Collectors have been improved to no longer rely on this
if isinstance(obj, Batch) and obj.is_empty(recurse=True):
continue
if hasattr(obj, "__len__") and (isinstance(obj, Batch) or obj.ndim > 0):
lens.append(len(obj))
else:
raise TypeError(f"Object {obj} in {self} has no len()")
if len(lens) == 0:
# empty batch has the shape of any, like the tensorflow '?' shape.
# So it has no length.
raise TypeError(f"Object {self} has no len()")
if not lens:
return 0
return min(lens)

def is_empty(self, recurse: bool = False) -> bool:
Expand Down

0 comments on commit e327caa

Please # to comment.