本文整理匯總了Python中numpy.random.get_state方法的典型用法代碼示例。如果您正苦於以下問題:Python random.get_state方法的具體用法?Python random.get_state怎麽用?Python random.get_state使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy.random
的用法示例。
在下文中一共展示了random.get_state方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _real_init
# 需要導入模塊: from numpy import random [as 別名]
# 或者: from numpy.random import get_state [as 別名]
def _real_init(self, dims, values):
self.randomstate = npr.get_state()
# Input dimensionality.
self.D = dims
# Initial length scales.
self.ls = np.ones(self.D)
# Initial amplitude.
self.amp2 = np.std(values)+1e-4
# Initial observation noise.
self.noise = 1e-3
# Initial mean.
self.mean = np.mean(values)
# Save hyperparameter samples
self.hyper_samples.append((self.mean, self.noise, self.amp2,
self.ls))
示例2: _real_init
# 需要導入模塊: from numpy import random [as 別名]
# 或者: from numpy.random import get_state [as 別名]
def _real_init(self, dims, values, durations):
self.randomstate = npr.get_state()
# Identify constraint violations
# Note that we'll treat NaNs and Infs as these values as well
# as an optional user defined value
goodvals = np.nonzero(np.logical_and(values != self.bad_value,
np.isfinite(values)))[0]
# Input dimensionality.
self.D = dims
# Initial length scales.
self.ls = np.ones(self.D)
self.constraint_ls = np.ones(self.D)
# Initial amplitude.
self.amp2 = np.std(values[goodvals])+1e-4
self.constraint_amp2 = 1.0
# Initial observation noise.
self.noise = 1e-3
self.constraint_noise = 1e-3
self.constraint_gain = 1
# Initial mean.
self.mean = np.mean(values[goodvals])
self.constraint_mean = 0.5
示例3: save
# 需要導入模塊: from numpy import random [as 別名]
# 或者: from numpy.random import get_state [as 別名]
def save(self, path):
del self.params_gen
self.random_state = random.get_state()
super(RandomProposer, self).save(path)
示例4: _real_init
# 需要導入模塊: from numpy import random [as 別名]
# 或者: from numpy.random import get_state [as 別名]
def _real_init(self, dims, values):
self.locker.lock_wait(self.state_pkl)
self.randomstate = npr.get_state()
if os.path.exists(self.state_pkl):
fh = open(self.state_pkl, 'r')
state = pickle.load(fh)
fh.close()
self.D = state['dims']
self.ls = state['ls']
self.amp2 = state['amp2']
self.noise = state['noise']
self.mean = state['mean']
self.hyper_samples = state['hyper_samples']
self.needs_burnin = False
else:
# Input dimensionality.
self.D = dims
# Initial length scales.
self.ls = np.ones(self.D)
# Initial amplitude.
self.amp2 = np.std(values)+1e-4
# Initial observation noise.
self.noise = 1e-3
# Initial mean.
self.mean = np.mean(values)
# Save hyperparameter samples
self.hyper_samples.append((self.mean, self.noise, self.amp2,
self.ls))
self.locker.unlock(self.state_pkl)
示例5: save_random_state
# 需要導入模塊: from numpy import random [as 別名]
# 或者: from numpy.random import get_state [as 別名]
def save_random_state():
state = get_state()
try:
yield
finally:
set_state(state)
示例6: __enter__
# 需要導入模塊: from numpy import random [as 別名]
# 或者: from numpy.random import get_state [as 別名]
def __enter__(self):
from numpy import random
self.startstate = random.get_state()
random.seed(self.seed)
示例7: local_seed
# 需要導入模塊: from numpy import random [as 別名]
# 或者: from numpy.random import get_state [as 別名]
def local_seed(seed):
state = random.get_state()
try:
random.seed(seed)
yield
finally:
random.set_state(state)
示例8: _real_init
# 需要導入模塊: from numpy import random [as 別名]
# 或者: from numpy.random import get_state [as 別名]
def _real_init(self, dims, values, durations):
self.locker.lock_wait(self.state_pkl)
self.randomstate = npr.get_state()
if os.path.exists(self.state_pkl):
fh = open(self.state_pkl, 'rb')
state = pickle.load(fh)
fh.close()
self.D = state['dims']
self.ls = state['ls']
self.amp2 = state['amp2']
self.noise = state['noise']
self.mean = state['mean']
self.constraint_ls = state['constraint_ls']
self.constraint_amp2 = state['constraint_amp2']
self.constraint_noise = state['constraint_noise']
self.constraint_mean = state['constraint_mean']
self.constraint_gain = state['constraint_gain']
self.needs_burnin = False
else:
# Identify constraint violations
# Note that we'll treat NaNs and Infs as these values as well
# as an optional user defined value
goodvals = np.nonzero(np.logical_and(values != self.bad_value,
np.isfinite(values)))[0]
# Input dimensionality.
self.D = dims
# Initial length scales.
self.ls = np.ones(self.D)
self.constraint_ls = np.ones(self.D)
# Initial amplitude.
self.amp2 = np.std(values[goodvals])+1e-4
self.constraint_amp2 = 1.0
# Initial observation noise.
self.noise = 1e-3
self.constraint_noise = 1e-3
self.constraint_gain = 1
# Initial mean.
self.mean = np.mean(values[goodvals])
self.constraint_mean = 0.5
self.locker.unlock(self.state_pkl)
示例9: preserve_random_state
# 需要導入模塊: from numpy import random [as 別名]
# 或者: from numpy.random import get_state [as 別名]
def preserve_random_state(func):
""" Decorator to preserve the numpy.random state during a function.
Parameters
----------
func : function
function around which to preserve the random state.
Returns
-------
wrapper : function
Function which wraps the input function by saving the state before
calling the function and restoring the function afterward.
Examples
--------
Decorate functions like this::
@preserve_random_state
def do_random_stuff(x, y):
return x + y * numpy.random.random()
Notes
-----
If numpy.random is not importable, the state is not saved or restored.
"""
try:
from numpy.random import get_state, seed, set_state
@contextmanager
def save_random_state():
state = get_state()
try:
yield
finally:
set_state(state)
def wrapper(*args, **kwargs):
with save_random_state():
seed(1234567890)
return func(*args, **kwargs)
wrapper.__name__ = func.__name__
return wrapper
except ImportError:
return func