Python numpy.random() Examples

The following are 30 code examples of numpy.random(). 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 , or try the search function .
Example #1
Source File: _multivariate.py    From lambda-packs with MIT License 6 votes vote down vote up
def rvs(self, alpha, size=1, random_state=None):
        """
        Draw random samples from a Dirichlet distribution.

        Parameters
        ----------
        %(_dirichlet_doc_default_callparams)s
        size : int, optional
            Number of samples to draw (default 1).
        %(_doc_random_state)s

        Returns
        -------
        rvs : ndarray or scalar
            Random variates of size (`size`, `N`), where `N` is the
            dimension of the random variable.

        """
        alpha = _dirichlet_check_parameters(alpha)
        random_state = self._get_random_state(random_state)
        return random_state.dirichlet(alpha, size=size) 
Example #2
Source File: utils.py    From contextualbandits with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def partial_fit(self, X, y, classes=None):
        if self.partial_method == "gamma":
            w_all = -np.log(self
                            .random_state
                            .random(size=(X.shape[0], self.nsamples))
                            .clip(min=1e-12, max=None))
            appear_times = None
            rng = None
        elif self.partial_method == "poisson":
            w_all = None
            appear_times = self.random_state.poisson(1, size = (X.shape[0], self.nsamples))
            rng = np.arange(X.shape[0])
        else:
            raise ValueError(_unexpected_err_msg)
        Parallel(n_jobs=self.njobs, verbose=0, require="sharedmem")\
                (delayed(self._partial_fit_single)\
                    (sample, w_all, appear_times, rng, X, y) \
                        for sample in range(self.nsamples)) 
Example #3
Source File: myImageTransformations.py    From Attention-Gated-Networks with MIT License 6 votes vote down vote up
def elastic_transform(image, alpha=1000, sigma=30, spline_order=1, mode='nearest', random_state=np.random):
    """Elastic deformation of image as described in [Simard2003]_.
    .. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for
       Convolutional Neural Networks applied to Visual Document Analysis", in
       Proc. of the International Conference on Document Analysis and
       Recognition, 2003.
    """
    assert image.ndim == 3
    shape = image.shape[:2]

    dx = gaussian_filter((random_state.rand(*shape) * 2 - 1),
                         sigma, mode="constant", cval=0) * alpha
    dy = gaussian_filter((random_state.rand(*shape) * 2 - 1),
                         sigma, mode="constant", cval=0) * alpha

    x, y = np.meshgrid(np.arange(shape[0]), np.arange(shape[1]), indexing='ij')
    indices = [np.reshape(x + dx, (-1, 1)), np.reshape(y + dy, (-1, 1))]
    result = np.empty_like(image)
    for i in range(image.shape[2]):
        result[:, :, i] = map_coordinates(
            image[:, :, i], indices, order=spline_order, mode=mode).reshape(shape)
    return result 
Example #4
Source File: viz.py    From evolution-strategies-starter with MIT License 6 votes vote down vote up
def main(env_id, policy_file, record, stochastic, extra_kwargs):
    import gym
    from gym import wrappers
    import tensorflow as tf
    from es_distributed.policies import MujocoPolicy
    import numpy as np

    env = gym.make(env_id)
    if record:
        import uuid
        env = wrappers.Monitor(env, '/tmp/' + str(uuid.uuid4()), force=True)

    if extra_kwargs:
        import json
        extra_kwargs = json.loads(extra_kwargs)

    with tf.Session():
        pi = MujocoPolicy.Load(policy_file, extra_kwargs=extra_kwargs)
        while True:
            rews, t = pi.rollout(env, render=True, random_stream=np.random if stochastic else None)
            print('return={:.4f} len={}'.format(rews.sum(), t))

            if record:
                env.close()
                return 
Example #5
Source File: test_common.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_random_state():
    import numpy.random as npr
    # Check with seed
    state = com.random_state(5)
    assert state.uniform() == npr.RandomState(5).uniform()

    # Check with random state object
    state2 = npr.RandomState(10)
    assert com.random_state(state2).uniform() == npr.RandomState(10).uniform()

    # check with no arg random state
    assert com.random_state() is np.random

    # Error for floats or strings
    with pytest.raises(ValueError):
        com.random_state('test')

    with pytest.raises(ValueError):
        com.random_state(5.5) 
