Classification cache (nclasses>1)
353234
private RuntimeOptions getRuntimeOptions() {
    RuntimeOptions opts = new RuntimeOptions();
    opts.setSeed((int) get_params().getOrMakeRealSeed());
    opts.setUseGPU(get_params()._gpu);
    opts.setDeviceID(get_params()._device_id);
    return opts;
  }
834808
def _getSuitableClasses(self, data, imageDosHeader):
        """"""
        classes = None
        machine = IMAGE_FILE_MACHINE[c_ushort.from_buffer(data,imageDosHeader.header.e_lfanew+4).value]

        if machine == IMAGE_FILE_MACHINE.I386:
            classes = PE32
        elif machine == IMAGE_FILE_MACHINE.AMD64:
            classes = PE64

        return classes
1012942
def _cache_dataset(dataset, prefix):
    
    if not os.path.exists(_constants.CACHE_PATH):
        os.makedirs(_constants.CACHE_PATH)
    src_data = np.concatenate([e[0] for e in dataset])
    tgt_data = np.concatenate([e[1] for e in dataset])
    src_cumlen = np.cumsum([0]+[len(e[0]) for e in dataset])
    tgt_cumlen = np.cumsum([0]+[len(e[1]) for e in dataset])
    np.savez(os.path.join(_constants.CACHE_PATH, prefix + '.npz'),
             src_data=src_data, tgt_data=tgt_data,
             src_cumlen=src_cumlen, tgt_cumlen=tgt_cumlen)
949415
def computePreRec(cm, class_names):
    '''
    
    '''
    n_classes = cm.shape[0]
    if len(class_names) != n_classes:
        print("Error in computePreRec! Confusion matrix and class_names "
              "list must be of the same size!")
        return
    precision = []
    recall = []
    f1 = []    
    for i, c in enumerate(class_names):
        precision.append(cm[i,i] / numpy.sum(cm[:,i]))
        recall.append(cm[i,i] / numpy.sum(cm[i,:]))
        f1.append( 2 * precision[-1] * recall[-1] / (precision[-1] + recall[-1]))
    return recall, precision, f1
788759
def subclasses(cls):
        """"""
        seen = set()
        queue = set([cls])

        while queue:
            c = queue.pop()
            seen.add(c)

            sc = c.__subclasses__()
            for c in sc:
                if c not in seen:
                    queue.add(c)

        seen.remove(cls)
        return seen
1011986
def _from_dict(cls, _dict):
        """"""
        args = {}
        if 'classifier_id' in _dict:
            args['classifier_id'] = _dict.get('classifier_id')
        if 'url' in _dict:
            args['url'] = _dict.get('url')
        if 'text' in _dict:
            args['text'] = _dict.get('text')
        if 'top_class' in _dict:
            args['top_class'] = _dict.get('top_class')
        if 'classes' in _dict:
            args['classes'] = [
                ClassifiedClass._from_dict(x) for x in (_dict.get('classes'))
            ]
        return cls(**args)
709215
def _getSuitableClasses(self, data):
        """"""
        classes = None
        if data[EI.CLASS] == ELFCLASS.BITS_32:
            if data[EI.DATA] == ELFDATA.LSB:
                classes = LSB_32
            elif data[EI.DATA] == ELFDATA.MSB:
                classes = MSB_32

        elif data[EI.CLASS] == ELFCLASS.BITS_64:
            if data[EI.DATA] == ELFDATA.LSB:
                classes = LSB_64
            elif data[EI.DATA] == ELFDATA.MSB:
                classes = MSB_64

        return classes
781830
def optimize(self, data_loader):
        
        torch.set_grad_enabled(True)
        self.model.train()
        torch.cuda.empty_cache()
        self.preallocate(data_loader, training=True)
        output = self.feed_data(data_loader, training=True)
        self.model.zero_grad()
        torch.cuda.empty_cache()
        return output
845001
def cache_info(self):
        """"""
        return {
            'single_node_repertoire':
                self._single_node_repertoire_cache.info(),
            'repertoire': self._repertoire_cache.info(),
            'mice': self._mice_cache.info()
        }
