Python random 模块,setstate() 实例源码

我们从Python开源项目中,提取了以下46个代码示例,用于说明如何使用random.setstate()

项目:deeppavlov    作者:deepmipt    | 项目源码 | 文件源码
def __init__(self, opt, shared=None):
        """Initialize the parameters of the DefaultTeacher"""
        assert opt['train_part'] + opt['test_part'] + opt['valid_part'] == 1
        self.parts = [opt['train_part'], opt['valid_part'], opt['test_part']]
        # store datatype
        self.dt = opt['datatype'].split(':')[0]
        self.opt = opt
        opt['datafile'] = _path(opt)

        # store identifier for the teacher in the dialog
        self.id = 'ner_teacher'

        random_state = random.getstate()
        random.seed(opt.get('teacher_seed'))
        self.random_state = random.getstate()
        random.setstate(random_state)

        if shared and shared.get('metrics'):
            self.metrics = shared['metrics']
        else:
            self.metrics = CoNLLClassificationMetrics(opt['model_file'])

        # define standard question, since it doesn't change for this task
        super().__init__(opt, shared)
项目:deeppavlov    作者:deepmipt    | 项目源码 | 文件源码
def __init__(self, opt, shared=None):
        """Initialize the class accordint to given parameters in opt."""
        # store datatype
        self.datatype_strict = opt['datatype'].split(':')[0]

        opt['datafile'] = _path(opt)

        # store identifier for the teacher in the dialog
        self.id = 'insults_teacher'

        self.answer_candidates = ['Non-insult', "Insult"]

        random_state = random.getstate()
        random.seed(opt.get('teacher_random_seed'))
        self.random_state = random.getstate()
        random.setstate(random_state)

        super().__init__(opt, shared)

        if shared:
            self.observations = shared['observations']
            self.labels = shared['labels']
        else:
            self.observations = []
            self.labels = []
项目:FrankWolfe    作者:neu-spiral    | 项目源码 | 文件源码
def adapt_z_state(self,main_rdd, cinfo,beta):
        ainv = cinfo
        def Updatez(tpl):
            tt=[]
            for ((tx,lam,state,z),index) in tpl:
                random.setstate(state)
                p = random.random()
                state = random.getstate()
                if p<self.ptr:
                    znew=float(-np.matrix(tx)*ainv*np.matrix(tx).T)
                else:
                    znew=0.0
                z=(1-beta)*z+beta*znew
                tt.append(((tx,lam,state,z),index))
            return tt
        main_rdd = main_rdd.mapValues(Updatez).cache()
        return main_rdd
项目:xgovctf    作者:alphagov    | 项目源码 | 文件源码
def get_instance_number(pid, tid):
    """
    Maps the token to an instance number for a prolem.

    Args:
        pid: the problem id
        tid: the team id
    Returns:
        The instance number
    """

    previous_state = seed_generator(tid, pid)

    total_instances = get_number_of_instances(pid)
    if total_instances == 0:
        raise InternalException("{} has no instances.".format(pid))

    instance_number = random.randint(0, total_instances-1)
    random.setstate(previous_state)

    return instance_number
项目:lang2program    作者:kelvinguu    | 项目源码 | 文件源码
def random_seed(seed=None):
    """Execute code inside this with-block using the specified seed.

    If no seed is specified, nothing happens.

    Does not affect the state of the random number generator outside this block.
    Not thread-safe.

    Args:
        seed (int): random seed
    """
    if seed is None:
        yield
    else:
        py_state = random.getstate()  # save state
        np_state = np.random.get_state()

        random.seed(seed)  # alter state
        np.random.seed(seed)
        yield

        random.setstate(py_state)  # restore state
        np.random.set_state(np_state)
