Python numpy.random 模块,choice() 实例源码

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

项目:facebook-message-analysis    作者:szheng17    | 项目源码 | 文件源码
def generate(cfd, start_word, n):
        word = start_word
        words = []
        for i in range(n):
            words.append(word)
            # word = cfd[word].max()
            fd = cfd[word]
            n_next_words = sum(fd.values())
            if n_next_words > 0:
                probabilities = [fd[w]/float(n_next_words) for w in sorted(fd.keys())]
                word = choice(sorted(fd.keys()), p=probabilities)
            else:
                # Pick random word
                old_word = word
                # TODO: use unigram probabilities later
                word = choice(cfd.keys())
        words.append(word)
        sentence = ' '.join(words)
        # TODO: modify above for punctuation
        return sentence
项目:adversarial-frcnn    作者:xiaolonw    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:fast-rcnn-distillation    作者:xiaolonw    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:faster-rcnn-resnet    作者:Eniac-Xie    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:mugen    作者:scherroman    | 项目源码 | 文件源码
def sample(self, duration: float) -> Segment:
        """
        Randomly samples a segment with the specified duration

        Parameters
        ----------
        duration
            duration of the sample

        Returns
        -------
        A randomly sampled segment with the specified duration
        """
        selected_source = choice(self.sources, p=self.sources.normalized_weights)
        sample = selected_source.sample(duration)

        return sample
项目:mugen    作者:scherroman    | 项目源码 | 文件源码
def sample(self, duration: float) -> VideoSegment:
        """
        Randomly samples a video segment with the specified duration.

        Parameters
        ----------
        duration
            duration of the video segment to sample
        """
        if self.time_boundaries:
            # Select a random time boundary to sample from, weighted by duration
            time_ranges = [TimeRange(*boundary) for boundary in self.time_boundaries]
            time_ranges = [time_range for time_range in time_ranges if time_range.duration >= duration]
            total_duration = sum([time_range.duration for time_range in time_ranges])
            time_range_weights = [time_range.duration / total_duration for time_range in time_ranges]
            time_range_to_sample = time_ranges[choice(len(time_ranges), p=time_range_weights)]
        else:
            time_range_to_sample = TimeRange(0, self.segment.duration)

        start_time = random.uniform(time_range_to_sample.start, time_range_to_sample.end - duration)
        sampled_clip = self.segment.subclip(start_time, start_time + duration)

        return sampled_clip
项目:reddit-iambic-pentameter    作者:pmichel31415    | 项目源码 | 文件源码
def find_rhyming_verse(self, rhyme, verse=None):
        """Finds a random verse that rhymes with the input"""
        # Get all rhyming verses
        rhyming_verses = self.rhymes[rhyme]
        # Until we have a different verse
        # if verse is None this will make sure the rhyming verse is different
        # TODO: this is not the best way to ensure the verses are different
        # it is however relatively simple and works in most cases
        # Sample 4 candidate verses
        num_candidates = min(4, len(rhyming_verses))
        candidate_ids = npr.choice(rhyming_verses, size=num_candidates, replace=False)
        candidates = [self.verses[i] for i in candidate_ids]
        if verse is not None:
            # Select the first candidate with a different last word
            for v in candidates:
                if poetry.last_word(verse) != poetry.last_word(v):
                    return v
        # If all the candidates have the same last word, just go with it
        return candidates[-1]
项目:py-faster-rcnn-tk1    作者:joeking11829    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:py-faster-rcnn-resnet-imagenet    作者:tianzhi0549    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:VacuumAI    作者:avelkoski    | 项目源码 | 文件源码
def payoff(self,state,rewards=rewards):
        fwd,p90,n90 = self.orientation, (self.orientation + 90) % 360, (self.orientation - 90) % 360
        results=[]
        for orientation in [fwd,p90,n90]:
            reward = 0
            step = self.position + self.step[orientation]
            touchsense, photosense, infrasense = (False,False,False)
            if min(step) < 0 or max(step) > 7:
                touchsense = True
                reward -= rewards['touch']
            elif state[step[1],step[0]]==1:
                photosense = True
                reward += rewards['photo']
            elif (step == self.home).all():
                infrasense = True
                reward -= rewards['infra']
            else:
                reward -= rewards['move']
            results.append({'orientation':orientation,'reward':reward})
        rewards = array([x['reward'] for x in results])
        indices = array(where(rewards==rewards.max())[0])
        index = random.choice(indices)
        return results[index]
