sandbox.rng_mrg – MRG random number generator

API

Implementation of MRG31k3p random number generator for Theano.

Generator code in SSJ package (L’Ecuyer & Simard).http://www.iro.umontreal.ca/~simardr/ssj/indexe.html

The MRG31k3p algorithm was published in:

  • L’Ecuyer and R. Touzin, Fast Combined Multiple Recursive Generators with Multipliers of the form a = +/- 2^d +/- 2^e, Proceedings of the 2000 Winter Simulation Conference, Dec. 2000, 683-689. The conception of the multi-stream from MRG31k3p was published in:

  • L’Ecuyer and R. Simard and E. Jack Chen and W. David Kelton, An Object-Oriented Random-Number Package with Many Long Streams and Substreams, Operations Research, volume 50, number 6, 2002, 1073-1075.

  • class theano.sandbox.rng_mrg.DotModulo[source]

  • Efficient and numerically stable implementation of a dot product followedby a modulo operation. This performs the same function as matVecModM.

We do this 2 times on 2 triple inputs and concatenating the output.

  • class theano.sandbox.rngmrg.MRG_RandomStreams(_seed=12345)[source]
  • Module component with similar interface to numpy.random(numpy.random.RandomState).

Parameters:seed (int or list of 6 int) – A default seed to initialize the random state.If a single int is given, it will be replicated 6 times.The first 3 values of the seed must all be less than M1 = 2147483647,and not all 0; and the last 3 values must all be less thanM2 = 2147462579, and not all 0.

  • choice(size=1, a=None, replace=True, p=None, ndim=None, dtype='int64', nstreams=None, **kwargs)[source]
  • Sample size times from a multinomial distribution defined byprobabilities p, and returns the indices of the sampled elements.Sampled values are between 0 and p.shape[1]-1.Only sampling without replacement is implemented for now.

Parameters:

  1. - **size** (_integer__ or __integer tensor__ (__default 1__)_) The number of samples. It should be between 1 and _p.shape[1]-1_.
  2. - **a** (_int__ or __None__ (__default None__)_) For now, a should be None. This function will samplevalues between 0 and _p.shape[1]-1_. When a != None will beimplemented, if _a_ is a scalar, the samples are drawn from therange 0,...,a-1. We default to 2 as to have the same interface asRandomStream.
  3. - **replace** (_bool__ (__default True__)_) Whether the sample is with or without replacement.Only replace=False is implemented for now.
  4. - **p** (_2d numpy array__ or __theano tensor_) the probabilities of the distribution, corresponding to values0 to _p.shape[1]-1_.
  5. - **Example** (_p =__ [__[__98__, __01__, __01__]__, __[__01__, __49__, __50__]__] __and size=1 will_)
  6. - **result in**** [****[****0****]****,****[****2****]****]**** When setting size=2****, ****this** (_probably_)
  7. - **probably result in**** [****[****0****,****1****]****,****[****2****,****1****]****]** (_will_)

Notes

-ndim is only there keep the same signature as otheruniform, binomial, normal, etc.

-Does not do any value checking on pvals, i.e. there is nocheck that the elements are non-negative, less than 1, orsum to 1. passing pvals = [[-2., 2.]] will result insampling [[0, 0]]

-Only replace=False is implemented for now.

  • getsubstream_rstates(args, *kwargs_)[source]
  • Initialize a matrix in which each row is a MRG stream state,and they are spaced by 2**72 samples.

  • inc_rstate()[source]

  • Update self.rstate to be skipped 2^134 steps forward to the next streamstart.

  • multinomial(size=None, n=1, pvals=None, ndim=None, dtype='int64', nstreams=None, **kwargs)[source]

  • Sample n (n needs to be >= 1, default 1) times from a multinomialdistribution defined by probabilities pvals.

Example : pvals = [[.98, .01, .01], [.01, .49, .50]] and n=1 willprobably result in [[1,0,0],[0,0,1]]. When setting n=2, thiswill probably result in [[2,0,0],[0,1,1]].

Notes

-size and ndim are only there keep the same signature as otheruniform, binomial, normal, etc.TODO : adapt multinomial to take that into account

