You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by gi...@git.apache.org on 2017/09/21 16:05:24 UTC
[GitHub] reminisce commented on a change in pull request #7939: Random refactor
reminisce commented on a change in pull request #7939: Random refactor
URL: https://github.com/apache/incubator-mxnet/pull/7939#discussion_r140287011
##########
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:
To chip in a bit, the main purpose of this PR is for separating default behaviors of symbolic and imperative random operators. You can see this https://github.com/apache/incubator-mxnet/pull/7874 for more context, while that PR provided a hacky solution, this one is more elegant and "software engineering oriented".
----------------------------------------------------------------
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:
users@infra.apache.org
With regards,
Apache Git Services