本文整理汇总了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)
示例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 ===