symbol.random

Random distribution generator Symbol API of MXNet.

Functions

uniform([low, high, shape, dtype])

Draw random samples from a uniform distribution.

normal([loc, scale, shape, dtype])

Draw random samples from a normal (Gaussian) distribution.

randn(*shape, **kwargs)

Draw random samples from a normal (Gaussian) distribution.

poisson([lam, shape, dtype])

Draw random samples from a Poisson distribution.

exponential([scale, shape, dtype])

Draw samples from an exponential distribution.

gamma([alpha, beta, shape, dtype])

Draw random samples from a gamma distribution.

categorical(data[, shape, get_prob, dtype])

Concurrent sampling from multiple categorical distributions.

multinomial([n, p, shape, dtype])

Concurrent sampling from multiple multinomial distributions.

binomial([n, p, shape, dtype])

Draw random samples from a binomial distribution.

negative_binomial([k, p, shape, dtype])

Draw random samples from a negative binomial distribution.

generalized_negative_binomial([mu, alpha, …])

Draw random samples from a generalized negative binomial distribution.

shuffle(data, **kwargs)

Shuffle the elements randomly.

randint(low, high[, shape, dtype])

Draw random samples from a discrete uniform distribution.

binomial_like([name, attr, out])

param name

Name of the resulting symbol.

exponential_like([data, lam, name, attr, out])

Draw random samples from an exponential distribution according to the input array shape.

gamma_like([data, alpha, beta, name, attr, out])

Draw random samples from a gamma distribution according to the input array shape.

generalized_negative_binomial_like([data, …])

Draw random samples from a generalized negative binomial distribution according to the input array shape.

negative_binomial_like([data, k, p, name, …])

Draw random samples from a negative binomial distribution according to the input array shape.

normal_like([data, loc, scale, name, attr, out])

Draw random samples from a normal (Gaussian) distribution according to the input array shape.

poisson_like([data, lam, name, attr, out])

Draw random samples from a Poisson distribution according to the input array shape.

uniform_like([data, low, high, name, attr, out])

Draw random samples from a uniform distribution according to the input array shape.

uniform(low=0, high=1, shape=_Null, dtype=_Null, **kwargs)[source]

Draw random samples from a uniform distribution.

Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high).

Parameters
  • low (float or Symbol, optional) – Lower boundary of the output interval. All values generated will be greater than or equal to low. The default value is 0.

  • high (float or Symbol, optional) – Upper boundary of the output interval. All values generated will be less than high. The default value is 1.0.

  • shape (int or tuple of ints, optional) – The number of samples to draw. If shape is, e.g., (m, n) and low and high are scalars, output shape will be (m, n). If low and high are Symbols with shape, e.g., (x, y), then output will have shape (x, y, m, n), where m*n samples are drawn for each [low, high) pair.

  • dtype ({'float16', 'float32', 'float64'}, optional) – Data type of output samples. Default is ‘float32’

Returns

If input shape has dimensions, e.g., (m, n), and low and high are scalars, returned Symbol will resolve to shape (m, n). If low and high are Symbols with shape, e.g., (x, y), returned Symbol will have shape (x, y, m, n), where m*n samples are drawn for each [low, high) pair.

Return type

Symbol

normal(loc=0, scale=1, shape=_Null, dtype=_Null, **kwargs)[source]

Draw random samples from a normal (Gaussian) distribution.

Samples are distributed according to a normal distribution parametrized by loc (mean) and scale (standard deviation).

Parameters
  • loc (float or Symbol, optional) – Mean (centre) of the distribution.

  • scale (float or Symbol, optional) – Standard deviation (spread or width) of the distribution.

  • shape (int or tuple of ints, optional) – The number of samples to draw. If shape is, e.g., (m, n) and loc and scale are scalars, output shape will be (m, n). If loc and scale are Symbols with shape, e.g., (x, y), then output will have shape (x, y, m, n), where m*n samples are drawn for each [loc, scale) pair.

  • dtype ({'float16', 'float32', 'float64'}, optional) – Data type of output samples. Default is ‘float32’

Returns

If input shape has dimensions, e.g., (m, n), and loc and scale are scalars, returned Symbol will resolve to shape (m, n). If loc and scale are Symbols with shape, e.g., (x, y), returned Symbol will resolve to shape (x, y, m, n), where m*n samples are drawn for each [loc, scale) pair.

Return type

Symbol

randn(*shape, **kwargs)[source]

Draw random samples from a normal (Gaussian) distribution.

Samples are distributed according to a normal distribution parametrized by loc (mean) and scale (standard deviation).

