pytorch/torch/distributions/exponential.py
Fritz Obermeyer 2431eac7c0 Ensure most Distribution methods are jittable (#11560)
Summary:
This adds tests in tests/test_distributions.py to ensure that all methods of `Distribution` objects are jittable.

I've replaced a few samplers with jittable versions:
- `.uniform_()` -> `torch.rand()`
- `.exponential_()` -> `-(-torch.rand()).log1p()`
- `.normal_()` -> `torch.normal(torch.zeros(...), torch.ones(...), ...)`

Some jit failures remain, and are marked in test_distributions.py
- `Cauchy` and `HalfCauchy` do not support sampling due to missing `.cauchy_()`
- `Binomial` does not support `.enumerate_support()` due to `arange` ignoring its first arg.
- `MultivariateNormal`, `LowRankMultivariateNormal` do not support `.mean`, `.entropy`

- [x] Currently some tests fail (I've skipped those) due to unavailability of `aten::uniform` and `aten::cauchy` in the jit. Can someone suggest how to add these? I tried to add declarations to `torch/csrc/ir.cpp` and `torch/csrc/passes/shape_analysis.cpp`, but that resulted in "Couldn't find operator" errors.
- [x] There are still lots of `TracerWarning`s that something doesn't match something. I'm not sure whether these are real.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/11560

Differential Revision: D9816327

Pulled By: apaszke

fbshipit-source-id: 72ec998ea13fc4c76d1ed003d9502e0fbaf728b8
2018-09-13 19:55:01 -07:00

84 lines
2.5 KiB
Python

from numbers import Number
import torch
from torch.distributions import constraints
from torch.distributions.exp_family import ExponentialFamily
from torch.distributions.utils import broadcast_all
class Exponential(ExponentialFamily):
r"""
Creates a Exponential distribution parameterized by :attr:`rate`.
Example::
>>> m = Exponential(torch.tensor([1.0]))
>>> m.sample() # Exponential distributed with rate=1
tensor([ 0.1046])
Args:
rate (float or Tensor): rate = 1 / scale of the distribution
"""
arg_constraints = {'rate': constraints.positive}
support = constraints.positive
has_rsample = True
_mean_carrier_measure = 0
@property
def mean(self):
return self.rate.reciprocal()
@property
def stddev(self):
return self.rate.reciprocal()
@property
def variance(self):
return self.rate.pow(-2)
def __init__(self, rate, validate_args=None):
self.rate, = broadcast_all(rate)
batch_shape = torch.Size() if isinstance(rate, Number) else self.rate.size()
super(Exponential, self).__init__(batch_shape, validate_args=validate_args)
def expand(self, batch_shape, _instance=None):
new = self._get_checked_instance(Exponential, _instance)
batch_shape = torch.Size(batch_shape)
new.rate = self.rate.expand(batch_shape)
super(Exponential, new).__init__(batch_shape, validate_args=False)
new._validate_args = self._validate_args
return new
def rsample(self, sample_shape=torch.Size()):
shape = self._extended_shape(sample_shape)
if torch._C._get_tracing_state():
# [JIT WORKAROUND] lack of support for ._exponential()
u = torch.rand(shape, dtype=self.rate.dtype, device=self.rate.device)
return -(-u).log1p() / self.rate
return self.rate.new(shape).exponential_() / self.rate
def log_prob(self, value):
if self._validate_args:
self._validate_sample(value)
return self.rate.log() - self.rate * value
def cdf(self, value):
if self._validate_args:
self._validate_sample(value)
return 1 - torch.exp(-self.rate * value)
def icdf(self, value):
if self._validate_args:
self._validate_sample(value)
return -torch.log(1 - value) / self.rate
def entropy(self):
return 1.0 - torch.log(self.rate)
@property
def _natural_params(self):
return (-self.rate, )
def _log_normalizer(self, x):
return -torch.log(-x)