當前位置: 首頁>>代碼示例>>Python>>正文


Python mxnet.random方法代碼示例

本文整理匯總了Python中mxnet.random方法的典型用法代碼示例。如果您正苦於以下問題:Python mxnet.random方法的具體用法?Python mxnet.random怎麽用?Python mxnet.random使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在mxnet的用法示例。


在下文中一共展示了mxnet.random方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: random_seed

# 需要導入模塊: import mxnet [as 別名]
# 或者: from mxnet import random [as 別名]
def random_seed(seed=None):
    """
    Runs a code block with a new seed for np, mx and python's random.

    Parameters
    ----------

    seed : the seed to pass to np.random, mx.random and python's random.

    To impose rng determinism, invoke e.g. as in:

    with random_seed(1234):
        ...

    To impose rng non-determinism, invoke as in:

    with random_seed():
        ...

    Upon conclusion of the block, the rng's are returned to
    a state that is a function of their pre-block state, so
    any prior non-determinism is preserved.

    """

    try:
        next_seed = np.random.randint(0, np.iinfo(np.int32).max)
        if seed is None:
            np.random.seed()
            seed = np.random.randint(0, np.iinfo(np.int32).max)
        logger = default_logger()
        logger.debug('Setting np, mx and python random seeds = %s', seed)
        np.random.seed(seed)
        mx.random.seed(seed)
        random.seed(seed)
        yield
    finally:
        # Reinstate prior state of np.random and other generators
        np.random.seed(next_seed)
        mx.random.seed(next_seed)
        random.seed(next_seed) 
開發者ID:awslabs,項目名稱:dynamic-training-with-apache-mxnet-on-aws,代碼行數:43,代碼來源:common.py

示例2: _inplace_arg_dict_randomization

# 需要導入模塊: import mxnet [as 別名]
# 或者: from mxnet import random [as 別名]
def _inplace_arg_dict_randomization(arg_dict, mean_arg_dict, bounds, std=STARTING_POINT_RANDOMIZATION_STD):
    """
    In order to initialize L-BFGS from multiple starting points, this function makes it possible to
    randomize, inplace, an arg_dict (as used by executors to communicate parameters to L-BFGS).
    The randomization is centered around mean_arg_dict, with standard deviation std.

    :param arg_dict: dict param_name to mx.nd (as used in executors). This argument is modified inplace
    :param mean_arg_dict: arg_dict around which the random perturbations occur (dict param_name to mx.nd, as used in executors))
    :param bounds: dict param_name to (lower, upper) bounds, as used in L-BFGS
    :param std: standard deviation according to which the (Gaussian) random perturbations happen
    """

    # We check that arg_dict and mean_arg_dict are compatible
    assert arg_dict.keys() == mean_arg_dict.keys()
    for name, param in arg_dict.items():
        assert param.shape == mean_arg_dict[name].shape
        assert param.dtype == mean_arg_dict[name].dtype
        assert param.context == mean_arg_dict[name].context

    # We apply a sort to make the for loop deterministic (especially with the internal calls to mx.random)
    for name, param in sorted(arg_dict.items()):

        arg_dict[name][:] = mean_arg_dict[name] + mx.random.normal(0.0, std, shape=param.shape, dtype=param.dtype, ctx=param.context)

        lower, upper = bounds[name]
        lower = lower if lower is not None else -np.inf
        upper = upper if upper is not None else np.inf

        # We project back arg_dict[name] within its specified lower and upper bounds
        # (in case of we would have perturbed beyond those bounds)
        arg_dict[name][:] = mx.nd.maximum(lower, mx.nd.minimum(upper, arg_dict[name]))


# === Exported functions === 
開發者ID:awslabs,項目名稱:autogluon,代碼行數:36,代碼來源:optimization_utils.py


注:本文中的mxnet.random方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。