python类getstate()的实例源码

utils.py 文件源码 项目:guillotina 作者: plone 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def get_random_string(length: int=30,
                      allowed_chars: str=string.ascii_letters + string.digits) -> str:
    """
    Heavily inspired by Plone/Django
    Returns a securely generated random string.
    """
    if not using_sys_random:
        # do our best to get secure random without sysrandom
        seed_value = "%s%s%s" % (random.getstate(), time.time(), RANDOM_SECRET)
        random.seed(sha(seed_value).digest())
    return ''.join([random.choice(allowed_chars) for i in range(length)])
cm_noiseChannels.py 文件源码 项目:CrowdMaster 作者: johnroper100 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def agentRandom(self, offset=0):
        """Return a random number that is consistent between frames but can
        be offset by an integer"""
        state = random.getstate()
        random.seed(hash(self.userid) - 1 + offset)
        # -1 so that this number is different to the first random number
        # generated on frame 0 (if used) of the simulation
        result = random.random()
        random.setstate(state)
        return result
state.py 文件源码 项目:PonyGE2 作者: PonyGE 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def create_state(individuals):
    """
    Create a dictionary representing the current state of an evolutionary
    run. The state includes the current population, the current random state,
    the parameters dictionary, the stats dictionary, and all lists in the
    utilities.stats.trackers module.

    :param individuals: A population of individuals to be saved.
    :return: The complete state of a run.
    """

    from algorithm.parameters import params
    from stats.stats import stats
    from utilities.stats import trackers
    from time import time

    # Get time.
    state_time = time()

    # Get random state.
    random_state = random.getstate()

    # Create a picklable version of the params dictionary. Since the params
    # dictionary contains functions and class instances, we need to replace
    # these with the names of their respective modules, since module
    # instances are not picklable.
    pickle_params = {param: (check_name(params[param]) if callable(
        params[param]) else params[param]) for param in params}

    # Create a picklable version of the trackers module.
    pickle_trackers = {i: getattr(trackers, i) for i in dir(trackers)
                       if not i.startswith("__")}

    # Create state dictionary
    state = {"trackers": pickle_trackers, "params": pickle_params,
             "stats": stats, "individuals": individuals,
             "random_state": random_state, "time": state_time}

    save_state(state)
util_lstm_seqlabel.py 文件源码 项目:neural_wfst 作者: se4u 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def shuffle(lol):
    '''
    shuffle inplace each list in the same order by ensuring that we
    use the same state for every run of shuffle.

    lol :: list of list as input
    '''
    state = random.getstate()
    for l in lol:
        random.setstate(state)
        random.shuffle(l)
utils.py 文件源码 项目:tfdnn-kaldi 作者: dreaming-dog 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def shuffle_together(self, mats):
        """

        :param mats: shuffles the given matrices and maintains the same 'shuffled order' in all matrices
        """
        rng = random.getstate()
        for mat in mats:
            random.setstate(rng) # reset random state to the saved state to get the same 'shuffled order' as previous shuffling
            random.shuffle(mat)
algorithm.py 文件源码 项目:gps 作者: cbfinn 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def __getstate__(self):
        state = self.__dict__.copy()
        state['_random_state'] = random.getstate()
        state['_np_random_state'] = np.random.get_state()
        return state

    # For unpickling.
c6_sgd.py 文件源码 项目:Sentiment-Analysis 作者: AliceDudu 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def save_params(iter, params):
    with open("saved_params_%d.npy" % iter, "w") as f:
        pickle.dump(params, f)
        pickle.dumpy(random.getstate(), f)