Parameters
  • loc (float or Symbol, optional) – Mean (centre) of the distribution.

  • scale (float or Symbol, optional) – Standard deviation (spread or width) of the distribution.

  • shape (int or tuple of ints) – The number of samples to draw. If shape is, e.g., (m, n) and loc and scale are scalars, output shape will be (m, n). If loc and scale are NDArrays with shape, e.g., (x, y), then output will have shape (x, y, m, n), where m*n samples are drawn for each [loc, scale) pair.

  • dtype ({'float16', 'float32', 'float64'}, optional) – Data type of output samples. Default is ‘float32’

poisson(lam=1, shape=_Null, dtype=_Null, **kwargs)[source]

Draw random samples from a Poisson distribution.

Samples are distributed according to a Poisson distribution parametrized by lambda (rate). Samples will always be returned as a floating point data type.

Parameters
  • lam (float or Symbol, optional) – Expectation of interval, should be >= 0.

  • shape (int or tuple of ints, optional) – The number of samples to draw. If shape is, e.g., (m, n) and lam is a scalar, output shape will be (m, n). If lam is an Symbol with shape, e.g., (x, y), then output will have shape (x, y, m, n), where m*n samples are drawn for each entry in lam.

  • dtype ({'float16', 'float32', 'float64'}, optional) – Data type of output samples. Default is ‘float32’

Returns

If input shape has dimensions, e.g., (m, n), and lam is a scalar, output shape will be (m, n). If lam is an Symbol with shape, e.g., (x, y), then output will have shape (x, y, m, n), where m*n samples are drawn for each entry in lam.

Return type

Symbol

exponential(scale=1, shape=_Null, dtype=_Null, **kwargs)[source]

Draw samples from an exponential distribution.

Its probability density function is

\[f(x; \frac{1}{\beta}) = \frac{1}{\beta} \exp(-\frac{x}{\beta}),\]

for x > 0 and 0 elsewhere. beta is the scale parameter, which is the inverse of the rate parameter lambda = 1/beta.

Parameters
  • scale (float or Symbol, optional) – The scale parameter, beta = 1/lambda.

  • shape (int or tuple of ints, optional) – The number of samples to draw. If shape is, e.g., (m, n) and scale is a scalar, output shape will be (m, n). If scale is an Symbol with shape, e.g., (x, y), then output will have shape (x, y, m, n), where m*n samples are drawn for each entry in scale.

  • dtype ({'float16', 'float32', 'float64'}, optional) – Data type of output samples. Default is ‘float32’

Returns

If input shape has dimensions, e.g., (m, n), and scale is a scalar, returned Symbol will have shape (m, n). If scale is a Symbol with shape, e.g., (x, y), returned Symbol will resolve to shape (x, y, m, n), where m*n samples are drawn for each entry in scale.

Return type

Symbol

gamma(alpha=1, beta=1, shape=_Null, dtype=_Null, **kwargs)[source]

Draw random samples from a gamma distribution.

Samples are distributed according to a gamma distribution parametrized by alpha (shape) and beta (scale).

Parameters
  • alpha (float or Symbol, optional) – The shape of the gamma distribution. Should be greater than zero.

  • beta (float or Symbol, optional) – The scale of the gamma distribution. Should be greater than zero. Default is equal to 1.

  • shape (int or tuple of ints, optional) – The number of samples to draw. If shape is, e.g., (m, n) and alpha and beta are scalars, output shape will be (m, n). If alpha and beta are Symbols with shape, e.g., (x, y), then output will have shape (x, y, m, n), where m*n samples are drawn for each [alpha, beta) pair.

  • dtype ({'float16', 'float32', 'float64'}, optional) – Data type of output samples. Default is ‘float32’

Returns

If input shape has dimensions, e.g., (m, n) and alpha and beta are scalars, returned Symbol will resolve to shape (m, n). If alpha and beta are Symbols with shape, e.g., (x, y), returned Symbol will resolve to shape (x, y, m, n), where m*n samples are drawn for each [alpha, beta) pair.

Return type

Symbol

categorical(data, shape=_Null, get_prob=True, dtype='int32', **kwargs)[source]

Concurrent sampling from multiple categorical distributions.

Note

The input distribution must be normalized, i.e. data must sum to 1 along its last dimension.