项目:lang2program    作者:kelvinguu    | 项目源码 | 文件源码
def random_seed(seed=None):
    """Execute code inside this with-block using the specified seed.

    If no seed is specified, nothing happens.

    Does not affect the state of the random number generator outside this block.
    Not thread-safe.

    Args:
        seed (int): random seed
    """
    if seed is None:
        yield
    else:
        py_state = random.getstate()  # save state
        np_state = np.random.get_state()

        random.seed(seed)  # alter state
        np.random.seed(seed)
        yield

        random.setstate(py_state)  # restore state
        np.random.set_state(np_state)
项目:coordinates    作者:markovmodel    | 项目源码 | 文件源码
def random_seed(seed=42):
    """ sets the random seed of Python within the context.

    Example
    -------
    >>> import random
    >>> with random_seed(seed=0):
    ...    random.randint(0, 1000) # doctest: +SKIP
    864
    """
    old_state = random.getstate()
    random.seed(seed)
    try:
        yield
    finally:
        random.setstate(old_state)
项目:bloomfilter-py    作者:seomoz    | 项目源码 | 文件源码
def derandomize(seed=123):
    '''
    Disable randomization for a block

    Since bloomfilter generates hash seeds randomly, it is inherently instable object.
    It considerably complicates testing. This helper addresses the issue:

        with bloomfilter.util.derandomize():
            bloom_filter_1 = BloomFilter(100, 0.1)

        with bloomfilter.util.derandomize():
            bloom_filter_2 = BloomFilter(100, 0.1)

    The resulting bloom_filters are stable between runs.
    '''
    state = random.getstate()
    try:
        random.seed(seed)
        yield
    finally:
        random.setstate(state)
项目:pytest-randomly    作者:pytest-dev    | 项目源码 | 文件源码
def _reseed(config, offset=0):
    seed = config.getoption('randomly_seed') + offset
    if seed not in random_states:
        random.seed(seed)
        random_states[seed] = random.getstate()
    else:
        random.setstate(random_states[seed])

    if have_factory_boy:
        factory_set_random_state(random_states[seed])

    if have_faker:
        faker_random.setstate(random_states[seed])

    if have_numpy:
        if seed not in np_random_states:
            np_random.seed(seed)
            np_random_states[seed] = np_random.get_state()
        else:
            np_random.set_state(np_random_states[seed])
项目:deeppavlov    作者:deepmipt    | 项目源码 | 文件源码
def reset(self):
        """Reset Teacher random states"""
        random_state = random.getstate()
        random.setstate(self.random_state)
        random.shuffle(self.data.data)
        self.random_state = random.getstate()
        random.setstate(random_state)

        self.lastY = None
        self.episode_idx = self.data_offset - self.step_size
        self.episode_done = True
        self.epochDone = False
        if not self.random and self.data_offset >= self.data.num_episodes():
            # could have bigger batchsize then episodes... so nothing to do
            self.epochDone = True
项目:deeppavlov    作者:deepmipt    | 项目源码 | 文件源码
def setup_data(self, path):
        """Read and iteratively yield data to agent"""
        print('loading: ' + path)

        questions = []
        y = []

        # open data file with labels
        # (path will be provided to setup_data from opt['datafile'] defined above)
        with open(path) as labels_file:
            context = csv.reader(labels_file)
            next(context)

            for item in context:
                label, text = item
                questions.append(text)
                y.append([self.answer_candidates[int(label)]])

        episode_done = True

        indexes = range(len(questions))
        if self.datatype_strict != 'test':
            random_state = random.getstate()
            random.setstate(self.random_state)
            kf_seed = random.randrange(500000)
            kf = KFold(self.opt.get('bagging_folds_number'), shuffle=True,
                       random_state=kf_seed)
            i = 0
            for train_index, test_index in kf.split(questions):
                indexes = train_index if self.datatype_strict == 'train' else test_index
                if i >= self.opt.get('bagging_fold_index', 0):
                    break
            self.random_state = random.getstate()
            random.setstate(random_state)

        # define iterator over all queries
        for i in indexes:
            # get current label, both as a digit and as a text
            # yield tuple with information and episode_done? flag
            yield (questions[i], y[i]), episode_done