Example #6
Source File: testing.py    From recruit with Apache License 2.0 6 votes vote down vote up
def _create_missing_idx(nrows, ncols, density, random_state=None):
    if random_state is None:
        random_state = np.random
    else:
        random_state = np.random.RandomState(random_state)

    # below is cribbed from scipy.sparse
    size = int(np.round((1 - density) * nrows * ncols))
    # generate a few more to ensure unique values
    min_rows = 5
    fac = 1.02
    extra_size = min(size + min_rows, fac * size)

    def _gen_unique_rand(rng, _extra_size):
        ind = rng.rand(int(_extra_size))
        return np.unique(np.floor(ind * nrows * ncols))[:size]

    ind = _gen_unique_rand(random_state, extra_size)
    while ind.size < size:
        extra_size *= 1.05
        ind = _gen_unique_rand(random_state, extra_size)

    j = np.floor(ind * 1. / nrows).astype(int)
    i = (ind - j * nrows).astype(int)
    return i.tolist(), j.tolist() 
Example #7
Source File: dataset.py    From rankeval with Mozilla Public License 2.0 6 votes vote down vote up
def _check_random_state(seed):
        """
        Turn seed into a np.random.RandomState instance (took for sklearn)

        Parameters
        ----------
        seed : None | int | instance of RandomState
            If seed is None, return the RandomState singleton used by np.random.
            If seed is an int, return a new RandomState instance seeded with it.
            If seed is already a RandomState instance, return it.
            Otherwise raise ValueError.
        """
        if seed is None or seed is np.random:
            return np.random.mtrand._rand
        if isinstance(seed, (numbers.Integral, np.integer)):
            return np.random.RandomState(seed)
        if isinstance(seed, np.random.RandomState):
            return seed
        raise ValueError('%r cannot be used to seed a numpy.random.RandomState'
                         ' instance' % seed) 
Example #8
Source File: utils.py    From mars with Apache License 2.0 6 votes vote down vote up
def check_random_state(seed):
    """
    Turn seed into a mt.random.RandomState instance

    :param seed:
        If seed is None, return the RandomState singleton used by mt.random.
        If seed is an int, return a new RandomState instance seeded with seed.
        If seed is already a RandomState instance, return it.
        Otherwise raise ValueError.
    :return:
    """
    from . import random as mtrand
    from numpy import random as np_mtrand

    if seed is None or seed is mtrand or seed is np_mtrand:
        return mtrand._random_state
    if isinstance(seed, (Integral, np.integer)):
        return mtrand.RandomState(seed)
    if isinstance(seed, np.random.RandomState):
        return mtrand.RandomState.from_numpy(seed)
    if isinstance(seed, mtrand.RandomState):
        return seed
    raise ValueError('%r cannot be used to seed a mt.random.RandomState'
                     ' instance' % seed) 
Example #9
Source File: Dataloader.py    From Text_Segmentation_Image_Inpainting with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, image_folder, max_images=False, image_size=(512, 512), add_random_masks=False):
        super(ImageInpaintingData, self).__init__()

        if isinstance(image_folder, str):
            self.images = glob.glob(os.path.join(image_folder, "clean/*"))
        else:
            self.images = list(chain.from_iterable([glob.glob(os.path.join(i, "clean/*")) for i in image_folder]))
        assert len(self.images) > 0

        if max_images:
            self.images = random.choices(self.images, k=max_images)
        print(f"Find {len(self.images)} images.")

        self.img_size = image_size

        self.transformer = Compose([RandomGrayscale(p=0.4),
                                    # ColorJitter(brightness=0.2, contrast=0.2, saturation=0, hue=0),
                                    ToTensor(),
                                    # Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
                                    ])
        self.add_random_masks = add_random_masks 
