def binomial(random_state, size=None, n=1, p=0.5, ndim=None, dtype='int64', prob=None): """ Sample n times with probability of success prob for each trial, return the number of successes. If the size argument is ambiguous on the number of dimensions, ndim may be a plain integer to supplement the missing information. If size is None, the output shape will be determined by the shapes of n and prob. """ if prob is not None: p = prob print >> sys.stderr, "DEPRECATION WARNING: the parameter prob to the binomal fct have been renamed to p to have the same name as numpy." n = tensor.as_tensor_variable(n) p = tensor.as_tensor_variable(p) ndim, size, bcast = _infer_ndim_bcast(ndim, size, n, p) if n.dtype == 'int64': ### THIS WORKS AROUND A NUMPY BUG on 32bit machine ### Erase when the following works on a 32bit machine: ### numpy.random.binomial( # n=numpy.asarray([2,3,4], dtype='int64'), # p=numpy.asarray([.1, .2, .3], dtype='float64')) n = tensor.cast(n, 'int32') op = RandomFunction( 'binomial', tensor.TensorType(dtype=dtype, broadcastable=(False, ) * ndim)) return op(random_state, size, n, p)
def random_integers(random_state, size=None, low=0, high=1, ndim=None, dtype='int64'): """ Sample a random integer between low and high, both inclusive. If the size argument is ambiguous on the number of dimensions, ndim may be a plain integer to supplement the missing information. If size is None, the output shape will be determined by the shapes of low and high. """ low = tensor.as_tensor_variable(low) high = tensor.as_tensor_variable(high) ndim, size, bcast = _infer_ndim_bcast(ndim, size, low, high) op = RandomFunction(random_integers_helper, tensor.TensorType(dtype=dtype, broadcastable=bcast)) return op(random_state, size, low, high)
def permutation(random_state, size=None, n=1, ndim=None, dtype='int64'): """ Returns permutations of the integers between 0 and n-1, as many times as required by size. For instance, if size=(p,q), p*q permutations will be generated, and the output shape will be (p,q,n), because each permutation is of size n. Theano tries to infer the number of dimensions from the length of the size argument and the shape of n, but you may always specify it with the `ndim` parameter. .. note:: Note that the output will then be of dimension ndim+1. """ ndim, size, bcast = _infer_ndim_bcast(ndim, size) #print "NDIM", ndim, size op = RandomFunction(permutation_helper, tensor.TensorType(dtype=dtype, broadcastable=bcast+(False,)), ndim_added=1) return op(random_state, size, n)
def uniform(random_state, size=None, low=0.0, high=1.0, ndim=None, dtype=None): """ Sample from a uniform distribution 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. If size is None, the output shape will be determined by the shapes of low and high. If dtype is not specified, it will be inferred from the dtype of low and high, but will be at least as precise as floatX. """ low = tensor.as_tensor_variable(low) high = tensor.as_tensor_variable(high) if dtype is None: dtype = tensor.scal.upcast(theano.config.floatX, low.dtype, high.dtype) ndim, size, bcast = _infer_ndim_bcast(ndim, size, low, high) op = RandomFunction('uniform', tensor.TensorType(dtype=dtype, broadcastable=bcast)) return op(random_state, size, low, high)
def normal(random_state, size=None, avg=0.0, std=1.0, ndim=None, dtype=None): """ Sample from a normal distribution centered on avg with the specified standard deviation (std). If the size argument is ambiguous on the number of dimensions, ndim may be a plain integer to supplement the missing information. If size is None, the output shape will be determined by the shapes of avg and std. If dtype is not specified, it will be inferred from the dtype of avg and std, but will be at least as precise as floatX. """ avg = tensor.as_tensor_variable(avg) std = tensor.as_tensor_variable(std) if dtype is None: dtype = tensor.scal.upcast(theano.config.floatX, avg.dtype, std.dtype) ndim, size, bcast = _infer_ndim_bcast(ndim, size, avg, std) op = RandomFunction('normal', tensor.TensorType(dtype=dtype, broadcastable=bcast)) return op(random_state, size, avg, std)
def multinomial(random_state, size=None, n=1, pvals=[0.5, 0.5], ndim=None, dtype='int64'): """Sample from one or more multinomial distributions defined by one-dimensional slices in pvals. :param pvals: a tensor of shape "nmulti+(L,)" describing each multinomial distribution. This tensor must have the property that numpy.allclose(pvals.sum(axis=-1), 1) is true. :param size: a vector of shape information for the output; this can also specify the "nmulti" part of pvals' shape. A -1 in the k'th position from the right means to borrow the k'th position from the right in nmulti. (See examples below.) Default ``None`` means size=nmulti. :param n: the number of experiments to simulate for each multinomial. This can be a scalar, or tensor, it will be broadcasted to have shape "nmulti". :param dtype: the dtype of the return value (which will represent counts) :returns: tensor of len(size)+1 dimensions, and shape[-1]==L, with the specified ``dtype``, with the experiment counts. See examples to understand the shape of the return value, which is derived from both size and pvals.shape. In return value rval, "numpy.allclose(rval.sum(axis=-1), n)" will be true. For example, to simulate n experiments from each multinomial in a batch of size B: size=None, pvals.shape=(B,L) --> rval.shape=[B,L] rval[i,j] is the count of possibility j in the i'th distribution (row) in pvals. Using size: size=(1,-1), pvals.shape=(A,B,L) --> rval.shape=[1,B,L], and requires that A==1. rval[k,i,j] is the count of possibility j in the distribution specified by pvals[k,i]. Using size for broadcasting of pvals: size=(10, 1, -1), pvals.shape=(A, B, L) --> rval.shape=[10,1,B,L], and requires that A==1. rval[l,k,i,j] is the count of possibility j in the distribution specified by pvals[k,i], in the l'th of 10 draws. """ n = tensor.as_tensor_variable(n) pvals = tensor.as_tensor_variable(pvals) # until ellipsis is implemented (argh) tmp = pvals.T[0].T ndim, size, bcast = _infer_ndim_bcast(ndim, size, n, tmp) bcast = bcast + (pvals.type.broadcastable[-1], ) op = RandomFunction(multinomial_helper, tensor.TensorType(dtype=dtype, broadcastable=bcast), ndim_added=1) return op(random_state, size, n, pvals)