项目:deeppavlov    作者:deepmipt    | 项目源码 | 文件源码
def reset(self):
        """Reset class, random state"""
        super().reset()

        random_state = random.getstate()
        random.setstate(self.random_state)
        random.shuffle(self.data.data)
        self.random_state = random.getstate()
        random.setstate(random_state)
项目:deeppavlov    作者:deepmipt    | 项目源码 | 文件源码
def reset(self):
        """Reset class and random state."""

        super().reset()

        random_state = random.getstate()
        random.setstate(self.random_state)
        random.shuffle(self.data.data)
        self.random_state = random.getstate()
        random.setstate(random_state)
项目:text    作者:pytorch    | 项目源码 | 文件源码
def use_internal_state(self):
        """Use a specific RNG state."""
        old_state = random.getstate()
        random.setstate(self._random_state)
        yield
        self._random_state = random.getstate()
        random.setstate(old_state)
项目:third_person_im    作者:bstadie    | 项目源码 | 文件源码
def set_seed_tmp(seed=None):
    if seed is None:
        yield
    else:
        state = random.getstate()
        np_state = np.random.get_state()
        random.seed(seed)
        np.random.seed(seed)
        yield
        np.random.set_state(np_state)
        random.setstate(state)
项目:cupy    作者:cupy    | 项目源码 | 文件源码
def do_teardown():
    global _old_python_random_state
    global _old_numpy_random_state
    global _old_cupy_random_states
    random.setstate(_old_python_random_state)
    numpy.random.set_state(_old_numpy_random_state)
    cupy.random.generator._random_states = _old_cupy_random_states
    _old_python_random_state = None
    _old_numpy_random_state = None
    _old_cupy_random_states = None


# In some tests (which utilize condition.repeat or condition.retry),
# setUp/tearDown is nested. _setup_random() and _teardown_random() do their
# work only in the outermost setUp/tearDown pair.
项目:CrowdMaster    作者:johnroper100    | 项目源码 | 文件源码
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
项目:changelog-cli    作者:mc706    | 项目源码 | 文件源码
def setUp(self):
        self.runner = CliRunner()
        os.environ.setdefault('LC_ALL', 'en_US.utf-8')
        os.environ.setdefault('LANG', 'en_US.utf-8')
        random.setstate(START)
项目:changelog-cli    作者:mc706    | 项目源码 | 文件源码
def test_determinism(self):
        # first pass
        with self.runner.isolated_filesystem():
            self.runner.invoke(cli, ['init'])
            self.runner.invoke(cli, ['new', "First Feature"])
            self.runner.invoke(cli, ['release', '--yes'])
            for i in range(random.randrange(10)):
                for j in range(random.randrange(3)):
                    line = random.choice(OPTIONS)
                    args = [line[0], line[1].format(random.random())]
                    self.runner.invoke(cli, args)
                self.runner.invoke(cli, ['release', '--yes'])
            with open("CHANGELOG.md", 'r') as first_pass_file:
                first_pass = first_pass_file.read()
        # reset random
        random.setstate(START)
        # second pass
        with self.runner.isolated_filesystem():
            self.runner.invoke(cli, ['init'])
            self.runner.invoke(cli, ['new', "First Feature"])
            self.runner.invoke(cli, ['release', '--yes'])
            for i in range(random.randrange(10)):
                for j in range(random.randrange(3)):
                    line = random.choice(OPTIONS)
                    args = [line[0], line[1].format(random.random())]
                    self.runner.invoke(cli, args)
                self.runner.invoke(cli, ['release', '--yes'])
            with open("CHANGELOG.md", 'r') as second_pass_file:
                second_pass = second_pass_file.read()
        self.assertEqual(first_pass, second_pass)
