def test_1D_Refinement(self): """ Refine a point in a 2D grid in one direction""" # get point ((2,1), (2,3)) (top right) that will be refined point_to_refine_idx = None point = None for i in xrange(17): point = self.grid_storage.get(i) if point.getLevel(0) == 2 and point.getIndex(0) == 1 \ and point.getLevel(1) == 2 and point.getIndex(1) == 3: point_to_refine_idx = i point_to_refine = point break self.assertNotEqual(point_to_refine_idx, None, 'Point ((2,1), (2,3)) was not found') # refine the point in x1-direction dim = 0 hash_refinement = HashRefinement() hash_refinement.refineGridpoint1D(self.grid_storage, point_to_refine_idx, dim) # check number of grid points self.assertEqual(self.grid.getSize(), 19, 'Number of grid points doesn\'t match') # check if new points are in the grid child = point_to_refine.__class__(point_to_refine) self.grid_storage.left_child(child, dim) 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, dim) 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') # refine the point in x2-direction dim = 1 hash_refinement.refineGridpoint1D(self.grid_storage, point_to_refine_idx, dim) # check number of grid points self.assertEqual(self.grid.getSize(), 21, 'Number of grid points doesn\'t match') # check if new points are in the grid child = point_to_refine.__class__(point_to_refine) self.grid_storage.left_child(child, dim) self.assertTrue(self.grid_storage.has_key(child), 'Left x2 left child was not found') child = point_to_refine.__class__(point_to_refine) self.grid_storage.right_child(child, dim) self.assertTrue(self.grid_storage.has_key(child), 'Left x2 right child was not found')
def testFreeRefine(self): """Tests surplus based refine for Hash-Storage""" from pysgpp import GridStorage, HashGenerator from pysgpp import SurplusRefinementFunctor, HashRefinement, DataVector s = GridStorage(2) g = HashGenerator() g.regular(s, 1) d = DataVector(1) d[0] = 1.0 f = SurplusRefinementFunctor(d) r = HashRefinement() r.free_refine(s, f) self.failUnlessEqual(s.size(), 5)
def setUp(self): # # Grid # DIM = 2 LEVEL = 2 self.grid = Grid.createLinearGrid(DIM) self.grid_gen = self.grid.getGenerator() self.grid_gen.regular(LEVEL) # # trainData, classes, errors # xs = [] DELTA = 0.05 DELTA_RECI = int(1 / DELTA) for i in range(DELTA_RECI): for j in range(DELTA_RECI): xs.append([DELTA * i, DELTA * j]) random.seed(1208813) ys = [random.randint(-10, 10) for i in range(DELTA_RECI**2)] self.trainData = DataMatrix(xs) self.classes = DataVector(ys) self.alpha = DataVector([3, 6, 7, 9, -1]) self.multEval = createOperationMultipleEval(self.grid, self.trainData) opEval = createOperationEval(self.grid) self.errors = DataVector(DELTA_RECI**2) coord = DataVector(DIM) for i in range(self.trainData.getNrows()): self.trainData.getRow(i, coord) self.errors.__setitem__( i, abs(self.classes[i] - opEval.eval(self.alpha, coord))) # # OnlinePredictiveRefinementDimension # hash_refinement = HashRefinement() self.strategy = OnlinePredictiveRefinementDimension(hash_refinement) self.strategy.setTrainDataset(self.trainData) self.strategy.setClasses(self.classes) self.strategy.setErrors(self.errors)
def test_1D_Refinement(self): """ Refine a point in a 2D grid in one direction""" # get point ((2,1), (2,3)) (top right) that will be refined point_to_refine_idx = None point = None for i in xrange(17): point = self.grid_storage.get(i) if point.getLevel(0) == 2 and point.getIndex(0) == 1 \ and point.getLevel(1) == 2 and point.getIndex(1) == 3: point_to_refine_idx = i point_to_refine = point break self.assertNotEqual(point_to_refine_idx, None, 'Point ((2,1), (2,3)) was not found') # refine the point in x1-direction dim = 0 hash_refinement = HashRefinement() hash_refinement.refineGridpoint1D(self.grid_storage, point_to_refine_idx, dim) # check number of grid points self.assertEqual(self.grid.getSize(), 19, 'Number of grid points doesn\'t match') # check if new points are in the grid child = point_to_refine.__class__(point_to_refine) self.grid_storage.left_child(child, dim) 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, dim) 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') # refine the point in x2-direction dim = 1 hash_refinement.refineGridpoint1D(self.grid_storage, point_to_refine_idx, dim) # check number of grid points self.assertEqual(self.grid.getSize(), 21, 'Number of grid points doesn\'t match') # check if new points are in the grid child = point_to_refine.__class__(point_to_refine) self.grid_storage.left_child(child, dim) self.assertTrue(self.grid_storage.has_key(child), 'Left x2 left child was not found') child = point_to_refine.__class__(point_to_refine) self.grid_storage.right_child(child, dim) self.assertTrue(self.grid_storage.has_key(child), 'Left x2 right child was not found')
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 range(17): point = self.grid_storage.getPoint(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 = ANOVAHashRefinement() 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: %d != %d' % (self.grid.getSize(), 19)) child = point_to_refine.__class__(point_to_refine) child.getLeftChild(0) self.assertTrue(self.grid_storage.isContaining(child), 'Left x1 left child was not found') child = point_to_refine.__class__(point_to_refine) child.getRightChild(0) self.assertTrue(self.grid_storage.isContaining(child), 'Left x1 right child was not found') child = point_to_refine.__class__(point_to_refine) child.getLeftChild(1) self.assertFalse( self.grid_storage.isContaining(child), 'Left x2 left child is present, though should not be') child = point_to_refine.__class__(point_to_refine) child.getRightChild(1) self.assertFalse( self.grid_storage.isContaining(child), 'Left x2 right child is present, though should not be')
def test_freeRefineSubspaceIsotropic(self): """Refine the isotropic middle subspace""" alpha = DataVector(self.grid.getSize()) alpha.setAll(1.0) for i in [13, 14, 15, 16]: alpha[i] = 2. #refinement stuff refinement = HashRefinement() decorator = SubspaceRefinement(refinement) # refine a single grid point each time functor = SurplusRefinementFunctor(alpha, 1) decorator.free_refine(self.HashGridStorage, functor) for i in range(self.grid.getSize()): HashGridPoint = self.HashGridStorage.getPoint(i) self.assertEqual(self.grid.getSize(), 33) for i in range(self.grid.getSize()): HashGridPoint = self.HashGridStorage.getPoint(i) levelIndex = eval(HashGridPoint.toString()) self.assertFalse(levelIndex[0] == 4 or levelIndex[2] == 4)
def test_freeRefineSubspaceIsotropic(self): """Refine the isotropic middle subspace""" alpha = DataVector(self.grid.getSize()) alpha.setAll(1.0) alpha[12] = 2. #refinement stuff refinement = HashRefinement() decorator = GSGRefinement(refinement) # refine a single grid point each time functor = SurplusRefinementFunctor(alpha, 1) decorator.freeRefineSubspace(self.HashGridStorage, functor) for i in xrange(self.grid.getSize()): HashGridIndex = self.HashGridStorage.get(i) print i, HashGridIndex.toString() self.assertEqual(self.grid.getSize(), 15) for i in xrange(self.grid.getSize()): HashGridIndex = self.HashGridStorage.get(i) levelIndex = eval(HashGridIndex.toString()) self.assertFalse(levelIndex[0] == 4 or levelIndex[2] >= 3)
def test_freeRefineSubspaceAnisotropic(self): """Refine Anisotropic subspace (x2)""" alpha = DataVector(self.grid.getSize()) alpha.setAll(1.0) for i in [9, 10, 11, 12]: alpha[i] = 2. #refinement stuff refinement = HashRefinement() decorator = SubspaceRefinement(refinement) # refine a single grid point each time functor = SurplusRefinementFunctor(alpha, 1) decorator.free_refine(self.HashGridStorage, functor) for i in xrange(self.grid.getSize()): HashGridIndex = self.HashGridStorage.get(i) print i, HashGridIndex.toString() self.assertEqual(self.grid.getSize(), 33) for i in xrange(self.grid.getSize()): HashGridIndex = self.HashGridStorage.get(i) levelIndex = eval(HashGridIndex.toString()) self.assertFalse(levelIndex[0] == 4)
def test_manual(self): print "#" * 20 result = {(1, 0): 5, (2, 0): 25} # # Grid # DIM = 1 LEVEL = 2 self.grid = Grid.createLinearGrid(DIM) self.grid_gen = self.grid.createGridGenerator() self.grid_gen.regular(LEVEL) # # trainData, classes, errors # xs = [[0.1], [0.4], [0.6], [0.8]] errs = [1, 2, 3, 4] self.trainData = DataMatrix(xs) self.errors = DataVector(errs) self.multEval = createOperationMultipleEval(self.grid, self.trainData) self.dim = DIM self.storage = self.grid.getStorage() self.gridSize = self.grid.getSize() # # OnlinePredictiveRefinementDimension # print "OnlineRefinementDim" hash_refinement = HashRefinement() online = OnlinePredictiveRefinementDimension(hash_refinement) online.setTrainDataset(self.trainData) online.setErrors(self.errors) online_result = refinement_map({}) online.collectRefinablePoints(self.grid.getStorage(), 10, online_result) for k, v in online_result.iteritems(): print k, v for k, v in online_result.iteritems(): self.assertAlmostEqual(online_result[k], result[k]) # # Naive # print print "Naive" naive_result = self.naive_calc() for k, v in naive_result.iteritems(): print k, v for k, v in naive_result.iteritems(): self.assertAlmostEqual(naive_result[k], result[k])
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
print "(1, 0): 4.04" print "#" * 10 d = 2 l = 2 xs = [[0.1, 0.9], [0.9, 0.2], [0.3, 0.5], [0.3, 0.0], [0.9, 0.0]] errs = [-2, -0.1, -0.2, -0.2, -1.8] grid = Grid.createLinearGrid(d) grid_gen = grid.createGridGenerator() grid_gen.regular(l) trainData = DataMatrix(xs) errors = DataVector(errs) multEval = createOperationMultipleEval(grid, trainData) dim = d storage = grid.getStorage() gridSize = grid.getSize() hash_refinement = HashRefinement() online = OnlinePredictiveRefinementDimension(hash_refinement) online.setTrainDataset(trainData) online.setErrors(errors) online_result = refinement_map({}) online.collectRefinablePoints(storage, 10, online_result) for k, v in online_result.iteritems(): print k, v
def general_test(self, d, l, num): # print "#"*20 # print xs = [self.get_random_x(d) for i in xrange(num)] dupl = True while dupl: dupl_tmp = False for x in xs: for y in xs: if x == y: dupl = True break if dupl: break dupl = dupl_tmp xs = [self.get_random_x(d) for i in xrange(num)] errs = [self.get_random_err() for i in xrange(num)] self.grid = Grid.createLinearGrid(d) self.grid_gen = self.grid.createGridGenerator() self.grid_gen.regular(l) self.trainData = DataMatrix(xs) self.errors = DataVector(errs) self.multEval = createOperationMultipleEval(self.grid, self.trainData) self.dim = d self.storage = self.grid.getStorage() self.gridSize = self.grid.getSize() # # OnlinePredictiveRefinementDimension # # print "OnlineRefinementDim" hash_refinement = HashRefinement(); online = OnlinePredictiveRefinementDimension(hash_refinement) online.setTrainDataset(self.trainData) online.setErrors(self.errors) online_result = refinement_map({}) online.collectRefinablePoints(self.storage, 5, online_result) # for k,v in online_result.iteritems(): # print k, v # # Naive # # print # print "Naive" naive_result = self.naive_calc() # for k,v in naive_result.iteritems(): # print k, v # # OnlinePredictiveRefinementDimensionOld # hash_refinement = HashRefinement(); online_old = OnlinePredictiveRefinementDimensionOld(hash_refinement) # # Assertions # for k,v in online_result.iteritems(): if abs(online_result[k] - naive_result[k]) >= 0.1: #print "Error in:", k #print online_result[k] #print naive_result[k] #print naive_result #print "Datapoints" #print xs #print "Errors" #print errs #print "All values:" #print "Key: Online result, naive result" #for k,v in online_result.iteritems(): # print("{} ({}): {}, {}".format(k, self.storage.get(k[0]).toString(), v, naive_result[k])) self.assertTrue(False) # self.assertAlmostEqual(online_result[k], naive_result[k]) del self.grid del self.grid_gen del self.trainData del self.errors del self.multEval del self.storage