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

[BugFix] Fix fallback of deterministic samples when mean is not available #828

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions tensordict/nn/probabilistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,16 +472,25 @@ def _dist_sample(
try:
return dist.deterministic_sample
except AttributeError:
fallback = (
"mean" if isinstance(dist.support, D.constraints._Real) else "mode"
)
try:
return dist.mean
if fallback == "mean":
return dist.mean
elif fallback == "mode":
# Categorical dists don't have an average
return dist.mode
else:
raise AttributeError
except AttributeError:
raise NotImplementedError(
f"method {type(dist)}.deterministic_sample is not implemented."
f"method {type(dist)}.deterministic_sample is not implemented, no replacement found."
)
finally:
warnings.warn(
"deterministic_sample wasn't found when queried. "
f"{type(self).__name__} is falling back on mean instead. "
f"deterministic_sample wasn't found when queried on {type(dist)}. "
f"{type(self).__name__} is falling back on {fallback} instead. "
f"For better code quality and efficiency, make sure to either "
f"provide a distribution with a deterministic_sample attribute or "
f"to change the InteractionMode to the desired value.",
Expand Down
Loading