项目:sockeye    作者:awslabs    | 项目源码 | 文件源码
def load_checkpoint(self, directory: str, train_iter: data_io.BaseParallelSampleIter) -> _TrainingState:
        """
        Loads the full training state from disk. This includes optimizer,
        random number generators and everything needed.  Note that params
        should have been loaded already by the initializer.

        :param directory: directory where the state has been saved.
        :param train_iter: training data iterator.
        """

        # Optimzer state (from mxnet)
        opt_state_fname = os.path.join(directory, C.OPT_STATES_LAST)
        self.load_optimizer_states(opt_state_fname)

        # State of the bucket iterator
        train_iter.load_state(os.path.join(directory, C.BUCKET_ITER_STATE_NAME))

        # RNG states: python's random and np.random provide functions for
        # storing the state, mxnet does not, but inside our code mxnet's RNG is
        # not used AFAIK
        with open(os.path.join(directory, C.RNG_STATE_NAME), "rb") as fp:
            random.setstate(pickle.load(fp))
            np.random.set_state(pickle.load(fp))

        # Monitor state, in order to get the full information about the metrics
        self.training_monitor.load_state(os.path.join(directory, C.MONITOR_STATE_NAME))

        # And our own state
        return self.load_state(os.path.join(directory, C.TRAINING_STATE_NAME))
项目:neural_wfst    作者:se4u    | 项目源码 | 文件源码
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)
项目:tfdnn-kaldi    作者:dreaming-dog    | 项目源码 | 文件源码
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)
项目:gps    作者:cbfinn    | 项目源码 | 文件源码
def __setstate__(self, state):
        self.__dict__ = state
        random.setstate(state.pop('_random_state'))
        np.random.set_state(state.pop('_np_random_state'))
项目:Sentiment-Analysis    作者:AliceDudu    | 项目源码 | 文件源码
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"
项目:GAKeras    作者:PetraVidnerova    | 项目源码 | 文件源码
def main(id, checkpoint_name=None):
    # random.seed(64)

    if checkpoint_name:
        # A file name has been given, then load the data from the file
        cp = pickle.load(open(checkpoint_name, "rb"))
        pop = cp["population"]
        start_gen = cp["generation"] + 1
        hof = cp["halloffame"]
        logbook = cp["logbook"]
        random.setstate(cp["rndstate"])
    else:
        pop = toolbox.population(n=Config.pop_size)
        start_gen = 0
        hof = tools.HallOfFame(1)
        logbook = tools.Logbook()

    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", np.mean)
    stats.register("std", np.std)
    stats.register("min", np.min)
    stats.register("max", np.max)

    pop, log = paralg.myAsyncEA(pop, start_gen, toolbox, cxpb=0.6, mutpb=0.2, ngen=Config.ngen, 
                              stats=stats, halloffame=hof, logbook=logbook, verbose=True,
                              id=id)

    return pop, log, hof
项目:GAKeras    作者:PetraVidnerova    | 项目源码 | 文件源码
def main(id, checkpoint_name=None):
    # random.seed(64)

    if checkpoint_name:
        # A file name has been given, then load the data from the file
        cp = pickle.load(open(checkpoint_name, "rb"))
        pop = cp["population"]
        start_gen = cp["generation"] + 1
        hof = cp["halloffame"]
        logbook = cp["logbook"]
        random.setstate(cp["rndstate"])
    else:
        pop = toolbox.population(n=Config.pop_size)
        start_gen = 0
        hof = tools.HallOfFame(1)
        logbook = tools.Logbook()

    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", np.mean)
    stats.register("std", np.std)
    stats.register("min", np.min)
    stats.register("max", np.max)

    pop, log = alg.myEASimple(pop, start_gen, toolbox, cxpb=0.6, mutpb=0.2, ngen=Config.ngen, 
                              stats=stats, halloffame=hof, logbook=logbook, verbose=True,
                              id=id)

    return pop, log, hof