项目:face-py-faster-rcnn    作者:playerkk    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:deep-fashion    作者:zuowang    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:RPN    作者:hfut721    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:TattDL    作者:z-harry-sun    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:CRAFT    作者:byangderek    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:CRAFT    作者:byangderek    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:smarty    作者:openforcefield    | 项目源码 | 文件源码
def change_bond(self,env,bond):
        """
        Makes changes to the Bond object
        returns probability of making change
        """
        #TODO: if symmetry in change_atom works for AlkEthOH, figure out how to handle bonds
        # Can only make changes to the bond OR or AND types
        changeOR = random.choice([True, False], p = [0.7, 0.3])
        if changeOR:
            # Bonds only have OR bases (no ORdecorators)
            new_prob = self.change_ORbase(bond, self.BondORbases, self.BondORdecorators)
            return 0.7 * new_prob

        else: # change AND type
            new_prob = self.change_ANDdecorators(bond, self.BondANDdecorators)
            return new_prob * 0.3
项目:faster_rcnn_logo    作者:romyny    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:pyshgp    作者:erp12    | 项目源码 | 文件源码
def simplify_once(genome):
    """Silences or noops between 1 and 3 random genes.

    Parameters
    ----------
    genome : list of Genes
        List of Plush genes.
    """
    gn = deepcopy(genome)
    n = randint(1, 4)
    action = choice(['silent', 'noop'])
    if action == 'silent':
        silent_n_random_genes(gn, n)
    else:
        noop_n_random_genes(gn, n)
    return gn
项目:Faster_RCNN_Training_Toolkit    作者:VerseChow    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:KITTI-detection-OHEM    作者:manutdzou    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:SCFGP    作者:MaxInGaussian    | 项目源码 | 文件源码
def load_co2_data(prop=0.8):
    from sklearn.datasets import fetch_mldata
    from sklearn import cross_validation
    data = fetch_mldata('mauna-loa-atmospheric-co2').data
    X = data[:, [1]]
    y = data[:, 0]
    y = y[:, None]
    X = X.astype(np.float64)
    ntrain = y.shape[0]
    train_inds = npr.choice(range(ntrain), int(prop*ntrain), replace=False)
    valid_inds = np.setdiff1d(range(ntrain), train_inds)
    X_train, y_train = X[train_inds].copy(), y[train_inds].copy()
    X_valid, y_valid = X[valid_inds].copy(), y[valid_inds].copy()
    return X_train, y_train, X_valid, y_valid

############################ Training & Visualizing ############################
项目:pyson    作者:niklasf    | 项目源码 | 文件源码
def do_work(self, term, intention):
        category = random.choice(CATEGORY_NAMES)
        c, d = self.compute_change_probability()

        if schedule.tick >= START_TICK_LAST_STAGE:
            num_deleted = numpy.random.geometric(min(1, 1 / d * 2.2))
            num_updated = numpy.random.geometric(0.35)
            num_created = numpy.random.geometric(min(1, 1 / c))
        else:
            num_deleted = numpy.random.geometric(min(1, 1 / d * 1.6))
            num_updated = numpy.random.geometric(0.425)
            num_created = numpy.random.geometric(min(1, 1 / c * 1.7475))

        changed = []
        self.delete_files(num_deleted, category)
        changed += self.update_files(num_updated, category)
        changed += self.create_files(num_created, category)

        self.create_coupling(changed)

        self.bugfix()
        yield