Parameters
  • data (Symbol) – An n dimensional array whose last dimension has length k, where k is the number of possible outcomes of each categorical distribution. For example, data with shape (m, n, k) specifies m*n categorical distributions each with k possible outcomes.

  • shape (int or tuple of ints, optional) – The number of samples to draw from each distribution. If shape is empty one sample will be drawn from each distribution.

  • get_prob (bool, optional) – If true, a second array containing log likelihood of the drawn samples will also be returned. This is usually used for reinforcement learning, where you can provide reward as head gradient w.r.t. this array to estimate gradient.

  • dtype (str or numpy.dtype, optional) – Data type of the sample output array. The default is int32. Note that the data type of the log likelihood array is the same with that of data.

Returns

For input data with n dimensions and shape (d1, d2, …, dn-1, k), and input shape with shape (s1, s2, …, sx), returns a Symbol that resovles to shape (d1, d2, … dn-1, s1, s2, …, sx). The s1, s2, … sx dimensions of the returned Symbol’s resolved value will consist of 0-indexed values sampled from each respective categorical distribution provided in the k dimension of data.

For the case n`=1, and `x`=1 (one shape dimension), returned Symbol will resolve to shape `(s1,).

If get_prob is set to True, this function returns a Symbol that will resolve to a list of outputs: [ndarray_output, log_likelihood_output], where log_likelihood_output will resolve to the same shape as the sampled outputs in ndarray_output.

Return type

Symbol

multinomial(n=[1], p=[[1.0]], shape=_Null, dtype='float32', **kwargs)[source]

Concurrent sampling from multiple multinomial distributions.

Note

The input distribution must be normalized, i.e. p must sum to 1 along its last dimension.

Parameters
  • n (Symbol) – An n dimensional array containing the number of trials of each multinomial distribution.

  • p (Symbol) – An n+1 dimensional array containing the probabilities of each multinomial distribution. Its last dimension has length k, where k is the number of possible outcomes of each multinomial distribution. For example, p with shape (m, n, k) specifies m*n multinomial distributions each with k possible outcomes.

  • shape (int or tuple of ints, optional) – The number of samples to draw from each distribution. If shape is empty one sample will be drawn from each distribution.

  • dtype ({'float16', 'float32', 'float64'}, optional) – Data type of output samples. Default is ‘float32’

Returns

If input shape has shape, e.g., (m, n) and n and p are a scalar and an array of length k respectively, output shape will be (m, n, k). If n and p are NDArrays with shape, e.g., (x, y) and (x, y, k), then output will have shape (x, y, m, n, k), where m*n samples are drawn for each [n, p) pair.

Return type

Symbol

binomial(n=1, p=0.5, shape=_Null, dtype=_Null, **kwargs)[source]

Draw random samples from a binomial distribution.

Samples are distributed according to a binomial distribution parametrized by n (number of trials) and p (success probability).

Parameters
  • n (float or Symbol, optional) – Number of experiments, > 0.

  • p (float or Symbol, optional) – Success probability in each experiment, >= 0 and <= 1.

  • shape (int or tuple of ints, optional) – The number of samples to draw. If shape is, e.g., (m, n) and n and p are scalars, output shape will be (m, n). If n and p are NDArrays with shape, e.g., (x, y), then output will have shape (x, y, m, n), where m*n samples are drawn for each [n, p) pair.

  • dtype ({'float16', 'float32', 'float64'}, optional) – Data type of output samples. Default is ‘float32’

Returns

If input shape has shape, e.g., (m, n) and n and p are scalars, output shape will be (m, n). If n and p are NDArrays with shape, e.g., (x, y), then output will have shape (x, y, m, n), where m*n samples are drawn for each [n, p) pair.

Return type

Symbol

negative_binomial(k=1, p=1, shape=_Null, dtype=_Null, **kwargs)[source]

Draw random samples from a negative binomial distribution.

Samples are distributed according to a negative binomial distribution parametrized by k (limit of unsuccessful experiments) and p (failure probability in each experiment). Samples will always be returned as a floating point data type.

Parameters
  • k (float or Symbol, optional) – Limit of unsuccessful experiments, > 0.

  • p (float or Symbol, optional) – Failure probability in each experiment, >= 0 and <=1.

  • shape (int or tuple of ints) – The number of samples to draw. If shape is, e.g., (m, n) and k and p are scalars, output shape will be (m, n). If k and p are Symbols with shape, e.g., (x, y), then output will have shape (x, y, m, n), where m*n samples are drawn for each [k, p) pair.

  • dtype ({'float16', 'float32', 'float64'}, optional) – Data type of output samples. Default is ‘float32’

Returns