c2_gradcheck.py 文件源码 项目:Sentiment-Analysis 作者: AliceDudu 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def gradcheck_naive(f, x):

    #Return an object capturing the current internal state of the generator
    rndstate = random.getstate()            #why use state??????
    random.setstate(rndstate)
    fx, grad = f(x)                         #fx=np.sum(x ** 2), grad=x * 2 
    h = 1e-4

    #Efficient multi-dimensional iterator object to iterate over arrays
    # Iterate over all indexes in x
    it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])    

    while not it.finished:
        ix = it.multi_index                 #starts from (0, 0) then (0, 1)

        x[ix] += h                          #To calculate [f(xi+h)-f(xi-h)] / 2h
        random.setstate(rndstate)
        fxh, _ = f(x)
        x[ix] -= 2*h
        random.setstate(rndstate)
        fxnh, _ = f(x)
        x[ix] += h
        numgrad = (fxh - fxnh) / 2 / h
                                            #To compare gradient calculated by formular and calculus
        reldiff = abs(numgrad - grad[ix]) / max(1, abs(numgrad), abs(grad[ix]))
        if reldiff > 1e-5:
            print "Gradient check failed."
            print "First gradient error found at index %s" % str(ix)
            print "Your gradient: %f \t Numerical gradient: %f" % (grad[ix], numgrad)
            return

        it.iternext()

    print "Gradient check passed"
alg.py 文件源码 项目:GAKeras 作者: PetraVidnerova 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def myEASimple(population, start_gen, toolbox, cxpb, mutpb, ngen, 
               stats, halloffame, logbook, verbose, id=None):

    total_time = datetime.timedelta(seconds=0) 
    for gen in range(start_gen, ngen):
        start_time = datetime.datetime.now()
        population = algorithms.varAnd(population, toolbox, cxpb=cxpb, mutpb=mutpb)

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in population if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        halloffame.update(population)
        record = stats.compile(population)
        logbook.record(gen=gen, evals=len(invalid_ind), **record)
        if verbose:
            print(logbook.stream)


        population = toolbox.select(population, k=len(population))

        if gen % 1 == 0:
            # Fill the dictionary using the dict(key=value[, ...]) constructor
            cp = dict(population=population, generation=gen, halloffame=halloffame,
                      logbook=logbook, rndstate=random.getstate())
            if id is None:
                cp_name = "checkpoint_ea.pkl"
            else:
                cp_name = "checkpoint_ea_{}.pkl".format(id)
            pickle.dump(cp, open(cp_name, "wb"))

        gen_time = datetime.datetime.now() - start_time
        total_time = total_time + gen_time
        #print("Time ", total_time)
        if total_time > datetime.timedelta(hours=4*24):
            print("Time limit exceeded.")
            break 

    return population, logbook
genesis.py 文件源码 项目:stargateRL 作者: thee-engineer 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def __init__(self, settings=None):
        """Construct the biomes using elevation and moisture."""
        if settings['homeworld']:
            self.width = settings['homeworld']['width']
            self.height = settings['homeworld']['height']
        else:
            self.width = random.randint(settings['min_size'],
                                        settings['max_size'])
            self.height = self.width

        logger.info(
            'Created PlanetGenerator %dx%d', self.width, self.height)

        if settings['homeworld']:
            elv_profile = read_profile(settings['homeworld']['elevation'],
                                       'elv')
            mst_profile = read_profile(settings['homeworld']['moisture'],
                                       'mst')
        else:
            elv_profile = read_profile('random', 'elv')
            mst_profile = read_profile('random', 'mst')

        # Generate map data using noise
        logger.debug('Creating elevation')
        self._generator_elevation = NoiseGenerator(self.width, self.height,
                                                   elv_profile)
        logger.debug('Creating moisture')
        self._generator_moisture = NoiseGenerator(self.width, self.height,
                                                  mst_profile)
        logger.debug('Creating biomes')
        self._data_biomes = self.generate_biomes()

        logger.debug('Computing hash of random state')
        self._hash = hashlib.sha256(str(random.getstate())).hexdigest()
q2_gradcheck.py 文件源码 项目:dl4nlp-stanford 作者: cioionut 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def gradcheck_naive(f, x):
    """
    Gradient check for a function f
    - f should be a function that takes a single argument and outputs the cost and its gradients
    - x is the point (numpy array) to check the gradient at
    """

    rndstate = random.getstate()
    random.setstate(rndstate)
    fx, grad = f(x) # Evaluate function value at original point
    h = 1e-6

    # Iterate over all indexes in x
    it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
    while not it.finished:
        ix = it.multi_index

        ### try modifying x[ix] with h defined above to compute numerical gradients
        ### make sure you call random.setstate(rndstate) before calling f(x) each time, this will make it
        ### possible to test cost functions with built in randomness later
        old_ix = x[ix]
        x[ix] += h
        random.setstate(rndstate)
        fxh, _ = f(x)
        numgrad = (fxh - fx) / (x[ix] - (x[ix] - h))
        x[ix] = old_ix
        # Compare gradients
        reldiff = abs(numgrad - grad[ix]) / max(1, abs(numgrad), abs(grad[ix]))
        if reldiff > 1e-5:
            print "Gradient check failed."
            print "First gradient error found at index %s" % str(ix)
            print "Your gradient: %f \t Numerical gradient: %f" % (grad[ix], numgrad)
            return

        it.iternext() # Step to next dimension

    print "Gradient check passed!"
