Skip to content

Conversation

@tillahoffmann
Copy link
Collaborator

This PR adds the batch and event shape to the representation of distributions to help with development and debugging.

def __repr__(self) -> str:
cls = self.__class__
return (
f"<{cls.__module__}.{cls.__name__} object at {id(self):#x} with batch "
Copy link
Member

@OlaRonning OlaRonning Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the behavior of :#x in the f-string?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The :#x formats the id of the object as a 0x[hex] to keep the repr method consistent with the default other than adding the shapes.

>>> from numpyro import distributions as dists
>>> 
>>> d = dists.Normal().expand((5, 10, 7)).to_event(2)
>>> d
<numpyro.distributions.distribution.Independent object at 0x10723f7c0 with batch shape (5,) and event shape (10, 7)>
>>> object.__repr__(d)
'<numpyro.distributions.distribution.Independent object at 0x10723f7c0>'

Copy link
Member

@OlaRonning OlaRonning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks @tillahoffmann.
I checked print(Normal) and print(Normal()) and test case. All behave as expected.

@fehiepsi fehiepsi merged commit d1ca868 into pyro-ppl:master Jan 12, 2025
10 checks passed
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.

3 participants