If input shape has dimensions, e.g., (m, n), and k and p are scalars, returned Symbol will resolve to shape (m, n). If k and p are Symbols with shape, e.g., (x, y), returned Symbol will resolve to shape (x, y, m, n), where m*n samples are drawn for each [k, p) pair.

Return type

Symbol

generalized_negative_binomial(mu=1, alpha=1, shape=_Null, dtype=_Null, **kwargs)[source]

Draw random samples from a generalized negative binomial distribution.

Samples are distributed according to a generalized negative binomial distribution parametrized by mu (mean) and alpha (dispersion). alpha is defined as 1/k where k is the failure limit of the number of unsuccessful experiments (generalized to real numbers). Samples will always be returned as a floating point data type.

Parameters
  • mu (float or Symbol, optional) – Mean of the negative binomial distribution.

  • alpha (float or Symbol, optional) – Alpha (dispersion) parameter of the negative binomial distribution.

  • shape (int or tuple of ints, optional) – The number of samples to draw. If shape is, e.g., (m, n) and mu and alpha are scalars, output shape will be (m, n). If mu and alpha are Symbols with shape, e.g., (x, y), then output will have shape (x, y, m, n), where m*n samples are drawn for each [mu, alpha) pair.

  • dtype ({'float16', 'float32', 'float64'}, optional) – Data type of output samples. Default is ‘float32’

Returns

If input shape has dimensions, e.g., (m, n), and mu and alpha are scalars, returned Symbol will resolve to shape (m, n). If mu and alpha are Symbols with shape, e.g., (x, y), returned Symbol will resolve to shape (x, y, m, n), where m*n samples are drawn for each [mu, alpha) pair.

Return type

Symbol

shuffle(data, **kwargs)[source]

Shuffle the elements randomly.

This shuffles the array along the first axis. The order of the elements in each subarray does not change. For example, if a 2D array is given, the order of the rows randomly changes, but the order of the elements in each row does not change.

Parameters

data (NDArray) – Input data array.

Returns

A new symbol representing the shuffled version of input data.

Return type

Symbol

Examples

>>> data = mx.nd.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
>>> a = mx.sym.Variable('a')
>>> b = mx.sym.random.shuffle(a)
>>> b.eval(a=data)
[[ 0.  1.  2.]
 [ 6.  7.  8.]
 [ 3.  4.  5.]]
<NDArray 2x3 @cpu(0)>
>>> b.eval(a=data)
[[ 3.  4.  5.]
 [ 0.  1.  2.]
 [ 6.  7.  8.]]
<NDArray 2x3 @cpu(0)>
randint(low, high, shape=_Null, dtype=_Null, **kwargs)[source]

Draw random samples from a discrete uniform distribution.

Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high).

Parameters
  • low (int, required) – Lower boundary of the output interval. All values generated will be greater than or equal to low.

  • high (int, required) – Upper boundary of the output interval. All values generated will be less than high.

  • shape (int or tuple of ints, optional) – The number of samples to draw. If shape is, e.g., (m, n) and low and high are scalars, output shape will be (m, n).

  • dtype ({'int32', 'int64'}, optional) – Data type of output samples. Default is ‘int32’

Returns

If input shape has dimensions, e.g., (m, n), and low and high are scalars, returned Symbol will resolve to shape (m, n).

Return type

Symbol

binomial_like(name=None, attr=None, out=None, **kwargs)
Parameters

name (string, optional.) – Name of the resulting symbol.

Returns

The result symbol.

Return type

Symbol

exponential_like(data=None, lam=_Null, name=None, attr=None, out=None, **kwargs)

Draw random samples from an exponential distribution according to the input array shape.

Samples are distributed according to an exponential distribution parametrized by lambda (rate).

Example:

exponential(lam=4, data=ones(2,2)) = [[ 0.0097189 ,  0.08999364],
                                      [ 0.04146638,  0.31715935]]

Defined in /work/mxnet/src/operator/random/sample_op.cc:L263

Parameters
  • lam (float, optional, default=1) – Lambda parameter (rate) of the exponential distribution.

  • data (Symbol) – The input

  • name (string, optional.) – Name of the resulting symbol.

Returns

The result symbol.

Return type

Symbol

gamma_like(data=None, alpha=_Null, beta=_Null, name=None, attr=None, out=None, **kwargs)

Draw random samples from a gamma distribution according to the input array shape.

Samples are distributed according to a gamma distribution parametrized by alpha (shape) and beta (scale).

Example:

gamma(alpha=9, beta=0.5, data=ones(2,2)) = [[ 7.10486984,  3.37695289],
                                            [ 3.91697288,  3.65933681]]