Example #10
Source File: Dataloader.py    From Text_Segmentation_Image_Inpainting with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, image_folder, max_images=False, image_size=(512, 512), add_random_masks=False):
        super(TestDataset, self).__init__()
        if isinstance(image_folder, str):
            self.images = glob.glob(os.path.join(image_folder, "clean/*"))
        else:
            self.images = list(chain.from_iterable([glob.glob(os.path.join(i, "clean/*")) for i in image_folder]))
        assert len(self.images) > 0

        if max_images:
            self.images = random.choices(self.images, k=max_images)
        print(f"Find {len(self.images)} images.")

        self.img_size = image_size

        self.transformer = Compose([  # RandomGrayscale(p=0.4),
            # ColorJitter(brightness=0.2, contrast=0.2, saturation=0, hue=0),
            ToTensor(),
            # Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])
        self.add_random_masks = add_random_masks 
Example #11
Source File: task.py    From NiaPy with MIT License 6 votes vote down vote up
def repair(self, x, rnd=rand):
        r"""Repair solution and put the solution in the random position inside of the bounds of problem.

        Arguments:
                x (numpy.ndarray): Solution to check and repair if needed.
                rnd (mtrand.RandomState): Random number generator.

        Returns:
                numpy.ndarray: Fixed solution.

        See Also:
                * :func:`NiaPy.util.limitRepair`
                * :func:`NiaPy.util.limitInversRepair`
                * :func:`NiaPy.util.wangRepair`
                * :func:`NiaPy.util.randRepair`
                * :func:`NiaPy.util.reflectRepair`

        """

        return self.frepair(x, self.Lower, self.Upper, rnd=rnd) 
Example #12
Source File: hc.py    From NiaPy with MIT License 6 votes vote down vote up
def Neighborhood(x, delta, task, rnd=rand):
	r"""Get neighbours of point.

	Args:
		x numpy.ndarray: Point.
		delta (float): Standard deviation.
		task (Task): Optimization task.
		rnd (Optional[mtrand.RandomState]): Random generator.

	Returns:
		Tuple[numpy.ndarray, float]:
			1. New solution.
			2. New solutions function/fitness value.
	"""
	X = x + rnd.normal(0, delta, task.D)
	X = task.repair(X, rnd)
	Xfit = task.eval(X)
	return X, Xfit 
Example #13
Source File: aso.py    From NiaPy with MIT License 6 votes vote down vote up
def Sequential(x, xpb, xb, xr, MP_c, MP_s, MP_p, F, CR, task, rnd=rand):
	r"""Sequentialy combines all three strategies.

	Args:
		x (numpy.ndarray): individual position.
		xpb (numpy.ndarray): individuals best position.
		xb (numpy.ndarray): current best position.
		xr (numpy.ndarray): random individual.
		MP_c (float): Fickleness index value.
		MP_s (float): External irregularity index value.
		MP_p (float): Internal irregularity index value.
		F (float): scale factor.
		CR (float): crossover factor.
		task (Task): optimization task.
		rnd (mtrand.randomstate): random number generator.

	Returns:
		tuple[numpy.ndarray, float]:
			1. new position
			2. new positions function/fitness value
	"""
	xn = task.repair(MP_S(MP_P(MP_C(x, F, CR, MP_c, rnd), xpb, CR, MP_p, rnd), xr, xb, CR, MP_s, rnd), rnd=rnd)
	return xn, task.eval(xn) 
Example #14
Source File: Dataloader.py    From Text_Segmentation_Image_Inpainting with GNU General Public License v3.0 6 votes vote down vote up
def random_masks(pil_img, size=512, offset=10):
    draw = ImageDraw.Draw(pil_img)
    # draw liens
    # can't use np.random because its not forkable under PyTorch's dataloader with multiprocessing
    reps = random.randint(1, 5)

    for i in range(reps):
        cords = np.array(random.choices(range(offset, size), k=4)).reshape(2, 2)
        cords[1] = np.clip(cords[1], a_min=cords[0] - 75, a_max=cords[0] + 75)

        width = random.randint(15, 20)
        draw.line(cords.reshape(-1).tolist(), width=width, fill=255)
    # # draw circles
    reps = random.randint(1, 5)
    for i in range(reps):
        cords = np.array(random.choices(range(offset, size - offset), k=2))
        cords.sort()
        ex = np.array(random.choices(range(20, 70), k=2)) + cords
        ex = np.clip(ex, a_min=offset, a_max=size - offset)
        draw.ellipse(np.concatenate([cords, ex]).tolist(), fill=255)
    return pil_img 
Example #15
Source File: _util.py    From lambda-packs with MIT License 6 votes vote down vote up
def check_random_state(seed):
    """Turn seed into a np.random.RandomState instance

    If seed is None (or np.random), return the RandomState singleton used
    by np.random.
    If seed is an int, return a new RandomState instance seeded with seed.
    If seed is already a RandomState instance, return it.
    Otherwise raise ValueError.
    """
    if seed is None or seed is np.random:
        return np.random.mtrand._rand
    if isinstance(seed, (numbers.Integral, np.integer)):
        return np.random.RandomState(seed)
    if isinstance(seed, np.random.RandomState):
        return seed
    raise ValueError('%r cannot be used to seed a numpy.random.RandomState'
                     ' instance' % seed) 
Example #16
Source File: de.py    From NiaPy with MIT License 6 votes vote down vote up
def popDecrement(self, pop, task):
		r"""Decrement population.

		Args:
			pop (numpy.ndarray): Current population.
			task (Task): Optimization task.

		Returns:
			numpy.ndarray[Individual]: Decreased population.
		"""
		deltapop = int(round(max(1, self.NP * self.deltaPopC(task.Iters))))
		if len(pop) - deltapop <= 0: return pop
		ni = self.Rand.choice(len(pop), deltapop, replace=False)
		npop = []
		for i, e in enumerate(pop):
			if i not in ni: npop.append(e)
			elif self.rand() >= self.omega: npop.append(e)
		return objects2array(npop) 
Example #17
Source File: es.py    From NiaPy with MIT License 6 votes vote down vote up
def runIteration(self, task, c, fpop, xb, fxb, **dparams):
		r"""Core function of EvolutionStrategyML algorithm.

		Args:
			task (Task): Optimization task.
			c (numpy.ndarray): Current population.
			fpop (numpy.ndarray): Current population fitness/function values.
			xb (numpy.ndarray): Global best individual.
			fxb (float): Global best individuals fitness/function value.
			**dparams Dict[str, Any]: Additional arguments.

		Returns:
			Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, float, Dict[str, Any]]:
				1. New population.
				2. New populations fitness/function values.
				3. New global best solution.
				4. New global best solutions fitness/objective value.
				5. Additional arguments.
		"""
		cn = objects2array([IndividualES(x=self.mutateRand(c, task), task=task, rand=self.Rand) for _ in range(self.lam)])
		c = self.newPop(cn)
		fc = asarray([x.f for x in c])
		xb, fxb = self.getBest(c, fc, xb, fxb)
		return c, fc, xb, fxb, {} 
Example #18
Source File: cro.py    From NiaPy with MIT License 6 votes vote down vote up
def BroodingSimple(pop, p, task, rnd=rand, **kwargs):
	r"""Brooding or internal sexual reproduction of corals.

	Args:
		pop (numpy.ndarray): Current population.
		p (float): Probability in range [0, 1].
		task (Task): Optimization task.
		rnd (mtrand.RandomState): Random generator.
		**kwargs (Dict[str, Any]): Additional arguments.

	Returns:
		Tuple[numpy.ndarray, numpy.ndarray]:
			1. New population.
			2. New population function/fitness values.
	"""
	for i in range(len(pop)): pop[i] = task.repair(asarray([pop[i, d] if rnd.rand() < p else task.Lower[d] + task.bRange[d] * rnd.rand() for d in range(task.D)]), rnd=rnd)
	return pop, apply_along_axis(task.eval, 1, pop) 
Example #19
Source File: cro.py    From NiaPy with MIT License 6 votes vote down vote up
def SexualCrossoverSimple(pop, p, task, rnd=rand, **kwargs):
	r"""Sexual reproduction of corals.

	Args:
		pop (numpy.ndarray): Current population.
		p (float): Probability in range [0, 1].
		task (Task): Optimization task.
		rnd (mtrand.RandomState): Random generator.
		**kwargs (Dict[str, Any]): Additional arguments.

	Returns:
		Tuple[numpy.ndarray, numpy.ndarray]:
			1. New population.
			2. New population function/fitness values.
	"""
	for i in range(len(pop) // 2): pop[i] = asarray([pop[i, d] if rnd.rand() < p else pop[i * 2, d] for d in range(task.D)])
	return pop, apply_along_axis(task.eval, 1, pop) 
Example #20
Source File: ca.py    From NiaPy with MIT License 6 votes vote down vote up
def nextX(self, cb, E_init, S_init, task, rnd=rand):
		r"""Apply function nextX on Camel.

		This method/function move this Camel to new position in search space.

		Args:
			cb (Camel): Best Camel in population.
			E_init (float): Starting endurance of camel.
			S_init (float): Starting supply of camel.
			task (Task): Optimization task.
			rnd (Optional[mtrand.RandomState]): Random number generator.
		"""
		delta = -1 + rnd.rand() * 2
		self.x = self.x_past + delta * (1 - (self.E / E_init)) * exp(1 - self.S / S_init) * (cb - self.x_past)
		if not task.isFeasible(self.x): self.x = self.x_past
		else: self.f = task.eval(self.x) 
Example #21
Source File: aso.py    From NiaPy with MIT License 6 votes vote down vote up
def MP_S(x, xr, xb, CR, MP, rnd=rand):
	r"""Get new position based on external irregularity.

	Args:
		x (numpy.ndarray): Current individuals position.
		xr (numpy.ndarray): Random individuals position.
		xb (numpy.ndarray): Global best individuals position.
		CR (float): Crossover probability.
		MP (float): External irregularity index.
		rnd (mtrand.RandomState): Random number generator.

	Returns:
		numpy.ndarray: New position.
	"""
	if MP < 0.25:
		b = sort(rnd.choice(len(x), 2, replace=False))
		x[b[0]:b[1]] = xb[b[0]:b[1]]
		return x
	elif MP < 0.5: return asarray([xb[i] if rnd.rand() < CR else x[i] for i in range(len(x))])
	elif MP < 0.75:
		b = sort(rnd.choice(len(x), 2, replace=False))
		x[b[0]:b[1]] = xr[b[0]:b[1]]
		return x
	return asarray([xr[i] if rnd.rand() < CR else x[i] for i in range(len(x))]) 
Example #22
Source File: aso.py    From NiaPy with MIT License 6 votes vote down vote up
def MP_C(x, F, CR, MP, rnd=rand):
	r"""Get bew position based on fickleness.

	Args:
		x (numpy.ndarray): Current individuals position.
		F (float): Scale factor.
		CR (float): Crossover probability.
		MP (float): Fickleness index value
		rnd (mtrand.RandomState): Random number generator

	Returns:
		numpy.ndarray: New position
	"""
	if MP < 0.5:
		b = sort(rnd.choice(len(x), 2, replace=False))
		x[b[0]:b[1]] = x[b[0]:b[1]] + F * rnd.normal(0, 1, b[1] - b[0])
		return x
	return asarray([x[i] + F * rnd.normal(0, 1) if rnd.rand() < CR else x[i] for i in range(len(x))]) 
Example #23
Source File: aso.py    From NiaPy with MIT License 6 votes vote down vote up
def Crossover(x, xpb, xb, xr, MP_c, MP_s, MP_p, F, CR, task, rnd=rand):
	r"""Create a crossover over all three strategies.

	Args:
		x (numpy.ndarray): individual position.
		xpb (numpy.ndarray): individuals best position.
		xb (numpy.ndarray): current best position.
		xr (numpy.ndarray): random individual.
		MP_c (float): Fickleness index value.
		MP_s (float): External irregularity index value.
		MP_p (float): Internal irregularity index value.
		F (float): scale factor.
		CR (float): crossover factor.
		task (Task): optimization task.
		rnd (mtrand.randomstate): random number generator.

	Returns:
		Tuple[numpy.ndarray, float]:
			1. new position
			2. new positions function/fitness value
	"""
	xns = [task.repair(MP_C(x, F, CR, MP_c, rnd), rnd=rnd), task.repair(MP_S(x, xr, xb, CR, MP_s, rnd), rnd=rnd), task.repair(MP_P(x, xpb, CR, MP_p, rnd), rnd=rnd)]
	x = asarray([xns[rnd.randint(len(xns))][i] if rnd.rand() < CR else x[i] for i in range(len(x))])
	return x, task.eval(x) 
Example #24
Source File: aso.py    From NiaPy with MIT License 6 votes vote down vote up
def Elitism(x, xpb, xb, xr, MP_c, MP_s, MP_p, F, CR, task, rnd=rand):
	r"""Select the best of all three strategies.

	Args:
		x (numpy.ndarray): individual position.
		xpb (numpy.ndarray): individuals best position.
		xb (numpy.ndarray): current best position.
		xr (numpy.ndarray): random individual.
		MP_c (float): Fickleness index value.
		MP_s (float): External irregularity index value.
		MP_p (float): Internal irregularity index value.
		F (float): scale factor.
		CR (float): crossover factor.
		task (Task): optimization task.
		rnd (mtrand.randomstate): random number generator.

	Returns:
		Tuple[numpy.ndarray, float]:
			1. New position of individual
			2. New positions fitness/function value
	"""
	xn = [task.repair(MP_C(x, F, CR, MP_c, rnd), rnd=rnd), task.repair(MP_S(x, xr, xb, CR, MP_s, rnd), rnd=rnd), task.repair(MP_P(x, xpb, CR, MP_p, rnd), rnd=rnd)]
	xn_f = apply_along_axis(task.eval, 1, xn)
	ib = argmin(xn_f)
	return xn[ib], xn_f[ib] 
Example #25
Source File: ca.py    From NiaPy with MIT License 5 votes vote down vote up
def runIteration(self, task, caravan, fcaravan, cb, fcb, **dparams):
		r"""Core function of Camel Algorithm.

		Args:
			task (Task): Optimization task.
			caravan (numpy.ndarray[Camel]): Current population of Camels.
			fcaravan (numpy.ndarray[float]): Current population fitness/function values.
			cb (Camel): Current best Camel.
			fcb (float): Current best Camel fitness/function value.
			**dparams (Dict[str, Any]): Additional arguments.

		Returns:
			Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, folat, dict]:
				1. New population
				2. New population function/fitness value
				3. New global best solution
				4. New global best fitness/objective value
				5. Additional arguments
		"""
		ncaravan = objects2array([self.walk(c, cb, task) for c in caravan])
		ncaravan = objects2array([self.oasis(c, self.rand(), self.alpha) for c in ncaravan])
		ncaravan = objects2array([self.lifeCycle(c, self.mu, task) for c in ncaravan])
		fncaravan = asarray([c.f for c in ncaravan])
		cb, fcb = self.getBest(ncaravan, fncaravan, cb, fcb)
		return ncaravan, fncaravan, cb, fcb, {}

# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3 
Example #26
Source File: ca.py    From NiaPy with MIT License 5 votes vote down vote up
def nextT(self, T_min, T_max, rnd=rand):
		r"""Apply nextT function on Camel.

		Args:
			T_min (float): TODO
			T_max (float): TODO
			rnd (Optional[mtrand.RandomState]): Random number generator.
		"""
		self.T = (T_max - T_min) * rnd.rand() + T_min 
Example #27
Source File: test_MissionSim.py    From EXOSIMS with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_random_seed_initialize_full(self):
        r"""Test random_seed_initialize method (longer).

        Method: Initialize with a specific random seed, and ensure the numpy
        RNG seeding mechanism was indeed called by the __init__ function.
        """
        # Note: the mission.__init__() method, in building up planets, etc., calls
        # the numpy RNG many times.  All these calls make it impossible to check the 
        # RNG state just after the object is again available to this test code.
        # So, to test, we have to ensure that np.random.seed is
        # called correctly instead of checking the RNG state after __init__ returns.

        # set up the seed, and the specs addition that includes it
        seed = 1234567890
        specs = {'seed': seed}

        # plugs in our own mock object to monitor calls to np.random.seed()
        with assertMethodIsCalled(np.random, "seed") as rng_seed_mock:
            # initialize the object
            with RedirectStreams(stdout=self.dev_null):
                mission = self.fixture(scriptfile=SimpleScript, **specs)
            # ensure np.random.seed was called once, with the given seed provided
            self.assertEqual(len(rng_seed_mock.method_args), 1, 'RNG was not seeded.')
            self.assertEqual(len(rng_seed_mock.method_args[0]), 1, 'RNG seed arguments incorrect.')
            self.assertEqual(rng_seed_mock.method_args[0][0], seed)
            # keyword argument should be an empty dictionary
            self.assertEqual(len(rng_seed_mock.method_kwargs), 1, 'RNG seeded with unexpected arguments.')
            self.assertDictEqual(rng_seed_mock.method_kwargs[0], {}, 'RNG seeded with unexpected arguments.')
        # might as well validate the object
        self.validate_object(mission) 
Example #28
Source File: utils.py    From contextualbandits with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _spawn_arm(self, fitted_classifier = None, n_w_rew = 0, n_wo_rew = 0,
                   buffer_X = None, buffer_y = None):
        self.n += 1
        self.rng_arm.append(self.random_state if (self.random_state == np.random) else \
                            _check_random_state(
                                self.random_state.integers(np.iinfo(np.int32).max) + 1))
        if self.smooth is not None:
            self.counters = np.c_[self.counters, np.array([n_w_rew + n_wo_rew]).reshape((1, 1)).astype(self.counters.dtype)]
        if (self.force_counters) or (self.thr > 0 and not self.force_fit):
            new_beta_col = np.array([0 if (n_w_rew + n_wo_rew) < self.thr else 1, self.alpha + n_w_rew, self.beta + n_wo_rew]).reshape((3, 1)).astype(self.beta_counters.dtype)
            self.beta_counters = np.c_[self.beta_counters, new_beta_col]
        if fitted_classifier is not None:
            if 'predict_proba' not in dir(fitted_classifier):
                fitted_classifier = _convert_decision_function_w_sigmoid(fitted_classifier)
            if partialfit:
                fitted_classifier = _add_method_predict_robust(fitted_classifier)
            self.algos.append(fitted_classifier)
        else:
            if self.force_fit or self.partialfit:
                if self.base is None:
                    raise ValueError("Must provide a classifier when initializing with different classifiers per arm.")
                self.algos.append( deepcopy(self.base) )
            else:
                if (self.force_counters) or (self.thr > 0 and not self.force_fit):
                    self.algos.append(_BetaPredictor(self.beta_counters[:, -1][1],
                                                     self.beta_counters[:, -1][2],
                                                     self.rng_arm[-1]))
                else:
                    self.algos.append(_ZeroPredictor())
        if (self.buffer is not None):
            self.buffer.append(_RefitBuffer(self.refit_buffer, self.deep_copy,
                                            self.rng_arm[-1]))
            if (buffer_X is not None):
                self.buffer[-1].add_obs(bufferX, buffer_y) 
Example #29
Source File: search.py    From ConvLab with MIT License 5 votes vote down vote up
def build_config_space(spec):
    '''
    Build ray config space from flattened spec.search
    Specify a config space in spec using `"{key}__{space_type}": {v}`.
    Where `{space_type}` is `grid_search` of `ray.tune`, or any function name of `np.random`:
    - `grid_search`: str/int/float. v = list of choices
    - `choice`: str/int/float. v = list of choices
    - `randint`: int. v = [low, high)
    - `uniform`: float. v = [low, high)
    - `normal`: float. v = [mean, stdev)

    For example:
    - `"explore_anneal_epi__randint": [10, 60],` will sample integers uniformly from 10 to 60 for `explore_anneal_epi`,
    - `"lr__uniform": [0.001, 0.1]`, and it will sample `lr` using `np.random.uniform(0.001, 0.1)`

    If any key uses `grid_search`, it will be combined exhaustively in combination with other random sampling.
    '''
    space_types = ('grid_search', 'choice', 'randint', 'uniform', 'normal')
    config_space = {}
    for k, v in util.flatten_dict(spec['search']).items():
        key, space_type = k.split('__')
        assert space_type in space_types, f'Please specify your search variable as {key}__<space_type> in one of {space_types}'
        if space_type == 'grid_search':
            config_space[key] = tune.grid_search(v)
        elif space_type == 'choice':
            config_space[key] = tune.sample_from(lambda spec, v=v: random.choice(v))
        else:
            np_fn = getattr(np.random, space_type)
            config_space[key] = tune.sample_from(lambda spec, v=v: np_fn(*v))
    return config_space 
Example #30
Source File: utils.py    From contextualbandits with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _score_rnd(self, X):
        if not self.ts_byrow:
            chosen_sample = self.random_state.integers(self.nsamples)
            return self._get_score(chosen_sample, X)
        else:
            pred = self._pred_by_sample(X)
            if not self.ts_weighted:
                return pred[np.arange(X.shape[0]),
                            self.random_state.integers(self.nsamples, size=X.shape[0])]
            else:
                w = self.random_state.random(size = (X.shape[0], self.nsamples))
                w[:] /= w.sum(axis=0, keepdims=True)
                return np.einsum("ij,ij->i", w, pred)