Skip to content
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: 10 additions & 7 deletions ml-agents/mlagents/trainers/torch/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ def pdf(self, value):
).squeeze(-1)

def log_prob(self, value):
return torch.log(self.pdf(value))
return torch.log(self.pdf(value) + EPSILON)

def all_log_prob(self):
return torch.log(self.probs)
return torch.log(self.probs + EPSILON)

def entropy(self):
return -torch.sum(self.probs * torch.log(self.probs), dim=-1)
return -torch.sum(self.probs * torch.log(self.probs + EPSILON), dim=-1)


class GaussianDistribution(nn.Module):
Expand Down Expand Up @@ -183,10 +183,13 @@ def _create_policy_branches(self, hidden_size: int) -> nn.ModuleList:
return nn.ModuleList(branches)

def _mask_branch(self, logits: torch.Tensor, mask: torch.Tensor) -> torch.Tensor:
raw_probs = torch.nn.functional.softmax(logits, dim=-1) * mask
normalized_probs = raw_probs / torch.sum(raw_probs, dim=-1).unsqueeze(-1)
normalized_logits = torch.log(normalized_probs + EPSILON)
return normalized_logits
# Zero out masked logits, then subtract a large value. Technique mentionend here:
# https://arxiv.org/abs/2006.14171. Our implementation is ONNX and Barrcuda-friendly.
flipped_mask = 1.0 - mask
adj_logits = logits * mask - 1e8 * flipped_mask
probs = torch.nn.functional.softmax(adj_logits, dim=-1)
log_probs = torch.log(probs + EPSILON)
return log_probs

def _split_masks(self, masks: torch.Tensor) -> List[torch.Tensor]:
split_masks = []
Expand Down