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

[RLlib] Fix PPOTorchPolicy producing float metrics when not using critic #27980

Merged
merged 4 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions rllib/algorithms/ppo/ppo_torch_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ def reduce_mean_valid(t):
mean_vf_loss = reduce_mean_valid(vf_loss_clipped)
# Ignore the value function.
else:
value_fn_out = 0
vf_loss_clipped = mean_vf_loss = 0.0
value_fn_out = torch.tensor(0.0).to(mean_policy_loss.device)
vf_loss_clipped = mean_vf_loss = torch.tensor(0.0).to(
mean_policy_loss.device
)

total_loss = reduce_mean_valid(
-surrogate_loss
Expand Down
3 changes: 3 additions & 0 deletions rllib/utils/torch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ def explained_variance(y: TensorType, pred: TensorType) -> TensorType:
The explained variance given a pair of labels and predictions.
"""
y_var = torch.var(y, dim=[0])
if y_var == 0.0:
# Model case in which y does not vary with explained variance of -1
return torch.tensor(-1.0).to(pred.device)
diff_var = torch.var(y - pred, dim=[0])
min_ = torch.tensor([-1.0]).to(pred.device)
return torch.max(min_, 1 - (diff_var / y_var))[0]
Expand Down