项目:pyson    作者:niklasf    | 项目源码 | 文件源码
def do_some_work(self):
        random_day = self.get_random_day()
        category = random.choice(CATEGORY_NAMES)
        c, d = self.compute_change_probability()

        if random_day <= self.days:
            if schedule.tick >= START_TICK_LAST_STAGE:
                num_deleted = numpy.random.geometric(min(1, 1 / d * 1.1))
                num_updated = numpy.random.geometric(0.15)
                num_created = numpy.random.geometric(1 / c * 0.95)
            else:
                num_deleted = numpy.random.geometric(min(1, 1 / d))
                num_updated = numpy.random.geometric(0.25)
                num_created = numpy.random.geometric(1 / c * 0.6)

            changed = []
            self.delete_files(num_deleted, category)
            changed += self.update_files(num_updated, category)
            changed += self.create_files(num_created, category)

            self.create_coupling(changed)

            self.bugfix()
        elif random_day <= 6:
            self.intention_bugfix()
项目:pyson    作者:niklasf    | 项目源码 | 文件源码
def do_some_work(self):
        random_day = self.get_random_day()
        category = random.choice(CATEGORY_NAMES)
        c, d = self.compute_change_probability()

        if random_day <= self.days:
            num_deleted = numpy.random.geometric(min(1, 1 / d * 0.95))
            num_updated = numpy.random.geometric(0.2)
            num_created = numpy.random.geometric(min(1, 1 / c * 1.3))

            changed = []
            self.delete_files(num_deleted, category)
            changed += self.update_files(num_updated, category)
            changed += self.create_files(num_created, category)

            self.create_coupling(changed)

            self.bugfix()
        elif random_day <= 5:
            num_updated = numpy.random.geometric(0.3275)
            updated = self.update_files(num_updated, category)
            self.create_coupling(updated)
        elif random_day <= 7:
            self.intention_bugfix()
项目:alib    作者:vnep-approx    | 项目源码 | 文件源码
def _add_cactus_edges(self, req):
        sub_trees = [(req.graph["root"], list(req.nodes))]
        cycles = 0
        edges_on_cycle = set()
        while sub_trees and (cycles < self._raw_parameters["max_cycles"]):
            cycles += 1
            root_node, sub_tree = sub_trees.pop()
            i = random.choice(sub_tree)
            j = random.choice(sub_tree)
            while i == j or (i in req.get_out_neighbors(j)) or (j in req.get_out_neighbors(i)):
                i = random.choice(sub_tree)
                j = random.choice(sub_tree)
            if req.node[i]["layer"] > req.node[j]["layer"]:
                i, j = j, i  # make edges always point down the tree
            if random.random() < self._raw_parameters["probability"]:
                req.add_edge(i, j, self._edge_demand)
                edges_on_cycle.add((i, j))

                path_i = CactusRequestGenerator._path_to_root(req, i, root_node)
                path_j = CactusRequestGenerator._path_to_root(req, j, root_node)
                new_cycle = path_i.symmetric_difference(path_j)  # only edges on the path to the first common ancestor lie on cycle
                edges_on_cycle = edges_on_cycle.union(new_cycle)

                sub_trees = CactusRequestGenerator._list_nontrivial_allowed_subtrees(req, edges_on_cycle)
                random.shuffle(sub_trees)
项目:alib    作者:vnep-approx    | 项目源码 | 文件源码
def _get_average_cost_from_embedding_graph_randomly(self, req, shortest_paths):
        costs = [0 for _ in xrange(self._iterations)]
        average_factor = 1.0 / self._iterations
        for i in xrange(self._iterations):
            mapped_node = dict()
            costs[i] = 0.0
            for node in req.get_nodes():
                restrictions = req.get_allowed_nodes(node)
                if restrictions is None or len(restrictions) == 0:
                    restrictions = self._scenario.substrate.get_nodes()
                snode = random.choice(tuple(restrictions))
                mapped_node[node] = snode
                # mapping costs are negative to be consistent with mip calculation:
                costs[i] -= self._scenario.substrate.get_node_type_cost(snode, req.node[node]["type"]) * req.node[node]["demand"] * average_factor
            for edge in req.get_edges():
                n1, n2 = edge
                sn1 = mapped_node[n1]
                sn2 = mapped_node[n2]
                # mapping costs are negative to be consistent with mip calculation:
                costs[i] -= shortest_paths[sn1][sn2] * req.edge[edge]["demand"] * average_factor
        return sum(costs)