项目:GAKeras    作者:PetraVidnerova    | 项目源码 | 文件源码
def main(id, checkpoint_name=None):
    # random.seed(64)

    if checkpoint_name:
        # A file name has been given, then load the data from the file
        cp = pickle.load(open(checkpoint_name, "rb"))
        pop = cp["population"]
        start_gen = cp["generation"] + 1
        hof = cp["halloffame"]
        logbook = cp["logbook"]
        random.setstate(cp["rndstate"])
    else:
        pop = toolbox.population(n=Config.MU)
        start_gen = 0
        hof = tools.HallOfFame(1)
        logbook = tools.Logbook()

    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", np.mean)
    stats.register("std", np.std)
    stats.register("min", np.min)
    stats.register("max", np.max)

    pop, log = alg.myEAMuCommaLambda(pop, start_gen, toolbox, Config.MU, Config.LAMBDA,
                                     cxpb=0.6, mutpb=0.2, ngen=Config.ngen,
                                     stats=stats, halloffame=hof, logbook=logbook, verbose=True,
                                     id=id)

    return pop, log, hof
项目:GAKeras    作者:PetraVidnerova    | 项目源码 | 文件源码
def main(id, checkpoint_name=None):
    # random.seed(64)

    if checkpoint_name:
        # A file name has been given, then load the data from the file
        cp = pickle.load(open(checkpoint_name, "rb"))
        pop = cp["population"]
        start_gen = cp["generation"] + 1
        hof = cp["halloffame"]
        logbook = cp["logbook"]
        random.setstate(cp["rndstate"])
    else:
        pop = toolbox.population(n=Config.pop_size)
        start_gen = 0
        hof = tools.HallOfFame(1)
        logbook = tools.Logbook()

    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", np.mean)
    stats.register("std", np.std)
    stats.register("min", np.min)
    stats.register("max", np.max)

    pop, log = paralg.myAsyncEA(pop, start_gen, toolbox, cxpb=0.6, mutpb=0.2, ngen=Config.ngen, 
                                stats=stats, halloffame=hof, logbook=logbook, verbose=True,
                                id=id)

    return pop, log, hof
项目:dl4nlp-stanford    作者:cioionut    | 项目源码 | 文件源码
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!"
项目:PACE-python    作者:mit-ll    | 项目源码 | 文件源码
def flip(self, *args):
        random.setstate(self.state)
        bit = super(SeededCoin, self).flip()
        self.state = random.getstate()
        return bit
项目:hanabi    作者:chikinn    | 项目源码 | 文件源码
def EndRandom(self):
        # This function is responsible for returning the RNG to the same state
        # as when it entered the AI        
        random.setstate(self.RNG_State)
项目:POTCO-PS    作者:ksmit799    | 项目源码 | 文件源码
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
项目:httpolice    作者:vfaronov    | 项目源码 | 文件源码
def test_fuzz(i):
    orig_state = random.getstate()
    random.seed(123456789 + i)      # Some arbitrary, but deterministic number.
    exch = make_exchange()
    random.setstate(orig_state)

    check_exchange(exch)
    text_report([exch], six.BytesIO())
    html_report([exch], six.BytesIO())
项目:adel    作者:openalea-incubator    | 项目源码 | 文件源码
def reinit_random_state():
    global initial_random_state
    random.setstate(initial_random_state)
