asmushetzel commented on a change in pull request #7939: Random refactor URL: https://github.com/apache/incubator-mxnet/pull/7939#discussion_r140165896
########## File path: python/mxnet/symbol/random.py ########## @@ -15,5 +15,245 @@ # specific language governing permissions and limitations # under the License. -"""Random Distribution Generator Symbol API of MXNet.""" -__all__ = [] +"""Random distribution generator Symbol API of MXNet.""" + +from ..base import numeric_types, _Null +from . import _internal +from .symbol import Symbol + + +__all__ = ['uniform', 'normal', 'poisson', 'exponential', 'gamma', 'multinomial', + 'negative_binomial', 'generalized_negative_binomial'] + + +def _random_helper(random, sampler, params, shape, dtype, kwargs): + """Helper function for random generators.""" + if isinstance(params[0], Symbol): + for i in params[1:]: + assert isinstance(i, Symbol), \ + "Distribution parameters must all have the same type, but got " \ + "both %s and %s."%(type(params[0]), type(i)) + return sampler(*params, shape=shape, dtype=dtype, **kwargs) + elif isinstance(params[0], numeric_types): + for i in params[1:]: + assert isinstance(i, numeric_types), \ + "Distribution parameters must all have the same type, but got " \ + "both %s and %s."%(type(params[0]), type(i)) + return random(*params, shape=shape, dtype=dtype, **kwargs) + + raise ValueError("Distribution parameters must be either Symbol or numbers, " + "but got %s."%type(params[0])) + + +def uniform(low=0, high=1, shape=_Null, dtype=_Null, **kwargs): + """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 + 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 + 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 + 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'} + Data type of output samples. Default is 'float32' + """ + return _random_helper(_internal._random_uniform, _internal._sample_uniform, + [low, high], shape, dtype, kwargs) + + +def normal(loc=0, scale=1, shape=_Null, dtype=_Null, **kwargs): Review comment: Is it really necessary that we have to duplicate the description of the operator now? Meaning having it one time in the python file and one time in the .cc file? FYI: We are close of getting the GPU support done and pushed. This will change only the files in the random-subdirectory. No interface change necessary. It will also no longer the uniform/normal distributions from mshadow. The only dependency on mshadow will be access that we get a random integer from the mshadow random generator that serves as the seed for our new code. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services