项目:lmkit    作者:jiangnanhugo    | 项目源码 | 文件源码
def sampler():
    K=8000000
    N=2000

    # Generating a random probability vector.
    probs=ram.dirichlet(np.ones(K),1).ravel()


    # Construct the table.
    J,q=alias_setup(probs)

    # Generate variates.
    start=time.time()
    X=np.zeros(N)
    for idx in xrange(N):
        X[idx]=alias_draw(J,q)
    print time.time()-start
    start = time.time()
    results=ram.choice(len(probs),N,p=probs)
    print time.time() - start
项目:ohem    作者:abhi2610    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:py-faster-rcnn-dockerface    作者:natanielruiz    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:fastmat    作者:EMS-TU-Ilmenau    | 项目源码 | 文件源码
def genGroundTruth(numN, numM, numK):
    '''
    '''

    arrX = np.zeros((numN, numM))

    for ii in range(numM):
        arrInd = int(0.1 * numN) + npr.choice(
            range(int(numN - 0.2 * numN)), numK, replace=False)
        arrX[arrInd, ii] = npr.uniform(1, 2, numK)

    return arrX


################################################################################
#                          CALCULATION SECTION
################################################################################
项目:PVANet-FACE    作者:twmht    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:CS101Scripts    作者:moneypro    | 项目源码 | 文件源码
def sendRandomNumber():
    import numpy as np
    import numpy.random as npr
    prob1 = np.array( range( 7575,7642 ) )
    prob2 = np.array( range( 5050,5098 ) )
    vals = np.zeros( (len(students),4),dtype=np.int16 )
    for i in range( vals.shape[0] ):
        vals[ i,0:2 ] = ( npr.choice( prob1, size=(2,), replace=False ) )
        vals[ i,2: ]  = ( npr.choice( prob2, size=(2,), replace=False ) )
    if len(students) == 0 or len(students[list(students.keys())[0]]) <= 2:
        print ("Error, not generated.")
    subject = "No-Reply: Four SECRET numbers for this week lab"
    _id = 0
    for netid, info in students.items():
        toAddr = netid + "@illinois.edu"
        content = "Your secret numbers are "+str(vals[_id])
        subprocess.call("mail -s '"+subject+"' "+toAddr+" <<<'"+content+"'", shell=True)
        _id +=1
项目:scene-graph-TF-release    作者:danfeiX    | 项目源码 | 文件源码
def _sample_bg_rois(roidb, num_bg_rois):
    """
    Sample rois from background
    """
    # label = class RoI has max overlap with
    labels = roidb['max_classes']
    overlaps = roidb['max_overlaps']

    bg_inds = np.where(((overlaps < cfg.TRAIN.BG_THRESH_HI) &
                       (overlaps >= cfg.TRAIN.BG_THRESH_LO)) |
                       (labels == 0))[0]
    bg_rois_per_this_image = np.minimum(num_bg_rois, bg_inds.size)
    if bg_inds.size > 0:
        bg_inds = npr.choice(bg_inds, size=bg_rois_per_this_image, replace=False)

    return bg_inds