项目:SICPinPython    作者:kimochg    | 项目源码 | 文件源码
def make_random_strat():
    """Makes a random pure strategy."""
    seed = random.randrange(0, 2 ** 31)

    def random_strat(score, opponent_score):
        # Save the state of the random generator, so strategy calls don't
        # impact dice rolls.
        state = random.getstate()
        random.seed(hash((score, opponent_score, seed)))
        roll = random.randrange(0, 11)
        random.setstate(state)
        return roll
    return random_strat
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def downsample(self, fraction_to_retain, random_seed, verbose=False):

        random_state = random.getstate()
        random.seed(random_seed)
        try:
            number_to_retain = \
                max(int(round(float(len(self._scenarios)*fraction_to_retain))), 1)
            random_list=random.sample(range(len(self._scenarios)), number_to_retain)

            scenario_bundle_list = []
            for i in xrange(number_to_retain):
                scenario_bundle_list.append(self._scenarios[random_list[i]]._name)

            if verbose:
                print("Downsampling scenario tree - retained %s "
                      "scenarios: %s"
                      % (len(scenario_bundle_list),
                         str(scenario_bundle_list)))

            self.compress(scenario_bundle_list) # do the downsampling
        finally:
            random.setstate(random_state)


    #
    # returns the root node of the scenario tree
    #
项目:cs224d    作者:kkihara    | 项目源码 | 文件源码
def simplified_data(num_train, num_dev, num_test):
    rndstate = random.getstate()
    random.seed(0)
    trees = loadTrees('train') + loadTrees('dev') + loadTrees('test')

    #filter extreme trees
    pos_trees = [t for t in trees if t.root.label==4]
    neg_trees = [t for t in trees if t.root.label==0]

    #binarize labels
    binarize_labels(pos_trees)
    binarize_labels(neg_trees)

    #split into train, dev, test
    print len(pos_trees), len(neg_trees)
    pos_trees = sorted(pos_trees, key=lambda t: len(t.get_words()))
    neg_trees = sorted(neg_trees, key=lambda t: len(t.get_words()))
    num_train/=2
    num_dev/=2
    num_test/=2
    train = pos_trees[:num_train] + neg_trees[:num_train]
    dev = pos_trees[num_train : num_train+num_dev] + neg_trees[num_train : num_train+num_dev]
    test = pos_trees[num_train+num_dev : num_train+num_dev+num_test] + neg_trees[num_train+num_dev : num_train+num_dev+num_test]
    random.shuffle(train)
    random.shuffle(dev)
    random.shuffle(test)
    random.setstate(rndstate)


    return train, dev, test
项目:26-Cogs    作者:Twentysix26    | 项目源码 | 文件源码
def penis(self, *, user : discord.Member):
        """Detects user's penis length

        This is 100% accurate."""
        state = random.getstate()
        random.seed(user.id)
        dong = "8{}D".format("=" * random.randint(0, 30))
        random.setstate(state)
        await self.bot.say("Size: " + dong)
项目:deeppavlov    作者:deepmipt    | 项目源码 | 文件源码
def setup_data(self, path):
        """Read and iteratively yield data to an agent."""

        print('loading: ' + path)

        questions = []
        y = []

        # open data file with labels
        # (path will be provided to setup_data from opt['datafile'] defined above)
        with open(path) as labels_file:
            tsv_reader = csv.reader(labels_file, delimiter='\t')

            for row in tsv_reader:
                if len(row) != 3:
                    print('Warn: expected 3 columns in a tsv row, got ' + str(row))
                    continue
                y.append(['??' if row[0] == '1' else '???'])
                questions.append(row[1] + '\n' + row[2])

        episode_done = True
        if not y:
            y = [None for _ in range(len(questions))]

        indexes = range(len(questions))
        if self.datatype_strict != 'test':
            random_state = random.getstate()
            random.setstate(self.random_state)
            kf_seed = random.randrange(500000)
            kf = KFold(self.opt.get('bagging_folds_number'), shuffle=True,
                       random_state=kf_seed)
            i = 0
            for train_index, test_index in kf.split(questions):
                indexes = train_index if self.datatype_strict == 'train' else test_index
                if i >= self.opt.get('bagging_fold_index', 0):
                    break
            self.random_state = random.getstate()
            random.setstate(random_state)

        # define iterator over all queries
        for i in indexes:
            # get current label, both as a digit and as a text
            # yield tuple with information and episode_done? flag
            yield (self.question + "\n" + questions[i], y[i]), episode_done
