Skip to content
Closed
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: 3 additions & 3 deletions aten/src/ATen/native/ReduceOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,10 @@ Tensor& logsumexp_out(Tensor& result, const Tensor &self, int64_t dim_, bool kee
return result;
}

Tensor logsumexp(const Tensor &self, int64_t dim_, bool keepdim) {
int64_t dim = maybe_wrap_dim(dim_, self.dim());
Tensor logsumexp(const Tensor &self, int64_t dim, bool keepdim) {
int64_t dim_ = maybe_wrap_dim(dim, self.dim());

This comment was marked as off-topic.

Tensor result = self.type().tensor();
return at::native::logsumexp_out(result, self, dim, keepdim);
return at::native::logsumexp_out(result, self, dim_, keepdim);
}

// \DIM REDUCE ################################################################
Expand Down
4 changes: 2 additions & 2 deletions torch/distributions/categorical.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import torch
from torch.distributions import constraints
from torch.distributions.distribution import Distribution
from torch.distributions.utils import probs_to_logits, logits_to_probs, log_sum_exp, lazy_property, broadcast_all
from torch.distributions.utils import probs_to_logits, logits_to_probs, lazy_property, broadcast_all


class Categorical(Distribution):
Expand Down Expand Up @@ -45,7 +45,7 @@ def __init__(self, probs=None, logits=None, validate_args=None):
if probs is not None:
self.probs = probs / probs.sum(-1, keepdim=True)
else:
self.logits = logits - log_sum_exp(logits)
self.logits = logits - torch.logsumexp(logits, dim=-1)
self._param = self.probs if probs is not None else self.logits
self._num_events = self._param.size()[-1]
batch_shape = self._param.size()[:-1] if self._param.ndimension() > 1 else torch.Size()
Expand Down
6 changes: 3 additions & 3 deletions torch/distributions/relaxed_categorical.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import torch
from torch.distributions import constraints
from torch.distributions.categorical import Categorical
from torch.distributions.utils import clamp_probs, broadcast_all, log_sum_exp
from torch.distributions.utils import clamp_probs, broadcast_all
from torch.distributions.distribution import Distribution
from torch.distributions.transformed_distribution import TransformedDistribution
from torch.distributions.transforms import ExpTransform
Expand Down Expand Up @@ -58,7 +58,7 @@ def rsample(self, sample_shape=torch.Size()):
uniforms = clamp_probs(self.logits.new(self._extended_shape(sample_shape)).uniform_())
gumbels = -((-(uniforms.log())).log())
scores = (self.logits + gumbels) / self.temperature
return scores - log_sum_exp(scores)
return scores - torch.logsumexp(scores, dim=-1)

def log_prob(self, value):
K = self._categorical._num_events
Expand All @@ -68,7 +68,7 @@ def log_prob(self, value):
log_scale = (self.temperature.new(self.temperature.shape).fill_(K).lgamma() -
self.temperature.log().mul(-(K - 1)))
score = logits - value.mul(self.temperature)
score = (score - log_sum_exp(score)).sum(-1)
score = (score - torch.logsumexp(score, dim=-1)).sum(-1)
return score + log_scale


Expand Down
13 changes: 0 additions & 13 deletions torch/distributions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,6 @@ def softmax(tensor):
return F.softmax(tensor, -1)


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

Args:
tensor (torch.Tensor)
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()


def logits_to_probs(logits, is_binary=False):
r"""
Converts a tensor of logits into probabilities. Note that for the
Expand Down