项目:CAPTCHA_Identifier    作者:AlphaLFC    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:py-R-FCN    作者:YuwenXiong    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:SubCNN    作者:tanshen    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:DQN    作者:jjakimoto    | 项目源码 | 文件源码
def sample_state(self, batch_size, window_length, alpha=0.5, epsilon=0.05):
        # udpate priority when sampling
        if len(self.priority) > self.limit:
            self.priority = self.priority[-self.limit:]
        # draw random indexes such that is bigger than window_length to enough length data
        index_space = np.arange(window_length, self.nb_entries)
        # prioritized sample
        p = np.array(self.priority)[window_length:]
        p_tilde = p + np.ones(self.nb_entries - window_length) * np.mean(p) * epsilon
        p_tilde[-1] = np.mean(p)
        p_tilde = p_tilde ** alpha
        p_tilde = p_tilde / np.sum(p_tilde)
        batch_idx = choice(index_space, p=p_tilde, size=batch_size - 1)
        # take the newest data
        batch_idx = np.concatenate((batch_idx, [self.nb_entries]))
        assert len(batch_idx) == batch_size

        # create experiences
        state = np.array([[self.observations[i] for i in range(idx - window_length,idx)] for idx in batch_idx])
        return state
项目:neurotools    作者:michaelerule    | 项目源码 | 文件源码
def bootstrap_statistic_two_sided(statistic, test_population, null_population, ntrials=1000):
    '''
    Estimate pvalue using bootstrapping

    Parameters
    ----------

    Resturns
    --------
    '''
    n = len(test_population)
    null     = statistic(null_population)
    observed = statistic(test_population)
    T = observed-null
    delta = abs(T)
    null_samples = array([statistic(random.choice(null_population,n)) for i in xrange(ntrials)])
    null_delta   = abs(null_samples - null)    
    pvalue = mean(null_delta>delta)
    return pvalue
项目:cohorts    作者:hammerlab    | 项目源码 | 文件源码
def generate_random_missense_variants(num_variants=10, max_search=100000, reference="GRCh37"):
    """
    Generate a random collection of missense variants by trying random variants repeatedly.
    """
    variants = []
    for i in range(max_search):
        bases = ["A", "C", "T", "G"]
        random_ref = choice(bases)
        bases.remove(random_ref)
        random_alt = choice(bases)
        random_contig = choice(["1", "2", "3", "4", "5"])
        random_variant = Variant(contig=random_contig, start=randint(1, 1000000),
                                 ref=random_ref, alt=random_alt, ensembl=reference)
        try:
            effects = random_variant.effects()
            for effect in effects:
                if isinstance(effect, Substitution):
                    variants.append(random_variant)
                    break
        except:
            continue
        if len(variants) == num_variants:
            break
    return VariantCollection(variants)
项目:Neural-Learner-for-English-Language-Test    作者:taineleau    | 项目源码 | 文件源码
def data_api(spilt_rate):
    raw_sent = brown.sents()
    partial_data = raw_sent[:int(0.1*len(raw_sent))]

    data_x, data_y = prepare_0(partial_data, word2intdict)

    print 'len data_x', len(data_x), len(data_y)

    train_inds = npr.choice(range(len(data_x)), size = int((1 - spilt_rate) * len(data_x)), replace = False)
    X_train = []
    Y_train = []
    X_test = []
    Y_test = []
    print 'len train_inds', len(train_inds), len(data_x)
    for i in range(len(data_x)):
        if i in train_inds:
            #print 'trn', i
            X_train.append(data_x[i])
            Y_train.append(data_y[i])
        else :
            #print 'tst', i
            X_test.append(data_x[i])
            Y_test.append(data_y[i])
    print 'len X_train', len(X_train), len(X_test)
    return (X_train, Y_train), (X_test, Y_test)
项目:DeepSpell_temp    作者:surmenok    | 项目源码 | 文件源码
def add_noise_to_string(self, a_string, amount_of_noise):
        """Add some artificial spelling mistakes to the string"""
        if rand() < amount_of_noise * len(a_string):
            # Replace a character with a random character
            random_char_position = random_randint(len(a_string))
            a_string = a_string[:random_char_position] + random_choice(CHARS[:-1]) + a_string[random_char_position + 1:]
        if rand() < amount_of_noise * len(a_string):
            # Delete a character
            random_char_position = random_randint(len(a_string))
            a_string = a_string[:random_char_position] + a_string[random_char_position + 1:]
        if len(a_string) < MAX_INPUT_LEN and rand() < amount_of_noise * len(a_string):
            # Add a random character
            random_char_position = random_randint(len(a_string))
            a_string = a_string[:random_char_position] + random_choice(CHARS[:-1]) + a_string[random_char_position:]
        if rand() < amount_of_noise * len(a_string):
            # Transpose 2 characters
            random_char_position = random_randint(len(a_string) - 1)
            a_string = (a_string[:random_char_position] +
                        a_string[random_char_position + 1] +
                        a_string[random_char_position] +
                        a_string[random_char_position + 2:])
        return a_string