348562
def _learner_distributed(learn:Learner, cuda_id:int, cache_dir:PathOrStr='tmp'):
    ""
    learn.callbacks.append(DistributedTrainer(learn, cuda_id))
    learn.callbacks.append(DistributedRecorder(learn, cuda_id, cache_dir))
    return learn
900832
protected void trainAndClassify(Instance inst){
    	
        nbInstances++;
    	
        boolean mature = true;
        boolean unmature = true;
        
    	for (int i = 0; i < getNbActiveClassifiers(); i++) {
        	
    		// check if all adaptive learners are mature
    		if (this.ensembleAges[i] < this.maturityOption.getValue() && i<getNbAdaptiveClassifiers())
            	mature = false;
        	
    		// check if all adaptive learners are not mature 
        	if (this.ensembleAges[i] >= this.maturityOption.getValue() && i<getNbAdaptiveClassifiers())
            	unmature = false;
        	
        	if (this.nbInstances >= this.ensembleWeights[i].index + 1){
        
        		// train adaptive learners
        		if (i < getNbAdaptiveClassifiers())
        			this.ensemble[i].trainOnInstance(inst);
        
        		int val = this.ensemble[i].correctlyClassifies(inst)?1:0;
            	double sum = updateEvaluationWindow(i, val);  
            	this.ensembleWeights[i].val = sum;
        		this.ensembleAges[i] = this.ensembleAges[i]+1;
        		
        	}
        	
        }
    	
    	// if all adaptive learners are not mature --> set weights to one 
    	if (unmature)
        	for (int i = 0; i < getNbAdaptiveClassifiers(); i++)
        		this.ensembleWeights[i].val=1;
        		
    	// if all adaptive learners are mature --> delete one learner
        if (mature){
        	Pair[] learners = getHalf(false);
        	
        	if (learners.length > 0){
        		double rand = classifierRandom.nextInt(learners.length);
        		discardModel(learners[(int)rand].index);		
        	}
        }
	
    }
702254
protected void updateClasses(HashMap<String,HashSet<Class>> cache, String superclass, HashSet<Class> names) {
    if (!cache.containsKey(superclass))
      cache.put(superclass, names);
    else
      cache.get(superclass).addAll(names);
  }
1019715
def _loadcache(cachefile):
    
    cache = {}
    if os.path.exists(cachefile):
        with open(cachefile) as f:
            for line in f:
                line = line.split()
                if len(line) == 2:
                    try:
                        cache[int(line[0])] = float(line[1])
                    except:
                        pass
    return cache
490832
def _get_classifier(self, prefix):
        
        with self.name_scope():
            classifier = nn.Dense(2, prefix=prefix)
        return classifier
532453
public MergingIterator<ZooPC> loadAllInstances(Class<?> cls, 
			boolean subClasses, boolean loadFromCache) {
		checkActiveRead();
		MergingIterator<ZooPC> iter = 
				new MergingIterator<ZooPC>(this, config.getFailOnClosedQueries());
        ZooClassDef def = cache.getSchema(cls, primary);
		loadAllInstances(def.getVersionProxy(), subClasses, iter, loadFromCache);
		if (loadFromCache) {
			//also add 'new' instances
			iter.add(cache.iterator(def, subClasses, ObjectState.PERSISTENT_NEW));
		}
		return iter;
	}
844132
def cafferesnet101(num_classes=1000, pretrained='imagenet'):
    
    model = ResNet(Bottleneck, [3, 4, 23, 3], num_classes=num_classes)
    if pretrained is not None:
        settings = pretrained_settings['cafferesnet101'][pretrained]
        assert num_classes == settings['num_classes'], \
            "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
        model.load_state_dict(model_zoo.load_url(settings['url']))
        model.input_space = settings['input_space']
        model.input_size = settings['input_size']
        model.input_range = settings['input_range']
        model.mean = settings['mean']
        model.std = settings['std']
    return model
789879
def memoize(f):
    """"""
    def _c(*args, **kwargs):
        if not hasattr(f, 'cache'):
            f.cache = dict()
        key = (args, tuple(kwargs))
        if key not in f.cache:
            f.cache[key] = f(*args, **kwargs)
        return f.cache[key]
    return wraps(f)(_c)