项目:CrowdMaster    作者:johnroper100    | 项目源码 | 文件源码
def evaluate(self):
        act = self.actionName
        if self.syncState:
            possible = False
            for sInp in self.inputs:
                if self.neurons[sInp].isCurrent:
                    possible = True
                    break

            if not possible or len(self.valueInputs) == 0:
                self.finalValue = 0
                self.finalValueCalcd = True
                return

            sm = self.brain.sim.syncManager
            userid = self.brain.userid

            for inp in self.valueInputs:
                vals = self.neurons[inp].evaluate()
                for key, v in vals.items():
                    if self.settings["RandomInput"]:
                        val = v + (self.settings["ValueDefault"] *
                                   v * random.random())
                    else:
                        val = v + (v * self.settings["ValueDefault"])
                    if val > 0:
                        if self.isGroup():
                            acNm = self.actionName
                            for act in self.brain.sim.actionGroups[acNm[1:-1]]:
                                sm.tell(userid, key, act, val, self.name)
                        else:
                            sm.tell(userid, key, self.actionName,
                                    val, self.name)

            (state, action), pairedAgent = sm.getResult(userid)

            if state == self.name:
                self.finalValue = 1
                self.action = action
            else:
                self.finalValue = 0
            self.finalValueCalcd = True
        elif self.isGroup():
            State.evaluate(self)
            acNm = self.actionName
            state = random.getstate()
            if not self.randomActionFromGroup:
                random.seed(hash(self.brain.userid))
            self.action = random.choice(
                self.brain.sim.actionGroups[acNm[1:-1]])
            random.setstate(state)
        else:
            State.evaluate(self)
            self.action = self.actionName
项目:PonyGE2    作者:PonyGE    | 项目源码 | 文件源码
def set_state(state):
    """
    Given a dictionary representing the state of an evolutionary run, set all
    aspects of the system to re-create that state. 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.

    Sets all aspects of the system and then returns a population of
    individuals at the current generation.

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

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

    # Set random state.
    random.setstate(state['random_state'])

    # Set stats.
    for stat in state['stats']:
        stats[stat] = state['stats'][stat]

    # Set trackers.
    for tracker in state['trackers']:
        setattr(trackers, tracker, state['trackers'][tracker])

    # Set parameters.
    for param in state['params']:
        params[param] = state['params'][param]

    # Set correct param imports for specified function options, including
    # error metrics and fitness functions.
    set_param_imports()

    # Set time adjustment to account for old time.
    stats['time_adjust'] = time() - state['time']

    return state['individuals']
项目:Sentiment-Analysis    作者:AliceDudu    | 项目源码 | 文件源码
def sgd(f, x0, step, iterations, postprocessing = None, useSaved = False, PRINT_EVERY = 10):
    """ Stochastic Gradient Descent """
    ANNEAL_EVERY = 20000

    if useSaved:
        start_iter, oldx, state = load_saved_params()
        if start_iter > 0:
            x0 = oldx;
            step *= 0.5 ** (start_iter / ANNEAL_EVERY)

        if state:
            random.setstate(state)

    else:
        start_iter = 0

    x = x0

    if not postprocessing:
        postprocessing = lambda x: x

    expcost = None

    for iter in xrange(start_iter + 1, iterations + 1):

        cost = None

        cost, grad = f(x)
        x -= step * grad

        x = postprocessing(x)

        if iter % PRINT_EVERY == 0:
            if not expcost:
                expcost = cost
            else:
                expcost = .95 * expcost + .05 * cost
            print "iter %d: %f" % (iter, expcost)

        if iter % SAVE_PARAMS_EVERY == 0 and useSaved:
            save_params(iter, x)

        if iter % ANNEAL_EVERY == 0:
            step *= 0.5

    return x
项目:cs224n_assignment    作者:xiaohu2015    | 项目源码 | 文件源码
def gradcheck_naive(f, x):
    """ Gradient check for a function f.

    Arguments:
    f -- a function that takes a single argument and outputs the
         cost and its gradients
    x -- 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-4        # Do not change this!

    # 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.

        ### YOUR CODE HERE:
        old_value = x[ix]
        random.setstate(rndstate)
        x[ix] = old_value + h
        fxh_left, _ = f(x)
        random.setstate(rndstate)
        x[ix] = old_value - h
        fxh_right, _ = f(x)
        numgrad = (fxh_left - fxh_right) / (2 * h)
        x[ix] = old_value
        ### END YOUR CODE

        # 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!")
