예제 #1
0
    def test_ANOVA_Refinement_Volume(self):
        """Dimensionally adaptive refinement using volume as local error
        indicator
        
        """

        # point ((3,7), (1,1)) (middle most right) gets larger surplus coefficient
        alpha = DataVector(self.grid.getSize())
        point_to_refine = None
        for i in xrange(17):
            point = self.grid_storage.get(i)
            if point.getLevel(0) == 3 and point.getIndex(0) == 7 \
                and point.getLevel(1) == 1 and point.getIndex(1) == 1:
                point_to_refine = point
                alpha[i] = 2.0
            else:
                alpha[i] = 1.0

        # refine one point
        functor = SurplusVolumeRefinementFunctor(alpha, 1, 0.0)
        hash_refinement = HashRefinement()
        refinement_strategy = ANOVARefinement(hash_refinement)
        refinement_strategy.free_refine(self.grid_storage, functor)

        # check if only the children along x1 direction were inserted
        self.assertEqual(self.grid.getSize(), 19,
                         'Number of grid points doesn\'t match')
        child = point_to_refine.__class__(point_to_refine)
        self.grid_storage.left_child(child, 0)
        self.assertTrue(self.grid_storage.has_key(child),
                        'Left x1 left child was not found')
        child = point_to_refine.__class__(point_to_refine)
        self.grid_storage.right_child(child, 0)
        self.assertTrue(self.grid_storage.has_key(child),
                        'Left x1 right child was not found')
        child = point_to_refine.__class__(point_to_refine)
        self.grid_storage.left_child(child, 1)
        self.assertFalse(
            self.grid_storage.has_key(child),
            'Left x2 left child is present, though should not be')
        child = point_to_refine.__class__(point_to_refine)
        self.grid_storage.right_child(child, 1)
        self.assertFalse(
            self.grid_storage.has_key(child),
            'Left x2 right child is present, though should not be')
    def test_ANOVA_Refinement_Volume(self):
        """Dimensionally adaptive refinement using volume as local error
        indicator
        
        """

        # point ((3,7), (1,1)) (middle most right) gets larger surplus coefficient
        alpha = DataVector(self.grid.getSize())
        point_to_refine = None
        for i in xrange(17):
            point = self.grid_storage.get(i)
            if point.getLevel(0) == 3 and point.getIndex(0) == 7 \
                and point.getLevel(1) == 1 and point.getIndex(1) == 1:
                point_to_refine = point
                alpha[i] = 2.0
            else:
                alpha[i] = 1.0
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
        # refine one point
        functor = SurplusVolumeRefinementFunctor(alpha, 1, 0.0)
        hash_refinement = HashRefinement()
        refinement_strategy = ANOVARefinement(hash_refinement)
        refinement_strategy.free_refine(self.grid_storage, functor)
        
        # check if only the children along x1 direction were inserted
        self.assertEqual(self.grid.getSize(), 19,
                         'Number of grid points doesn\'t match')
        child = point_to_refine.__class__(point_to_refine)
        self.grid_storage.left_child(child, 0)
        self.assertTrue(self.grid_storage.has_key(child),
                        'Left x1 left child was not found')
        child = point_to_refine.__class__(point_to_refine)
        self.grid_storage.right_child(child, 0)
        self.assertTrue(self.grid_storage.has_key(child),
                        'Left x1 right child was not found')
        child = point_to_refine.__class__(point_to_refine)
        self.grid_storage.left_child(child, 1)
        self.assertFalse(self.grid_storage.has_key(child),
                         'Left x2 left child is present, though should not be')
        child = point_to_refine.__class__(point_to_refine)
        self.grid_storage.right_child(child, 1)
        self.assertFalse(self.grid_storage.has_key(child),
                         'Left x2 right child is present, though should not be')
