当前位置: 首页>>代码示例>>Python>>正文


Python Dataset.set_indexing方法代码示例

本文整理汇总了Python中dataset.Dataset.set_indexing方法的典型用法代码示例。如果您正苦于以下问题:Python Dataset.set_indexing方法的具体用法?Python Dataset.set_indexing怎么用?Python Dataset.set_indexing使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dataset.Dataset的用法示例。


在下文中一共展示了Dataset.set_indexing方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Sampler

# 需要导入模块: from dataset import Dataset [as 别名]
# 或者: from dataset.Dataset import set_indexing [as 别名]
class Sampler(object):
    
    def __init__(self,x, model_file):
        """
        Sampling works as follows.
        You feed it a model and a dataset, and model_files. 
        The model_files allow you to load in models from different checkpoints.
        A feed through function is compiled that samples from the test dataset. 
        It calculates the error and the output for each element in test dataset. 
        It generates two distributions -- output for signal, and output for background.
        args:
            model: MLP object
            dataset Dataset object
            model_files: list of files corresponding to saved models  
        """ 
        self.model_file = model_file
        self.param = self.detect_params(self.model_file)
        self.dataset = Dataset(self.param['dataset'])
        self.dataset.set_indexing(self.param['indexing'])
        self.shared_train_x = self.dataset.train_x
        self.shared_train_y = self.dataset.train_y 
        self.shared_test_x = self.dataset.test_x
        self.shared_test_y = self.dataset.test_y
        try:
            self.train_labels = self.dataset.train_labels
            self.test_labels = self.dataset.test_labels
        except AttributeError:
            print("You're used a dataset without labels. You won't be able to call gen_labeled_outputs")  
        mlp = MLP(x,[self.param['h0'],self.param['h1'],2],np.random.RandomState(1234), transfer_func=T.nnet.relu)
        mlp.load_params(self.model_file,mode='hdf5')
        self.model = mlp   
        self.predicted = dict()

    def compileFunctions(self,x,y):
        """
        Compile Theano functions.
        test -- get the error of a certain segment of the dataset 
        feed_thru -- feed a dataset (or a segment thereof) through the MLP, to generate predictions. 
        """

        index = T.lscalar() 
        self.errors = self.model.errors(y) 
        print("Compiling functions...") 

        t0 = time.time() 
        self.feed_thru = theano.function(
            inputs=[x],
            outputs=self.model.MLPoutput,
        )
        self.test = theano.function(
            inputs=[x,y],
            outputs = self.errors,
        )
        print("Function compilation complete. Took {:.2f} seconds".format(time.time() -t0)) 
   
    def gen_labeled_outputs(self, save=True):
        """
        Generate a correspondance between dataset labels and MLP output. 
        kwargs:
            save: Save labels, signal or background category and MLP output value to a file 
        """
        t0 = time.time()
        cur_time = time.strftime("%d-%m")  
        try:
            feed_thru = self.feed_thru
        except AttributeError:
            print("You didn't compile relevanet theano functions yet. Doing it now...") 
            x = T.matrix('x') 
            y = T.lvector('y') 
            self.compileFunctions(x,y)
            feed_thru = self.feed_thru
        try:
            train_labels = self.train_labels.get_value() 
            test_labels = self.test_labels.get_value() 
        except AttributeError:
            print("The dataset you're using doesn't have labels.") 
            return  
        
        train_output = feed_thru(self.shared_train_x.get_value())[:,0]
        test_output = feed_thru(self.shared_test_x.get_value())[:,0]
        f = h5py.File("labeled_output_{}_{}.hdf5".format(self.dataset['criterion'],cur_time),"w")
        grpmain = f["/"] 
        grpmain.attrs['modelfile'] = self.model_file 
        grptrain = f.create_group('train') 
        grptest = f.create_group('test') 
        grptrain.create_dataset('labels',data=train_labels) 
        grptrain.create_dataset('output',data=train_output) 
        grptrain.create_dataset('category',data=self.shared_train_y.get_value()) 
         
        grptest.create_dataset('labels',data=test_labels) 
        grptest.create_dataset('output',data=test_output) 
        grptest.create_dataset('category',data=self.shared_test_y.get_value())
        f.close()  
        print("Time generating labeled output: {:.2f} seconds".format(time.time() - t0))
    
    def gen_output_distribution(self,**kwargs):
        """
        Generate output distributions for model files.
        """ 
        try:
#.........这里部分代码省略.........
开发者ID:dean-shaff,项目名称:MLP_theano,代码行数:103,代码来源:sampler.py


注:本文中的dataset.Dataset.set_indexing方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。