Distributions¶
Distributions to use with borch.
It basically does some minor modifications to torch.distributions
-
class
borch.distributions.Bernoulli(probs=None, logits=None, validate_args=None, posterior=None)¶ Creates a Bernoulli distribution parameterized by
probsorlogits(but not both).Samples are binary (0 or 1). They take the value 1 with probability p and 0 with probability 1 - p.
Example:
>> m = Bernoulli(torch.tensor([0.3])) >> m.sample() # 30% chance 1; 70% chance 0 tensor([ 0.])
- Parameters
probs (Number, Tensor) – the probability of sampling 1
logits (Number, Tensor) – the log-odds of sampling 1
-
distribution_cls¶ alias of
torch.distributions.bernoulli.Bernoulli
-
class
borch.distributions.Beta(concentration1, concentration0, validate_args=None, posterior=None)¶ Beta distribution parameterized by
concentration1andconcentration0.Example:
>> m = Beta(torch.tensor([0.5]), torch.tensor([0.5])) >> m.sample() # Beta distributed with concentration concentration1 and concentration0 tensor([ 0.1046])
- Parameters
concentration1 (float or Tensor) – 1st concentration parameter of the distribution (often referred to as alpha)
concentration0 (float or Tensor) – 2nd concentration parameter of the distribution (often referred to as beta)
-
distribution_cls¶ alias of
torch.distributions.beta.Beta
-
class
borch.distributions.Binomial(total_count, probs=None, logits=None, validate_args=None, posterior=None)¶ Creates a Binomial distribution parameterized by
total_countand eitherprobsorlogits(but not both).total_countmust be broadcastable withprobs/logits.Example:
>> m = Binomial(100, torch.tensor([0 , .2, .8, 1])) >> x = m.sample() tensor([ 0., 22., 71., 100.]) >> m = Binomial(torch.tensor([[5.], [10.]]), torch.tensor([0.5, 0.8])) >> x = m.sample() tensor([[ 4., 5.], [ 7., 6.]])
- Parameters
total_count (int or Tensor) – number of Bernoulli trials
probs (Tensor) – Event probabilities
logits (Tensor) – Event log-odds
-
distribution_cls¶ alias of
torch.distributions.binomial.Binomial
-
class
borch.distributions.Categorical(probs=None, logits=None, validate_args=None, posterior=None)¶ Creates a categorical distribution parameterized by either
probsorlogits(but not both).Note
It is equivalent to the distribution that
torch.multinomial()samples from.Samples are integers from \(\{0, \ldots, K-1\}\) where K is
probs.size(-1).If probs is 1-dimensional with length-K, each element is the relative probability of sampling the class at that index.
If probs is N-dimensional, the first N-1 dimensions are treated as a batch of relative probability vectors.
Note
The probs argument must be non-negative, finite and have a non-zero sum, and it will be normalized to sum to 1 along the last dimension.
probswill return this normalized value. The logits argument will be interpreted as unnormalized log probabilities and can therefore be any real number. It will likewise be normalized so that the resulting probabilities sum to 1 along the last dimension.logitswill return this normalized value.See also:
torch.multinomial()Example:
>> m = Categorical(torch.tensor([ 0.25, 0.25, 0.25, 0.25 ])) >> m.sample() # equal probability of 0, 1, 2, 3 tensor(3)
- Parameters
probs (Tensor) – event probabilities
logits (Tensor) – event log probabilities (unnormalized)
-
distribution_cls¶ alias of
torch.distributions.categorical.Categorical
-
class
borch.distributions.Cauchy(loc, scale, validate_args=None, posterior=None)¶ Samples from a Cauchy (Lorentz) distribution. The distribution of the ratio of independent normally distributed random variables with means 0 follows a Cauchy distribution.
Example:
>> m = Cauchy(torch.tensor([0.0]), torch.tensor([1.0])) >> m.sample() # sample from a Cauchy distribution with loc=0 and scale=1 tensor([ 2.3214])
- Parameters
loc (float or Tensor) – mode or median of the distribution.
scale (float or Tensor) – half width at half maximum.
-
distribution_cls¶ alias of
torch.distributions.cauchy.Cauchy
-
class
borch.distributions.Chi2(df, validate_args=None, posterior=None)¶ Creates a Chi2 distribution parameterized by shape parameter
df. This is exactly equivalent toGamma(alpha=0.5*df, beta=0.5)Example:
>> m = Chi2(torch.tensor([1.0])) >> m.sample() # Chi2 distributed with shape df=1 tensor([ 0.1046])
- Parameters
df (float or Tensor) – shape parameter of the distribution
-
distribution_cls¶ alias of
torch.distributions.chi2.Chi2
-
class
borch.distributions.Delta(value, posterior=None)¶ Implements a Delta distribution
Example
>>> >> dist = Delta(0) >> float(dist.sample()) 0.0
-
distribution_cls¶ alias of
borch.distributions.distributions.Delta
-
-
class
borch.distributions.Dirichlet(concentration, validate_args=None, posterior=None)¶ Creates a Dirichlet distribution parameterized by concentration
concentration.Example:
>> m = Dirichlet(torch.tensor([0.5, 0.5])) >> m.sample() # Dirichlet distributed with concentrarion concentration tensor([ 0.1046, 0.8954])
- Parameters
concentration (Tensor) – concentration parameter of the distribution (often referred to as alpha)
-
distribution_cls¶ alias of
torch.distributions.dirichlet.Dirichlet
-
class
borch.distributions.Exponential(rate, validate_args=None, posterior=None)¶ Creates a Exponential distribution parameterized by
rate.Example:
>> m = Exponential(torch.tensor([1.0])) >> m.sample() # Exponential distributed with rate=1 tensor([ 0.1046])
- Parameters
rate (float or Tensor) – rate = 1 / scale of the distribution
-
distribution_cls¶ alias of
torch.distributions.exponential.Exponential
-
class
borch.distributions.FisherSnedecor(df1, df2, validate_args=None, posterior=None)¶ Creates a Fisher-Snedecor distribution parameterized by
df1anddf2.Example:
>> m = FisherSnedecor(torch.tensor([1.0]), torch.tensor([2.0])) >> m.sample() # Fisher-Snedecor-distributed with df1=1 and df2=2 tensor([ 0.2453])
- Parameters
df1 (float or Tensor) – degrees of freedom parameter 1
df2 (float or Tensor) – degrees of freedom parameter 2
-
distribution_cls¶ alias of
torch.distributions.fishersnedecor.FisherSnedecor
-
class
borch.distributions.Gamma(concentration, rate, validate_args=None, posterior=None)¶ Creates a Gamma distribution parameterized by shape
concentrationandrate.Example:
>> m = Gamma(torch.tensor([1.0]), torch.tensor([1.0])) >> m.sample() # Gamma distributed with concentration=1 and rate=1 tensor([ 0.1046])
- Parameters
concentration (float or Tensor) – shape parameter of the distribution (often referred to as alpha)
rate (float or Tensor) – rate = 1 / scale of the distribution (often referred to as beta)
-
distribution_cls¶ alias of
torch.distributions.gamma.Gamma
-
class
borch.distributions.Geometric(probs=None, logits=None, validate_args=None, posterior=None)¶ Creates a Geometric distribution parameterized by
probs, whereprobsis the probability of success of Bernoulli trials. It represents the probability that in \(k + 1\) Bernoulli trials, the first \(k\) trials failed, before seeing a success.Samples are non-negative integers [0, \(\inf\)).
Example:
>> m = Geometric(torch.tensor([0.3])) >> m.sample() # underlying Bernoulli has 30% chance 1; 70% chance 0 tensor([ 2.])
- Parameters
probs (Number, Tensor) – the probability of sampling 1. Must be in range (0, 1]
logits (Number, Tensor) – the log-odds of sampling 1.
-
distribution_cls¶ alias of
torch.distributions.geometric.Geometric
-
class
borch.distributions.Gumbel(loc, scale, validate_args=None, posterior=None)¶ Samples from a Gumbel Distribution.
Examples:
>> m = Gumbel(torch.tensor([1.0]), torch.tensor([2.0])) >> m.sample() # sample from Gumbel distribution with loc=1, scale=2 tensor([ 1.0124])
- Parameters
loc (float or Tensor) – Location parameter of the distribution
scale (float or Tensor) – Scale parameter of the distribution
-
distribution_cls¶ alias of
torch.distributions.gumbel.Gumbel
-
class
borch.distributions.HalfCauchy(scale, validate_args=None, posterior=None)¶ Creates a half-Cauchy distribution parameterized by scale where:
X ~ Cauchy(0, scale) Y = |X| ~ HalfCauchy(scale)
Example:
>> m = HalfCauchy(torch.tensor([1.0])) >> m.sample() # half-cauchy distributed with scale=1 tensor([ 2.3214])
- Parameters
scale (float or Tensor) – scale of the full Cauchy distribution
-
distribution_cls¶ alias of
torch.distributions.half_cauchy.HalfCauchy
-
class
borch.distributions.HalfNormal(scale, validate_args=None, posterior=None)¶ Creates a half-normal distribution parameterized by scale where:
X ~ Normal(0, scale) Y = |X| ~ HalfNormal(scale)
Example:
>> m = HalfNormal(torch.tensor([1.0])) >> m.sample() # half-normal distributed with scale=1 tensor([ 0.1046])
- Parameters
scale (float or Tensor) – scale of the full Normal distribution
-
distribution_cls¶ alias of
torch.distributions.half_normal.HalfNormal
-
class
borch.distributions.Kumaraswamy(concentration1, concentration0, validate_args=None, posterior=None)¶ Samples from a Kumaraswamy distribution.
Example:
>> m = Kumaraswamy(torch.tensor([1.0]), torch.tensor([1.0])) >> m.sample() # sample from a Kumaraswamy distribution with concentration alpha=1 and beta=1 tensor([ 0.1729])
- Parameters
concentration1 (float or Tensor) – 1st concentration parameter of the distribution (often referred to as alpha)
concentration0 (float or Tensor) – 2nd concentration parameter of the distribution (often referred to as beta)
-
distribution_cls¶ alias of
torch.distributions.kumaraswamy.Kumaraswamy
-
class
borch.distributions.LKJCholesky(dimension, concentration, validate_args=None, posterior=None)¶ LKJ distribution for lower Cholesky factor of correlation matrices. The distribution is controlled by
concentrationparameter \(\eta\) to make the probability of the correlation matrix \(M\) generated from a Cholesky factor propotional to \(\det(M)^{\eta - 1}\). Because of that, whenconcentration == 1, we have a uniform distribution over Cholesky factors of correlation matrices. Note that this distribution samples the Cholesky factor of correlation matrices and not the correlation matrices themselves and thereby differs slightly from the derivations in [1] for the LKJCorr distribution. For sampling, this uses the Onion method from [1] Section 3.L ~ LKJCholesky(dim, concentration) X = L @ L’ ~ LKJCorr(dim, concentration)
Example:
>> l = LKJCholesky(3, 0.5) >> l.sample() # l @ l.T is a sample of a correlation 3x3 matrix tensor([[ 1.0000, 0.0000, 0.0000], [ 0.3516, 0.9361, 0.0000], [-0.1899, 0.4748, 0.8593]])
- Parameters
dimension (dim) – dimension of the matrices
concentration (float or Tensor) – concentration/shape parameter of the distribution (often referred to as eta)
References
[1] Generating random correlation matrices based on vines and extended onion method, Daniel Lewandowski, Dorota Kurowicka, Harry Joe.
-
distribution_cls¶ alias of
torch.distributions.lkj_cholesky.LKJCholesky
-
class
borch.distributions.Laplace(loc, scale, validate_args=None, posterior=None)¶ Creates a Laplace distribution parameterized by
locandscale.Example:
>> m = Laplace(torch.tensor([0.0]), torch.tensor([1.0])) >> m.sample() # Laplace distributed with loc=0, scale=1 tensor([ 0.1046])
- Parameters
loc (float or Tensor) – mean of the distribution
scale (float or Tensor) – scale of the distribution
-
distribution_cls¶ alias of
torch.distributions.laplace.Laplace
-
class
borch.distributions.LogNormal(loc, scale, validate_args=None, posterior=None)¶ Creates a log-normal distribution parameterized by
locandscalewhere:X ~ Normal(loc, scale) Y = exp(X) ~ LogNormal(loc, scale)
Example:
>> m = LogNormal(torch.tensor([0.0]), torch.tensor([1.0])) >> m.sample() # log-normal distributed with mean=0 and stddev=1 tensor([ 0.1046])
- Parameters
loc (float or Tensor) – mean of log of distribution
scale (float or Tensor) – standard deviation of log of the distribution
-
distribution_cls¶ alias of
torch.distributions.log_normal.LogNormal
-
class
borch.distributions.Multinomial(total_count, probs=None, logits=None, validate_args=None, posterior=None)¶ Creates a Multinomial distribution parameterized by
total_countand eitherprobsorlogits(but not both). The innermost dimension ofprobsindexes over categories. All other dimensions index over batches.Note that
total_countneed not be specified if onlylog_prob()is called (see example below)Note
The probs argument must be non-negative, finite and have a non-zero sum, and it will be normalized to sum to 1 along the last dimension.
probswill return this normalized value. The logits argument will be interpreted as unnormalized log probabilities and can therefore be any real number. It will likewise be normalized so that the resulting probabilities sum to 1 along the last dimension.logitswill return this normalized value.sample()requires a single shared total_count for all parameters and samples.log_prob()allows different total_count for each parameter and sample.
Example:
>> m = Multinomial(100, torch.tensor([ 1., 1., 1., 1.])) >> x = m.sample() # equal probability of 0, 1, 2, 3 tensor([ 21., 24., 30., 25.]) >> Multinomial(probs=torch.tensor([1., 1., 1., 1.])).log_prob(x) tensor([-4.1338])
- Parameters
total_count (int) – number of trials
probs (Tensor) – event probabilities
logits (Tensor) – event log probabilities (unnormalized)
-
distribution_cls¶ alias of
torch.distributions.multinomial.Multinomial
-
class
borch.distributions.MultivariateNormal(loc, covariance_matrix=None, precision_matrix=None, scale_tril=None, validate_args=None, posterior=None)¶ Creates a multivariate normal (also called Gaussian) distribution parameterized by a mean vector and a covariance matrix.
The multivariate normal distribution can be parameterized either in terms of a positive definite covariance matrix \(\mathbf{\Sigma}\) or a positive definite precision matrix \(\mathbf{\Sigma}^{-1}\) or a lower-triangular matrix \(\mathbf{L}\) with positive-valued diagonal entries, such that \(\mathbf{\Sigma} = \mathbf{L}\mathbf{L}^\top\). This triangular matrix can be obtained via e.g. Cholesky decomposition of the covariance.
Example
>>>>> m = MultivariateNormal(torch.zeros(2), torch.eye(2)) >> m.sample() # normally distributed with mean=`[0,0]` and covariance_matrix=`I` tensor([-0.2102, -0.5429])
- Parameters
loc (Tensor) – mean of the distribution
covariance_matrix (Tensor) – positive-definite covariance matrix
precision_matrix (Tensor) – positive-definite precision matrix
scale_tril (Tensor) – lower-triangular factor of covariance, with positive-valued diagonal
Note
Only one of
covariance_matrixorprecision_matrixorscale_trilcan be specified.Using
scale_trilwill be more efficient: all computations internally are based onscale_tril. Ifcovariance_matrixorprecision_matrixis passed instead, it is only used to compute the corresponding lower triangular matrices using a Cholesky decomposition.-
distribution_cls¶ alias of
torch.distributions.multivariate_normal.MultivariateNormal
-
class
borch.distributions.NegativeBinomial(total_count, probs=None, logits=None, validate_args=None, posterior=None)¶ Creates a Negative Binomial distribution, i.e. distribution of the number of successful independent and identical Bernoulli trials before
total_countfailures are achieved. The probability of success of each Bernoulli trial isprobs.- Parameters
total_count (float or Tensor) – non-negative number of negative Bernoulli trials to stop, although the distribution is still valid for real valued count
probs (Tensor) – Event probabilities of success in the half open interval [0, 1)
logits (Tensor) – Event log-odds for probabilities of success
-
distribution_cls¶ alias of
torch.distributions.negative_binomial.NegativeBinomial
-
class
borch.distributions.Normal(loc, scale, validate_args=None, posterior=None)¶ Creates a normal (also called Gaussian) distribution parameterized by
locandscale.Example:
>> m = Normal(torch.tensor([0.0]), torch.tensor([1.0])) >> m.sample() # normally distributed with loc=0 and scale=1 tensor([ 0.1046])
- Parameters
loc (float or Tensor) – mean of the distribution (often referred to as mu)
scale (float or Tensor) – standard deviation of the distribution (often referred to as sigma)
-
distribution_cls¶ alias of
torch.distributions.normal.Normal
-
class
borch.distributions.OneHotCategorical(probs=None, logits=None, validate_args=None, posterior=None)¶ Creates a one-hot categorical distribution parameterized by
probsorlogits.Samples are one-hot coded vectors of size
probs.size(-1).Note
The probs argument must be non-negative, finite and have a non-zero sum, and it will be normalized to sum to 1 along the last dimension.
probswill return this normalized value. The logits argument will be interpreted as unnormalized log probabilities and can therefore be any real number. It will likewise be normalized so that the resulting probabilities sum to 1 along the last dimension.logitswill return this normalized value.See also:
torch.distributions.Categorical()for specifications ofprobsandlogits.Example:
>> m = OneHotCategorical(torch.tensor([ 0.25, 0.25, 0.25, 0.25 ])) >> m.sample() # equal probability of 0, 1, 2, 3 tensor([ 0., 0., 0., 1.])
- Parameters
probs (Tensor) – event probabilities
logits (Tensor) – event log probabilities (unnormalized)
-
distribution_cls¶ alias of
torch.distributions.one_hot_categorical.OneHotCategorical
-
class
borch.distributions.Pareto(scale, alpha, validate_args=None, posterior=None)¶ Samples from a Pareto Type 1 distribution.
Example:
>> m = Pareto(torch.tensor([1.0]), torch.tensor([1.0])) >> m.sample() # sample from a Pareto distribution with scale=1 and alpha=1 tensor([ 1.5623])
- Parameters
scale (float or Tensor) – Scale parameter of the distribution
alpha (float or Tensor) – Shape parameter of the distribution
-
distribution_cls¶ alias of
torch.distributions.pareto.Pareto
-
class
borch.distributions.PointMass(value, support, posterior=None)¶ Implements a PointMass distribution, it takes an unconstrained value and constrains it according to the provided support.
-
distribution_cls¶ alias of
borch.distributions.distributions.PointMass
-
-
class
borch.distributions.Poisson(rate, validate_args=None, posterior=None)¶ Creates a Poisson distribution parameterized by
rate, the rate parameter.Samples are nonnegative integers, with a pmf given by
\[\mathrm{rate}^k \frac{e^{-\mathrm{rate}}}{k!}\]Example:
>> m = Poisson(torch.tensor([4])) >> m.sample() tensor([ 3.])
- Parameters
rate (Number, Tensor) – the rate parameter
-
distribution_cls¶ alias of
torch.distributions.poisson.Poisson
-
class
borch.distributions.RandomVariable(validate_args=None, posterior=None)¶ Base class for a
RandomVariableprimitive used to model stochastic nodes. It merges atorch.tensor,torch.nn.Moduleand atorch.distribution.Distributions. It is not intended to be used directly but to be inherited from when creating aRandomVariable.When used it will act as a
torch.Tensorwith a sample drawn from the distribution with most of the methods from atorch.nn.Moduleand atorch.distribution.DistributionsExamples
>>> import torch >>> import borch
>>> class MyRV(RandomVariable): ... distribution_cls = torch.distributions.Normal ... def __init__(self, loc=1, scale=1): ... super().__init__() ... self.register_param_or_buffer("loc", loc) ... self.register_param_or_buffer("scale", scale) ... self() ... def _distribution(self): ... 'Call that creates the torch distribution' ... return self.distribution_cls( ... borch.as_tensor(self.loc), ... borch.as_tensor(self.scale) ... ) >>> rv = MyRV(torch.nn.Parameter(torch.tensor(1.)), 2.)
It works as a normal tensor >>> torch.exp(rv) > 0 tensor(True)
One can also use the tensor attribute directly >>> type(rv.tensor) <class ‘torch.Tensor’> >>> torch.exp(rv) == torch.exp(rv.tensor) tensor(True)
The random variable have all the functionality from the
torch.nn.Module>>> list(rv.parameters()) [Parameter containing: tensor(1., requires_grad=True)]-
cdf(value=None)¶ Returns the cumulative density/mass function evaluated at
value. :param value: :type value: Tensor
-
property
distribution¶ Create the
torch.distributions.Distributionequivalent
-
distribution_cls()¶ Creator for the
torch.distributions.Distribution
-
entropy()¶ Returns entropy of distribution, batched over batch_shape.
- Returns
Tensor of shape batch_shape.
-
forward()¶ Draw a sample from the distribution
-
property
has_enumerate_support¶ support enumerate
-
property
has_rsample¶ Check if rsample is implemented
-
icdf(value=None)¶ Returns the inverse cumulative density/mass function evaluated at
value. :param value: :type value: Tensor
-
log_prob(value=None)¶ Returns the log of the probability density/mass function evaluated at
value. :param value: :type value: Tensor
-
perplexity()¶ Returns perplexity of distribution, batched over batch_shape. :returns: Tensor of shape batch_shape.
-
rsample(sample_shape=torch.Size([]))¶ Generates a sample_shape shaped reparameterized sample or sample_shape shaped batch of reparameterized samples if the distribution parameters are batched.
-
sample(sample_shape=torch.Size([]))¶ Generates a sample_shape shaped sample or sample_shape shaped batch of samples if the distribution parameters are batched.
-
property
support¶ Returns a :class:
~torch.distributions.constraints.Constraintobject representing this distribution’s support.
-
property
validate_args¶ if the args should be validated when creating the distribution
-
-
class
borch.distributions.RelaxedBernoulli(temperature, probs=None, logits=None, validate_args=None, posterior=None)¶ Creates a RelaxedBernoulli distribution, parametrized by
temperature, and eitherprobsorlogits(but not both). This is a relaxed version of the Bernoulli distribution, so the values are in (0, 1), and has reparametrizable samples.Example:
>> m = RelaxedBernoulli(torch.tensor([2.2]), torch.tensor([0.1, 0.2, 0.3, 0.99])) >> m.sample() tensor([ 0.2951, 0.3442, 0.8918, 0.9021])
- Parameters
temperature (Tensor) – relaxation temperature
probs (Number, Tensor) – the probability of sampling 1
logits (Number, Tensor) – the log-odds of sampling 1
-
distribution_cls¶ alias of
torch.distributions.relaxed_bernoulli.RelaxedBernoulli
-
class
borch.distributions.RelaxedOneHotCategorical(temperature, probs=None, logits=None, validate_args=None, posterior=None)¶ Creates a RelaxedOneHotCategorical distribution parametrized by
temperature, and eitherprobsorlogits. This is a relaxed version of theOneHotCategoricaldistribution, so its samples are on simplex, and are reparametrizable.Example:
>> m = RelaxedOneHotCategorical(torch.tensor([2.2]), torch.tensor([0.1, 0.2, 0.3, 0.4])) >> m.sample() tensor([ 0.1294, 0.2324, 0.3859, 0.2523])
- Parameters
temperature (Tensor) – relaxation temperature
probs (Tensor) – event probabilities
logits (Tensor) – unnormalized log probability for each event
-
distribution_cls¶ alias of
torch.distributions.relaxed_categorical.RelaxedOneHotCategorical
-
class
borch.distributions.StudentT(df, loc, scale, validate_args=None, posterior=None)¶ Creates a Student’s t-distribution parameterized by degree of freedom
df, meanlocand scalescale.Example:
>> m = StudentT(torch.tensor([2.0])) >> m.sample() # Student's t-distributed with degrees of freedom=2 tensor([ 0.1046])
- Parameters
df (float or Tensor) – degrees of freedom
loc (float or Tensor) – mean of the distribution
scale (float or Tensor) – scale of the distribution
-
distribution_cls¶ alias of
torch.distributions.studentT.StudentT
-
class
borch.distributions.TransformedDistribution(base_distribution, transforms, validate_args=None, posterior=None)¶ Extension of the Distribution class, which applies a sequence of Transforms to a base distribution. Let f be the composition of transforms applied:
X ~ BaseDistribution Y = f(X) ~ TransformedDistribution(BaseDistribution, f) log p(Y) = log p(X) + log |det (dX/dY)|
Note that the
.event_shapeof aTransformedDistributionis the maximum shape of its base distribution and its transforms, since transforms can introduce correlations among events.An example for the usage of
TransformedDistributionwould be:# Building a Logistic Distribution # X ~ Uniform(0, 1) # f = a + b * logit(X) # Y ~ f(X) ~ Logistic(a, b) base_distribution = Uniform(0, 1) transforms = [SigmoidTransform().inv, AffineTransform(loc=a, scale=b)] logistic = TransformedDistribution(base_distribution, transforms)
For more examples, please look at the implementations of
Gumbel,HalfCauchy,HalfNormal,LogNormal,Pareto,Weibull,RelaxedBernoulliandRelaxedOneHotCategorical-
distribution_cls¶ alias of
torch.distributions.transformed_distribution.TransformedDistribution
-
-
class
borch.distributions.Uniform(low, high, validate_args=None, posterior=None)¶ Generates uniformly distributed random samples from the half-open interval
[low, high).Example:
>> m = Uniform(torch.tensor([0.0]), torch.tensor([5.0])) >> m.sample() # uniformly distributed in the range [0.0, 5.0) tensor([ 2.3418])
- Parameters
low (float or Tensor) – lower range (inclusive).
high (float or Tensor) – upper range (exclusive).
-
distribution_cls¶ alias of
torch.distributions.uniform.Uniform
-
class
borch.distributions.VonMises(loc, concentration, validate_args=None, posterior=None)¶ A circular von Mises distribution.
This implementation uses polar coordinates. The
locandvalueargs can be any real number (to facilitate unconstrained optimization), but are interpreted as angles modulo 2 pi.- Example::
>> m = dist.VonMises(torch.tensor([1.0]), torch.tensor([1.0])) >> m.sample() # von Mises distributed with loc=1 and concentration=1 tensor([1.9777])
- Parameters
loc (torch.Tensor) – an angle in radians.
concentration (torch.Tensor) – concentration parameter
-
distribution_cls¶ alias of
torch.distributions.von_mises.VonMises
-
class
borch.distributions.Weibull(scale, concentration, validate_args=None, posterior=None)¶ Samples from a two-parameter Weibull distribution.
Example
>>>>> m = Weibull(torch.tensor([1.0]), torch.tensor([1.0])) >> m.sample() # sample from a Weibull distribution with scale=1, concentration=1 tensor([ 0.4784])
- Parameters
scale (float or Tensor) – Scale parameter of distribution (lambda).
concentration (float or Tensor) – Concentration parameter of distribution (k/shape).
-
distribution_cls¶ alias of
torch.distributions.weibull.Weibull
-
borch.distributions.as_tensor(val)¶ Convert val to a
torch.Tensorif possibleExamples
>>> as_tensor(1.) tensor(1.) >>> as_tensor('hello') 'hello'
-
borch.distributions.disable_doctests(string)¶ Disable any doctests present in
string.Notes
This should be done by appending the modifier
# doctest: +SKIPat the correct position, but this is quite involved. The current fix is to replace and occurrence of {>>>,…} with {>>,..} and start the docs with an empty >>> to make it render correctly.
-
borch.distributions.kl_divergence(p_dist, q_dist)¶ Compute Kullback-Leibler divergence \(KL(p_dist \| q_dist)\) between two distributions.
\[KL(p_dist \| q_dist) = \int p_dist(x) \log\frac {p_dist(x)} {q_dist(x)} \,dx\]- Parameters
p_dist (Distribution, RandomVariable) – A
Distributionor RandomVariable.q_dist (Distribution, RandomVariable) – A
Distributionor RandomVariable.
- Returns
A batch of KL divergences of shape batch_shape.
- Return type
Tensor
- Raises
NotImplementedError – If the distribution types have not been registered via
register_kl().
-
borch.distributions.kl_exists(p_dist: Union[torch.distributions.distribution.Distribution, borch.random_variable.RandomVariable], q_dist: Union[torch.distributions.distribution.Distribution, borch.random_variable.RandomVariable]) → bool¶ Determine whether the kl divergence is implemented between
p_distandq_dist.- Parameters
p_dist – Prior distribution.
q_dist – Approximating distribution.
- Returns
whether kl divergence is defined.
- Return type
True/False