Skip to content
Closed
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
9 changes: 5 additions & 4 deletions torch/distributions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,18 @@ def softmax(tensor):
return F.softmax(tensor, -1)


def log_sum_exp(tensor, keepdim=True):
def log_sum_exp(tensor, dim=-1, keepdim=True):
r"""
Numerically stable implementation for the `LogSumExp` operation. The
summing is done along the last dimension.
summing is done along the required dimension. (Default: last dimension)

Args:
tensor (torch.Tensor)
dim (int): The dimension to sum up. Default: -1
keepdim (Boolean): Whether to retain the last dimension on summing.
"""
max_val = tensor.max(dim=-1, keepdim=True)[0]
return max_val + (tensor - max_val).exp().sum(dim=-1, keepdim=keepdim).log()
max_val = tensor.max(dim=dim, keepdim=True)[0]
return max_val + (tensor - max_val).exp().sum(dim=dim, keepdim=keepdim).log()


def logits_to_probs(logits, is_binary=False):
Expand Down