-Does not do any value checking on pvals, i.e. there is nocheck that the elements are non-negative, less than 1, orsum to 1. passing pvals = [[-2., 2.]] will result insampling [[0, 0]]

  • normal(size, avg=0.0, std=1.0, ndim=None, dtype=None, nstreams=None, truncate=False, **kwargs)[source]
  • Sample a tensor of values from a normal distribution.

Parameters:

  1. - **size** (_int_vector_like_) Array dimensions for the output tensor.
  2. - **avg** (_float_like__, __optional_) The mean value for the truncated normal to sample from (defaults to 0.0).
  3. - **std** (_float_like__, __optional_) The standard deviation for the truncated normal to sample from (defaults to 1.0).
  4. - **truncate** (_bool__, __optional_) Truncates the normal distribution at 2 standard deviations if True (defaults to False).When this flag is set, the standard deviation of the result will be less than the one specified.
  5. - **ndim** (_int__, __optional_) The number of dimensions for the output tensor (defaults to None).This argument is necessary if the size argument is ambiguous on the number of dimensions.
  6. - **dtype** (_str__, __optional_) The data-type for the output tensor. If not specified,the dtype is inferred from avg and std, but it is at least as precise as floatX.
  7. - **kwargs** Other keyword arguments for random number generation (see uniform).Returns:

samples – A Theano tensor of samples randomly drawn from a normal distribution. Return type: TensorVariable

  • seed(seed=None)[source]
  • Re-initialize each random stream.

Parameters:seed (None or integer in range 0 to 2**30) – Each random stream will be assigned a unique state that dependsdeterministically on this value.Returns:Return type:None

  • truncatednormal(_size, avg=0.0, std=1.0, ndim=None, dtype=None, nstreams=None, **kwargs)[source]
  • Sample a tensor of values from a symmetrically truncated normal distribution.

Parameters:

  1. - **size** (_int_vector_like_) Array dimensions for the output tensor.
  2. - **avg** (_float_like__, __optional_) The mean value for the truncated normal to sample from (defaults to 0.0).
  3. - **std** (_float_like__, __optional_) The standard deviation for the truncated normal to sample from (defaults to 1.0).
  4. - **ndim** (_int__, __optional_) The number of dimensions for the output tensor (defaults to None).This argument is necessary if the size argument is ambiguous on the number of dimensions.
  5. - **dtype** (_str__, __optional_) The data-type for the output tensor. If not specified,the dtype is inferred from avg and std, but it is at least as precise as floatX.
  6. - **kwargs** Other keyword arguments for random number generation (see uniform).Returns:

samples – A Theano tensor of samples randomly drawn from a truncated normal distribution. Return type: TensorVariable

See also

normal()

  • uniform(size, low=0.0, high=1.0, ndim=None, dtype=None, nstreams=None, **kwargs)[source]
  • Sample a tensor of given size whose element from a uniformdistribution between low and high.

If the size argument is ambiguous on the number of dimensions,ndim may be a plain integer to supplement the missing information.

Parameters:

  1. - **low** Lower bound of the interval on which values are sampled.If the <code>dtype</code> arg is provided, <code>low</code> will be cast intodtype. This bound is excluded.
  2. - **high** Higher bound of the interval on which values are sampled.If the <code>dtype</code> arg is provided, <code>high</code> will be cast intodtype. This bound is excluded.
  3. - **size** Can be a list of integer or Theano variable (ex: the shapeof other Theano Variable).
  4. - **dtype** The output data type. If dtype is not specified, it will beinferred from the dtype of low and high, but will be atleast as precise as floatX.
  • theano.sandbox.rngmrg.guess_n_streams(_size, warn=False)[source]
  • Return a guess at a good number of streams.

Parameters:warn (bool, optional) – If True, warn when a guess cannot be made (in which case wereturn 60 * 256).

  • theano.sandbox.rngmrg.multMatVect(_v, A, m1, B, m2)[source]
  • Multiply the first half of v by A with a modulo of m1 and the second halfby B with a modulo of m2.

Notes

The parameters of dot_modulo are passed implicitly because passing themexplicitly takes more time than running the function’s C-code.