Defined in /work/mxnet/src/operator/random/sample_op.cc:L251

Parameters
  • alpha (float, optional, default=1) – Alpha parameter (shape) of the gamma distribution.

  • beta (float, optional, default=1) – Beta parameter (scale) of the gamma distribution.

  • data (Symbol) – The input

  • name (string, optional.) – Name of the resulting symbol.

Returns

The result symbol.

Return type

Symbol

generalized_negative_binomial_like(data=None, mu=_Null, alpha=_Null, name=None, attr=None, out=None, **kwargs)

Draw random samples from a generalized negative binomial distribution according to the input array shape.

Samples are distributed according to a generalized negative binomial distribution parametrized by mu (mean) and alpha (dispersion). alpha is defined as 1/k where k is the failure limit of the number of unsuccessful experiments (generalized to real numbers). Samples will always be returned as a floating point data type.

Example:

generalized_negative_binomial(mu=2.0, alpha=0.3, data=ones(2,2)) = [[ 2.,  1.],
                                                                    [ 6.,  4.]]

Defined in /work/mxnet/src/operator/random/sample_op.cc:L307

Parameters
  • mu (float, optional, default=1) – Mean of the negative binomial distribution.

  • alpha (float, optional, default=1) – Alpha (dispersion) parameter of the negative binomial distribution.

  • data (Symbol) – The input

  • name (string, optional.) – Name of the resulting symbol.

Returns

The result symbol.

Return type

Symbol

negative_binomial_like(data=None, k=_Null, p=_Null, name=None, attr=None, out=None, **kwargs)

Draw random samples from a negative binomial distribution according to the input array shape.

Samples are distributed according to a negative binomial distribution parametrized by k (limit of unsuccessful experiments) and p (failure probability in each experiment). Samples will always be returned as a floating point data type.

Example:

negative_binomial(k=3, p=0.4, data=ones(2,2)) = [[ 4.,  7.],
                                                 [ 2.,  5.]]

Defined in /work/mxnet/src/operator/random/sample_op.cc:L290

Parameters
  • k (int, optional, default='1') – Limit of unsuccessful experiments.

  • p (float, optional, default=1) – Failure probability in each experiment.

  • data (Symbol) – The input

  • name (string, optional.) – Name of the resulting symbol.

Returns

The result symbol.

Return type

Symbol

normal_like(data=None, loc=_Null, scale=_Null, name=None, attr=None, out=None, **kwargs)

Draw random samples from a normal (Gaussian) distribution according to the input array shape.

Samples are distributed according to a normal distribution parametrized by loc (mean) and scale (standard deviation).

Example:

normal(loc=0, scale=1, data=ones(2,2)) = [[ 1.89171135, -1.16881478],
                                          [-1.23474145,  1.55807114]]

Defined in /work/mxnet/src/operator/random/sample_op.cc:L239

Parameters
  • loc (float, optional, default=0) – Mean of the distribution.

  • scale (float, optional, default=1) – Standard deviation of the distribution.

  • data (Symbol) – The input

  • name (string, optional.) – Name of the resulting symbol.

Returns

The result symbol.

Return type

Symbol

poisson_like(data=None, lam=_Null, name=None, attr=None, out=None, **kwargs)

Draw random samples from a Poisson distribution according to the input array shape.

Samples are distributed according to a Poisson distribution parametrized by lambda (rate). Samples will always be returned as a floating point data type.

Example:

poisson(lam=4, data=ones(2,2)) = [[ 5.,  2.],
                                  [ 4.,  6.]]

Defined in /work/mxnet/src/operator/random/sample_op.cc:L276

Parameters
  • lam (float, optional, default=1) – Lambda parameter (rate) of the Poisson distribution.

  • data (Symbol) – The input

  • name (string, optional.) – Name of the resulting symbol.

Returns

The result symbol.

Return type

Symbol

uniform_like(data=None, low=_Null, high=_Null, name=None, attr=None, out=None, **kwargs)

Draw random samples from a uniform distribution according to the input array shape.

Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high).

Example:

uniform(low=0, high=1, data=ones(2,2)) = [[ 0.60276335,  0.85794562],
                                          [ 0.54488319,  0.84725171]]

Defined in /work/mxnet/src/operator/random/sample_op.cc:L226

Parameters
  • low (float, optional, default=0) – Lower bound of the distribution.

  • high (float, optional, default=1) – Upper bound of the distribution.

  • data (Symbol) – The input

  • name (string, optional.) – Name of the resulting symbol.

Returns

The result symbol.

Return type

Symbol