Python numpy.random.get_state() Examples

The following are 10 code examples of numpy.random.get_state(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numpy.random , or try the search function .
Example #1
Source File: gpeiopt_chooser.py    From Milano with Apache License 2.0 6 votes vote down vote up
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)) 
Example #2
Source File: gpei_constrained_chooser.py    From Milano with Apache License 2.0 5 votes vote down vote up
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 
Example #3
Source File: RandomProposer.py    From auptimizer with GNU General Public License v3.0 5 votes vote down vote up
def save(self, path):
        del self.params_gen
        self.random_state = random.get_state()
        super(RandomProposer, self).save(path) 
Example #4
Source File: GPEIOptChooser.py    From auptimizer with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #5
Source File: test_algebraic_connectivity.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def save_random_state():
        state = get_state()
        try:
            yield
        finally:
            set_state(state) 
Example #6
Source File: misc.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __enter__(self):
        from numpy import random

        self.startstate = random.get_state()
        random.seed(self.seed) 
Example #7
Source File: test_parser.py    From streaming-form-data with MIT License 5 votes vote down vote up
def local_seed(seed):
    state = random.get_state()

    try:
        random.seed(seed)
        yield
    finally:
        random.set_state(state) 
Example #8
Source File: test_algebraic_connectivity.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def save_random_state():
        state = get_state()
        try:
            yield
        finally:
            set_state(state) 
Example #9
Source File: GPConstrainedEIChooser.py    From auptimizer with GNU General Public License v3.0 4 votes vote down vote up
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) 
Example #10
Source File: decorators.py    From Carnets with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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