项目:CS224n    作者:akash9182    | 项目源码 | 文件源码
def gradcheck_naive(f, x):
    """ Gradient check for a function f.

    Arguments:
    f -- a function that takes a single argument and outputs the
         cost and its gradients
    x -- 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-4        # Do not change this!

    # 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.

        ### YOUR CODE HERE:
        old_xix = x[ix]
        x[ix] = old_xix  + h 
        random.setstate(rndstate)
        fp = f(x)[0] 
        x[ix] = old_xix - h        
        random.setstate(rndstate)
        fo = f(x)[0]
        numgrad = (fp - fo)/ (2*h)
        x[ix] = old_xix

        ### END YOUR CODE

        # 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!")
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def create_random_bundles(self,
                              scenario_tree_instance,
                              num_bundles,
                              random_seed):

        random_state = random.getstate()
        random.seed(random_seed)
        try:
            num_scenarios = len(self._scenarios)

            sequence = list(range(num_scenarios))
            random.shuffle(sequence)

            scenario_tree_instance.Bundling[None] = True

            next_scenario_index = 0

            # this is a hack-ish way to re-initialize the Bundles set of a
            # scenario tree instance, which should already be there
            # (because it is defined in the abstract model).  however, we
            # don't have a "clear" method on a set, so...
            scenario_tree_instance.del_component("Bundles")
            scenario_tree_instance.add_component("Bundles", Set(ordered=True))
            for i in xrange(1, num_bundles+1):
                bundle_name = "Bundle"+str(i)
                scenario_tree_instance.Bundles.add(bundle_name)

            # ditto above comment regarding del_component/add_component
            scenario_tree_instance.del_component("BundleScenarios")
            scenario_tree_instance.add_component("BundleScenarios",
                                                 Set(scenario_tree_instance.Bundles,
                                                     ordered=True))

            bundles = []
            for i in xrange(num_bundles):
                bundle_name = "Bundle"+str(i+1)
                tmp = Set(ordered=True)
                tmp.construct()
                scenario_tree_instance.BundleScenarios[bundle_name] = tmp
                bundles.append(scenario_tree_instance.BundleScenarios[bundle_name])

            scenario_index = 0
            while (scenario_index < num_scenarios):
                for bundle_index in xrange(num_bundles):
                    if (scenario_index == num_scenarios):
                        break
                    bundles[bundle_index].add(
                        self._scenarios[sequence[scenario_index]]._name)
                    scenario_index += 1

            self._construct_scenario_bundles(scenario_tree_instance)
        finally:
            random.setstate(random_state)

    #
    # a utility function to pretty-print the static/non-cost
    # information associated with a scenario tree
    #
项目:cs224d    作者:kkihara    | 项目源码 | 文件源码
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-4

    # 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
        ### YOUR CODE HERE:
        x_ = x.copy()
        random.setstate(rndstate)  
        x_[ix] += h
        fx1, _ = f(x_)
        random.setstate(rndstate)
        x_ = x.copy()
        x_[ix] -= h
        fx2 , _ = f(x_)
        numgrad = (fx1 - fx2) / (2 * h)
        ### END YOUR CODE

        # 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!"