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

[Feature] SAC compatibility with composite distributions. #2447

Merged
merged 8 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test fixes
  • Loading branch information
albertbou92 authored and vmoens committed Oct 10, 2024
commit c8c9cc8c6905d89f7dfbee9cb170990203e301cd
19 changes: 13 additions & 6 deletions test/test_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -3541,6 +3541,18 @@ def _create_mock_common_layer_setup(
n_hidden=2,
composite_action_dist=False,
):
class QValueClass(nn.Module):
def __init__(self):
super().__init__()
self.linear1 = nn.Linear(n_hidden + n_act, n_hidden)
self.relu = nn.ReLU()
self.linear2 = nn.Linear(n_hidden, 1)

def forward(self, obs, act):
if isinstance(act, TensorDictBase):
act = act.get("action1")
return self.linear2(self.relu(self.linear1(torch.cat([obs, act], -1))))

common = MLP(
num_cells=ncells,
in_features=n_obs,
Expand All @@ -3553,12 +3565,7 @@ def _create_mock_common_layer_setup(
depth=1,
out_features=2 * n_act,
)
qvalue = MLP(
in_features=n_hidden + n_act,
num_cells=ncells,
depth=1,
out_features=1,
)
qvalue = QValueClass()
batch = [batch]
action = torch.randn(*batch, n_act)
td = TensorDict(
Expand Down
4 changes: 1 addition & 3 deletions torchrl/objectives/sac.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ def compute_log_prob(action_dist, action_or_tensordict, tensor_key):
if isinstance(action_or_tensordict, torch.Tensor):
log_p = action_dist.log_prob(action_or_tensordict)
else:
tensordict = action_dist.log_prob(action_or_tensordict)
log_p = tensordict.get(tensor_key)
maybe_log_prob = action_dist.log_prob(tensordict)
maybe_log_prob = action_dist.log_prob(action_or_tensordict)
if not isinstance(maybe_log_prob, torch.Tensor):
log_p = maybe_log_prob.get(tensor_key)
else:
Expand Down