Files
pytorch/torch/distributions/exponential.py
Alican Bozkurt 94ff31f54d Implement Exponential distribution (#4234)
* add exponential distribution

* add exponential tests

* fix default val of sample_shape

* lambd->rate

* updates per review

* remove notes, keep failure_rate same in exponential test
2017-12-18 16:44:35 -05:00

35 lines
1.0 KiB
Python

from numbers import Number
import torch
from torch.distributions.distribution import Distribution
from torch.distributions.utils import broadcast_all
class Exponential(Distribution):
r"""
Creates a Exponential distribution parameterized by `rate`.
Example::
>>> m = Exponential(torch.Tensor([1.0]))
>>> m.sample() # Exponential distributed with rate=1
0.1046
[torch.FloatTensor of size 1]
Args:
rate (float or Tensor or Variable): rate = 1 / scale of the distribution
"""
has_rsample = True
def __init__(self, rate):
self.rate, = broadcast_all(rate)
batch_shape = torch.Size() if isinstance(rate, Number) else self.rate.size()
super(Exponential, self).__init__(batch_shape)
def rsample(self, sample_shape=torch.Size()):
shape = self._extended_shape(sample_shape)
return self.rate.new(*shape).exponential_() / self.rate
def log_prob(self, value):
self._validate_log_prob_arg(value)
return self.rate.log() - self.rate * value