项目:lsi-faster-rcnn    作者:cguindel    | 项目源码 | 文件源码
def _get_feature_scale(self, num_images=100):
        TARGET_NORM = 20.0 # Magic value from traditional R-CNN
        _t = Timer()
        roidb = self.imdb.roidb
        total_norm = 0.0
        count = 0.0
        inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
                          replace=False)
        for i_, i in enumerate(inds):
            im = cv2.imread(self.imdb.image_path_at(i))
            if roidb[i]['flipped']:
                im = im[:, ::-1, :]
            _t.tic()
            scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
            _t.toc()
            feat = self.net.blobs[self.layer].data
            total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
            count += feat.shape[0]
            print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
                                                           total_norm / count))

        return TARGET_NORM * 1.0 / (total_norm / count)
项目:shell-complete    作者:src-d    | 项目源码 | 文件源码
def dislpay_sample_correction(args, model, X, y, nb_corrections=10):
    """
    Select a given number of misspelled commands and print the current correction of the model.
    """
    chars = get_chars(args.vocabulary)
    seq2seq = Seq2seq(chars)
    print()
    for _ in range(nb_corrections):
        ind = choice(X.shape[0])
        rowX = X[np.array([ind])]
        rowy = y[np.array([ind])]
        preds = model.predict_classes(rowX)
        true_command = seq2seq.decode(rowy[0])
        model_correction = seq2seq.decode(preds[0], reduction=False)
        misspelled_command = seq2seq.decode(rowX[0], inverted=True)
        print('Command misspelled :', misspelled_command[::-1])
        print('True command :', true_command)
        print("Correction predicted :", model_correction)
        print('---')
    return model_correction
项目:cellranger    作者:10XGenomics    | 项目源码 | 文件源码
def split(args):
    seed(1)

    if not args.clonotype_assignments:
        return {'chunks':[{'chunk_clonotypes':[]}]}

    # Distribute clonotypes to chunks. The input file might be sorted by frequency
    # so make sure you shuffle the clonotypes in order to better distribute load.
    with open(args.clonotype_assignments) as f:
        clonotypes = json.load(f)
    num_clonotypes = len(clonotypes)

    clonotypes_per_chunk = max(MIN_CLONOTYPES_PER_CHUNK, np.ceil(float(num_clonotypes) / MAX_CHUNKS))
    num_chunks = int(np.ceil(float(num_clonotypes) / clonotypes_per_chunk))

    chunk_clonotypes = [[] for _ in range(num_chunks)]

    chunks = []

    if num_clonotypes > 0:
        # Pick a chunk 0..num_chunks num_clonotypes times.
        chunk_assignments = choice(np.arange(num_chunks), num_clonotypes, replace=True)
        for idx, clonotype_id in enumerate(clonotypes.iterkeys()):
            chunk_clonotypes[chunk_assignments[idx]].append(clonotype_id)

        chunks = [{'chunk_clonotypes':c, '__mem_gb': 12.0} for c in chunk_clonotypes if len(c) > 0]

    if len(chunks) == 0:
        chunks = [{'chunk_clonotypes':[]}]

    return {'chunks': chunks, 'join': {'__mem_gb': 16.0}}