예제 #3
0
    def __testANOVAS(self, suffix):
        #        from bin.controller.InfoToScreen import InfoToScreen
        builder = LearnerBuilder()
        builder = builder.buildRegressor()
        learner = builder.withTrainingDataFromCSVFile('refinement_strategy_%s.csv.gz'%suffix)\
        .withGrid().withLevel(3)\
        .withBorder(Types.BorderTypes.NONE)\
        .withSpecification().withIdentityOperator().withAdaptThreshold(0.001)\
        .withAdaptRate(1.0)\
        .withLambda(0.0001)\
        .withCGSolver().withImax(500)\
        .withStopPolicy().withAdaptiveItarationLimit(5)\
        .andGetResult()

        learner.specification.setBOperator(
            createOperationMultipleEval(
                learner.grid,
                learner.dataContainer.getPoints(DataContainer.TRAIN_CATEGORY)))

        while True:  #repeat until policy says "stop"
            learner.notifyEventControllers(LearnerEvents.LEARNING_STEP_STARTED)

            #learning step
            learner.alpha = learner.doLearningIteration(learner.dataContainer)
            learner.knowledge.update(learner.alpha)

            #compress grid
            if learner.iteration == 0:
                generator = learner.grid.getGenerator()
                functor = self.coarsening_functor(
                    learner.alpha, generator.getNumberOfRemovablePoints(),
                    learner.specification.getAdaptThreshold())
                generator.coarsen(functor, learner.alpha)

            self.plotGrid(learner, suffix)

            storage = learner.grid.getStorage()

            self.plot_grid_historgram(suffix, learner, storage)

            formatter = GridImageFormatter()
            formatter.serializeToFile(
                learner.grid,
                "%s%d_projections_anova.png" % (suffix, learner.iteration))

            #calculate avg. error for training and test data and avg. for refine alpha
            learner.updateResults(learner.alpha, learner.dataContainer)
            learner.notifyEventControllers(
                LearnerEvents.LEARNING_STEP_COMPLETE)
            p_val = learner.trainAccuracy[-1] + learner.specification.getL(
            ) * np.sum(learner.alpha.array()**2)
            print("ANOVA %s iteration %d: %d grid points, %1.9f MSE, p* = %1.10f" \
                    % (suffix, learner.iteration, storage.getSize(), learner.trainAccuracy[-1], p_val))
            learner.iteration += 1
            if learner.iteration == 5:
                pass
            if (learner.stopPolicy.isTrainingComplete(learner)): break

            #refine grid
            learner.notifyEventControllers(LearnerEvents.REFINING_GRID)
            learner.grid.getStorage().recalcLeafProperty()
            refinable_poits = learner.grid.getGenerator(
            ).getNumberOfRefinablePoints()
            pointsNum = learner.specification.getNumOfPointsToRefine(
                refinable_poits)
            #            learner.grid.getGenerator().refine( SurplusRefinementFunctor(learner.errors, int(pointsNum), learner.specification.getAdaptThreshold()) )

            refiner = HashRefinement()
            functor = self.refinement_functor(
                learner.alpha, int(pointsNum),
                learner.specification.getAdaptThreshold())
            anova_refinement = ANOVARefinement(refiner)
            anova_refinement.free_refine(learner.grid.getStorage(), functor)
        #formatter = GridFormatter()
        #formatter.serializeToFile(learner.grid, "grid_anova_%s.txt"%suffix)

        del learner
    def __testANOVAS(self, suffix):
#        from bin.controller.InfoToScreen import InfoToScreen
        builder = LearnerBuilder()
        builder = builder.buildRegressor()
        learner = builder.withTrainingDataFromCSVFile('refinement_strategy_%s.csv.gz'%suffix)\
        .withGrid().withLevel(3)\
        .withBorder(Types.BorderTypes.NONE)\
        .withSpecification().withIdentityOperator().withAdaptThreshold(0.001)\
        .withAdaptRate(1.0)\
        .withLambda(0.0001)\
        .withCGSolver().withImax(500)\
        .withStopPolicy().withAdaptiveItarationLimit(5)\
        .andGetResult()
        
        
        learner.specification.setBOperator(createOperationMultipleEval(learner.grid,
                    learner.dataContainer.getPoints(DataContainer.TRAIN_CATEGORY)))
        
        while True: #repeat until policy says "stop"
            learner.notifyEventControllers(LearnerEvents.LEARNING_STEP_STARTED)

            #learning step
            learner.alpha = learner.doLearningIteration(learner.dataContainer)
            learner.knowledge.update(learner.alpha)

            #compress grid
            if learner.iteration == 0:
                generator = learner.grid.createGridGenerator()
                functor = self.coarsening_functor(
                          learner.alpha,
                          generator.getNumberOfRemovablePoints(),
                          0.99, learner.grid.getStorage())
#                functor = self.coarsening_functor(
#                          learner.alpha,
#                          generator.getNumberOfRemovablePoints(),
#                          learner.specification.getAdaptThreshold())
                generator.coarsen(functor, learner.alpha)
            #print "coersening finished"
            self.plotGrid(learner, suffix)
            
            storage = learner.grid.getStorage()

            self.plot_grid_historgram(suffix, learner, storage)
            
            
            
            formatter = GridImageFormatter()
            formatter.serializeToFile(learner.grid, "%s%d_projections_anova.png"%(suffix, learner.iteration))
            
            
            #calculate avg. error for training and test data and avg. for refine alpha
            learner.updateResults(learner.alpha, learner.dataContainer)
            learner.notifyEventControllers(LearnerEvents.LEARNING_STEP_COMPLETE)
            p_val = learner.trainAccuracy[-1] + learner.specification.getL()*np.sum(learner.alpha.array()**2)
            print "ANOVA %s iteration %d: %d grid points, %1.9f MSE, p* = %1.10f" % \
            (suffix, learner.iteration, storage.size(), learner.trainAccuracy[-1], p_val)           
            learner.iteration += 1
            if learner.iteration == 5: 
                pass
            if(learner.stopPolicy.isTrainingComplete(learner)): break
            
            #refine grid
            learner.notifyEventControllers(LearnerEvents.REFINING_GRID)
            learner.grid.getStorage().recalcLeafProperty()
            refinable_poits = learner.grid.createGridGenerator().getNumberOfRefinablePoints()
            pointsNum = learner.specification.getNumOfPointsToRefine(refinable_poits)
#            learner.grid.createGridGenerator().refine( SurplusRefinementFunctor(learner.errors, int(pointsNum), learner.specification.getAdaptThreshold()) )

            
            refiner = HashRefinement()
            functor = self.refinement_functor(learner.alpha, int(pointsNum), learner.specification.getAdaptThreshold())
            anova_refinement = ANOVARefinement(refiner)
            anova_refinement.free_refine(learner.grid.getStorage(), functor)
        #formatter = GridFormatter()
        #formatter.serializeToFile(learner.grid, "grid_anova_%s.txt"%suffix)

        del learner