q3_sgd.py 文件源码 项目:dl4nlp-stanford 作者: cioionut 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def save_params(iter, params):
    with open("saved_params_%d.npy" % iter, "w") as f:
        pickle.dump(params, f)
        pickle.dump(random.getstate(), f)
util_test.py 文件源码 项目:bloomfilter-py 作者: seomoz 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_derandomize_allows_exceptions(self):
        '''derandomize propagates exception, but restores random state'''
        state = random.getstate()
        with self.assertRaises(ValueError):
            with bloomfilter.util.derandomize(234):
                raise ValueError('boom!')
        self.assertEqual(random.getstate(), state)
coin.py 文件源码 项目:PACE-python 作者: mit-ll 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __init__(self, seed):
        random.seed(seed)
        self.state = random.getstate()
coin.py 文件源码 项目:PACE-python 作者: mit-ll 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def flip(self, *args):
        random.setstate(self.state)
        bit = super(SeededCoin, self).flip()
        self.state = random.getstate()
        return bit
coin.py 文件源码 项目:PACE-python 作者: mit-ll 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def reseed(self, seed):
        random.seed(seed)
        self.state = random.getstate()
general_encoding_player.py 文件源码 项目:hanabi 作者: chikinn 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def StartRandom(self,seed):
        # This function initializes the fixed seed random method used by
        # players. There is a concern that the use of this method, particularly
        # the setting of a fixed seed, may bias other functions which wish to 
        # call random. For this reason, the RNG state is recorded at the start 
        # of the player call; the RNG state will then be set back to this state
        # before the player concludes it's turn. This will prevent the fixed
        # seed method from interacting with other random calls outside of the
        # AI program.
        self.RNG_State = random.getstate()
        random.seed(seed)
test_it.py 文件源码 项目:pytest-randomly 作者: pytest-dev 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def test_fixtures_dont_interfere_with_tests_getting_same_random_state(ourtestdir):
    ourtestdir.makepyfile(
        test_one="""
        import random

        import pytest


        random.seed(2)
        state_at_seed_two = random.getstate()


        @pytest.fixture(scope='module')
        def myfixture():
            return random.random()


        @pytest.mark.one()
        def test_one(myfixture):
            assert random.getstate() == state_at_seed_two


        @pytest.mark.two()
        def test_two(myfixture):
            assert random.getstate() == state_at_seed_two
        """
    )
    args = ['--randomly-seed=2']

    out = ourtestdir.runpytest(*args)
    out.assert_outcomes(passed=2)

    out = ourtestdir.runpytest('-m', 'one', *args)
    out.assert_outcomes(passed=1)
    out = ourtestdir.runpytest('-m', 'two', *args)
    out.assert_outcomes(passed=1)
DistributedGameTable.py 文件源码 项目:POTCO-PS 作者: ksmit799 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def createAiPlayerName(self, female, seed):
        state = random.getstate()
        random.seed(seed)
        if female:
            first_name_array = PLocalizer.PirateNames_FirstNamesFemale
        else:
            first_name_array = PLocalizer.PirateNames_FirstNamesMale
        last_name_prefix_array = PLocalizer.PirateNames_LastNamePrefixesGeneric
        last_name_suffix_array = PLocalizer.PirateNames_LastNameSuffixesGeneric
        string = ''
        string = string + self.randomArraySelection(first_name_array) + ' '
        string = string + self.randomArraySelection(last_name_prefix_array)
        string = string + self.randomArraySelection(last_name_suffix_array)
        random.setstate(state)
        return string


问题


面经


文章

微信
公众号

扫码关注公众号