项目:HandDetection    作者:YunqiuXu    | 项目源码 | 文件源码
def proposal_top_layer(rpn_cls_prob, rpn_bbox_pred, im_info, _feat_stride, anchors, num_anchors):
  """A layer that just selects the top region proposals
     without using non-maximal suppression,
     For details please see the technical report
  """
  rpn_top_n = cfg.TEST.RPN_TOP_N

  scores = rpn_cls_prob[:, :, :, num_anchors:]

  rpn_bbox_pred = rpn_bbox_pred.reshape((-1, 4))
  scores = scores.reshape((-1, 1))

  length = scores.shape[0]
  if length < rpn_top_n:
    # Random selection, maybe unnecessary and loses good proposals
    # But such case rarely happens
    top_inds = npr.choice(length, size=rpn_top_n, replace=True)
  else:
    top_inds = scores.argsort(0)[::-1]
    top_inds = top_inds[:rpn_top_n]
    top_inds = top_inds.reshape(rpn_top_n, )

  # Do the selection here
  anchors = anchors[top_inds, :]
  rpn_bbox_pred = rpn_bbox_pred[top_inds, :]
  scores = scores[top_inds]

  # Convert anchors into proposals via bbox transformations
  proposals = bbox_transform_inv(anchors, rpn_bbox_pred)

  # Clip predicted boxes to image
  proposals = clip_boxes(proposals, im_info[:2])

  # Output rois blob
  # Our RPN implementation only supports a single input image, so all
  # batch inds are 0
  batch_inds = np.zeros((proposals.shape[0], 1), dtype=np.float32)
  blob = np.hstack((batch_inds, proposals.astype(np.float32, copy=False)))
  return blob, scores
项目:scientific-paper-summarisation    作者:EdCo95    | 项目源码 | 文件源码
def get_feed_dicts(data_train_np, placeholders, batch_size, inst_length):
    # around 8 times faster as get_feed_dicts_old() with generator as it doesn't need to go over the whole training data every time during training
    data_train_batched = []
    realsamp = int(inst_length/batch_size)
    additionsamp = inst_length%batch_size
    if additionsamp != 0:
        realsamp += 1
    ids1 = choice(range(0, inst_length), inst_length-additionsamp, replace=False)  # sample without replacement so we get every sample once
    ids2 = choice(range(0, inst_length), additionsamp, replace=True)  # sample a few additional ones to fill up batch
    ids = np.append(ids1, ids2)

    start = 0
    for i in range(0, realsamp):
        batch_i = {}
        if i != 0:
            start = i * batch_size
        if i != realsamp:
            ids_sup = ids[start:((i+1)*batch_size)]
        else:
            ids_sup = ids[start:realsamp]
        for key, value in data_train_np.items():
            batch_i[placeholders[key]] = [data_train_np[key][ii] for ii in ids_sup]

        data_train_batched.append(batch_i)

    return data_train_batched
项目:dpl    作者:ppengtang    | 项目源码 | 文件源码
def _sample_rois(roidb, num_classes):
    """Generate a random sample of RoIs comprising foreground and background
    examples.
    """
    # label = class RoI has max overlap with
    labels = roidb['labels']
    rois = roidb['boxes']
    num_rois = len(rois)

    keep_inds = npr.choice(num_rois, size=num_rois, replace=False)
    rois = rois[keep_inds]

    return labels, rois
项目:reddit-iambic-pentameter    作者:pmichel31415    | 项目源码 | 文件源码
def sample_noun(self, vector):
        """Sample a noun at random.
        The probability of word :math:`w` is
        .. math::
            \log(p(w))\propto w^Tv`

        where :math:`p` is the poem vector and :math:`w` the word vector"""
        p = util.softmax(self.noun_vectors.dot(vector) / self.tau)
        return npr.choice(self.nouns, p=p)
项目:reddit-iambic-pentameter    作者:pmichel31415    | 项目源码 | 文件源码
def sample_adjective(self, vector):
        """Sample an adjective at random (same method as sample_noun)"""
        p = util.softmax(self.adj_vectors.dot(vector) / self.tau)
        return npr.choice(self.adjs, p=p)