def evalSGFunctionMulti(grid, alpha, samples, isConsistent=True): if len(samples.shape) == 1: raise AttributeError( 'the samples to be evaluated have to be a 2d numpy array') if samples.shape[1] != grid.getStorage().getDimension(): raise AttributeError( 'the dimensionality of the samples differ from the dimensionality of the grid (%i != %i)' % (samples.shape[1], grid.getStorage().getDimension())) samples_matrix = DataMatrix(samples) if isConsistent: if grid.getType() in multipleEvalNaiveGridTypes: opEval = createOperationMultipleEvalNaive(grid, samples_matrix) else: if grid.getType() == GridType_Linear: # use streaming approach for multiple eval evalConfig = OperationMultipleEvalConfiguration( OperationMultipleEvalType_STREAMING, OperationMultipleEvalSubType_DEFAULT) opEval = createOperationMultipleEval(grid, samples_matrix, evalConfig) else: # use standard approach opEval = createOperationMultipleEval(grid, samples_matrix) else: opEval = createOperationMultipleEvalNaive(grid, samples_matrix) res_vec = DataVector(samples.shape[0]) alpha_vec = DataVector(alpha) opEval.mult(alpha_vec, res_vec) return res_vec.array()
def learnData(self): self.notifyEventControllers(LearnerEvents.LEARNING_STARTED) self.specification.setBOperator( createOperationMultipleEval( self.grid, self.dataContainer.getPoints(DataContainer.TRAIN_CATEGORY))) print self.getL() while True: # repeat until policy says "stop" print "Learning %i/%i" % ( self.iteration, self.stopPolicy.getAdaptiveIterationLimit()) self.notifyEventControllers(LearnerEvents.LEARNING_STEP_STARTED) # learning step self.alpha = self.doLearningIteration(self.dataContainer) # calculate avg. error for training and test data and avg. for refine alpha self.updateResults(self.alpha, self.dataContainer) self.notifyEventControllers(LearnerEvents.LEARNING_STEP_COMPLETE) self.iteration += 1 if (self.stopPolicy.isTrainingComplete(self)): break # refine grid self.refineGrid() # from pysgpp.extensions.datadriven.uq.plot import plotNodal3d # plotNodal3d(self.grid, self.alpha) # data = self.dataContainer.getPoints('train').array() # fig = plt.figure() # plt.plot(data[:, 0], data[:, 1], ' ', marker='v') # fig.show() # plt.show() self.notifyEventControllers(LearnerEvents.LEARNING_COMPLETE) return self.alpha
def gradient_fun(self, params): ''' Compute the gradient vector in the current state ''' #import ipdb; ipdb.set_trace() # gradient_array = np.empty((self.batch_size, self.grid.getSize())) for sample_idx in xrange(self.batch_size): x = self._lastseen[sample_idx, :self.dim] y = self._lastseen[sample_idx, self.dim] params_DV = DataVector(params) gradient = DataVector(len(params_DV)) single_alpha = DataVector(1) single_alpha[0] = 1 data_matrix = DataMatrix(x.reshape(1,-1)) mult_eval = createOperationMultipleEval(self.grid, data_matrix); mult_eval.multTranspose(single_alpha, gradient); residual = gradient.dotProduct(params_DV) - y; gradient.mult(residual); #import ipdb; ipdb.set_trace() # gradient_array[sample_idx, :] = gradient.array() return gradient_array
def currentDiagHess(self, params): #return np.ones(params.shape) # if hasattr(self, 'H'): # return self.H # op_l2_dot = createOperationLTwoDotProduct(self.grid) # self.H = np.empty((self.grid.getSize(), self.grid.getSize())) # u = DataVector(self.grid.getSize()) # u.setAll(0.0) # result = DataVector(self.grid.getSize()) # for grid_idx in xrange(self.grid.getSize()): # u[grid_idx] = 1.0 # op_l2_dot.mult(u, result) # self.H[grid_idx,:] = result.array() # u[grid_idx] = 0.0 # self.H = np.diag(self.H).reshape(1,-1) # return self.H #import ipdb; ipdb.set_trace() size = self._lastseen.shape[0] data_matrix = DataMatrix(self._lastseen[:,:self.dim]) mult_eval = createOperationMultipleEval(self.grid, data_matrix); params_DV = DataVector(self.grid.getSize()) params_DV.setAll(0.) results_DV = DataVector(size) self.H = np.zeros(self.grid.getSize()) for i in xrange(self.grid.getSize()): params_DV[i] = 1.0 mult_eval.mult(params_DV, results_DV); self.H[i] = results_DV.l2Norm()**2 params_DV[i] = 0.0 self.H = self.H.reshape(1,-1)/size #import ipdb; ipdb.set_trace() return self.H
def __testSpaceS(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) #self.plotGrid(learner, suffix) storage = learner.grid.getStorage() #self.plot_grid_historgram(suffix, learner, storage, 'space') # formatter = GridImageFormatter() # formatter.serializeToFile(learner.grid, "%s%d_projections_space.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("Space %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.stopPolicy.isTrainingComplete(learner)): break #refine grid learner.notifyEventControllers(LearnerEvents.REFINING_GRID) pointsNum = learner.specification.getNumOfPointsToRefine( learner.grid.getGenerator().getNumberOfRefinablePoints()) learner.grid.getGenerator().refine( self.refinement_functor( learner.alpha, int(pointsNum), learner.specification.getAdaptThreshold())) #formatter = GridFormatter() #formatter.serializeToFile(learner.grid, "grid_anova_%s.txt"%suffix) del learner
def dehierarchizeList(grid, alpha, gps): """ evaluate sparse grid function at grid points in gps @param grid: Grid @param alpha: DataVector @param gps: list of HashGridIndex """ dim = grid.getStorage().dim() p = DataVector(dim) nodalValues = DataVector(len(gps)) A = DataMatrix(len(gps), dim) for i, gp in enumerate(gps): gp.getCoords(p) A.setRow(i, p) createOperationMultipleEval(grid, A).mult(alpha, nodalValues) return nodalValues
def get_Phi(grid, X_train, svd=False): def eval_op(x, op, size): result_vec = sg.DataVector(size) x = sg.DataVector(np.array(x).flatten()) op.mult(x, result_vec) return result_vec.array().copy() def eval_op_transpose(x, op, size): result_vec = sg.DataVector(size) x = sg.DataVector(np.array(x).flatten()) op.multTranspose(x, result_vec) return result_vec.array().copy() data_train = to_data_matrix(X_train) num_elem = X_train.shape[0] op = sg.createOperationMultipleEval(grid, data_train) matvec = lambda x: eval_op(x, op, num_elem) rmatvec = lambda x: eval_op_transpose(x, op, grid.getSize()) shape = (num_elem, grid.getSize()) linop = LinearOperator(shape, matvec, rmatvec, dtype='float64') if svd: k = min(grid.getSize(), X_train.shape[0]) _, s, _ = svds(linop, k=k - 1) return s else: Phi = linop.matmat(np.matrix(np.identity(grid.getSize()))) return Phi
def get_Phi(X_train): def eval_op(x, op, size): result_vec = sg.DataVector(size) x = sg.DataVector(np.array(x).flatten()) op.mult(x, result_vec) return result_vec.array().copy() def eval_op_transpose(x, op, size): result_vec = sg.DataVector(size) x = sg.DataVector(np.array(x).flatten()) op.multTranspose(x, result_vec) return result_vec.array().copy() num_elem = X_train.array().shape[0] grid = sg.Grid.createModLinearGrid(10) gen = grid.getGenerator() gen.regular(2) op = sg.createOperationMultipleEval(grid, X_train) matvec = lambda x: eval_op(x, op, num_elem) rmatvec = lambda x: eval_op_transpose(x, op, grid.getSize()) shape = (num_elem, grid.getSize()) linop = LinearOperator(shape, matvec, rmatvec, dtype='float64') Phi = linop.matmat(np.matrix(np.identity(grid.getSize()))) return Phi
def evalSGFunctionMulti(grid, alpha, A): if not isinstance(A, DataMatrix): raise AttributeError('A has to be a DataMatrix') size = A.getNrows() opEval = createOperationMultipleEval(grid, A) res = DataVector(size) opEval.mult(alpha, res) return res
def general_test(self, d, l, bb, xs): test_desc = "dim=%d, level=%d, len(x)=%s" % (d, l, len(xs)) print(test_desc) self.grid = Grid.createLinearGrid(d) self.grid_gen = self.grid.getGenerator() self.grid_gen.regular(l) alpha = DataVector( [self.get_random_alpha() for i in range(self.grid.getSize())]) bb_ = BoundingBox(d) for d_k in range(d): dimbb = BoundingBox1D() dimbb.leftBoundary = bb[d_k][0] dimbb.rightBoundary = bb[d_k][1] bb_.setBoundary(d_k, dimbb) # Calculate the expected value without the bounding box expected_normal = [ self.calc_exp_value_normal(x, d, bb, alpha) for x in xs ] #expected_transposed = [self.calc_exp_value_transposed(x, d, bb, alpha) for x in xs] # Now set the bounding box self.grid.getStorage().setBoundingBox(bb_) dm = DataMatrix(len(xs), d) for k, x in enumerate(xs): dv = DataVector(x) dm.setRow(k, dv) multEval = createOperationMultipleEval(self.grid, dm) actual_normal = DataVector(len(xs)) #actual_transposed = DataVector(len(xs)) multEval.mult(alpha, actual_normal) #multEval.mult(alpha, actual_transposed) actual_normal_list = [] for k in range(len(xs)): actual_normal_list.append(actual_normal.__getitem__(k)) #actual_transposed_list = [] #for k in xrange(len(xs)): # actual_transposed_list.append(actual_transposed.__getitem__(k)) self.assertAlmostEqual(actual_normal_list, expected_normal) #self.assertAlmostEqual(actual_tranposed_list, expected_tranposed) del self.grid
def __testSpaceS(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) #self.plotGrid(learner, suffix) storage = learner.grid.getStorage() #self.plot_grid_historgram(suffix, learner, storage, 'space') # formatter = GridImageFormatter() # formatter.serializeToFile(learner.grid, "%s%d_projections_space.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 "Space %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.stopPolicy.isTrainingComplete(learner)): break #refine grid learner.notifyEventControllers(LearnerEvents.REFINING_GRID) pointsNum = learner.specification.getNumOfPointsToRefine( learner.grid.createGridGenerator().getNumberOfRefinablePoints() ) learner.grid.createGridGenerator().refine( self.refinement_functor(learner.alpha, int(pointsNum), learner.specification.getAdaptThreshold()) ) #formatter = GridFormatter() #formatter.serializeToFile(learner.grid, "grid_anova_%s.txt"%suffix) del learner
def general_test(self, d, l, bb, xs): test_desc = "dim=%d, level=%d, len(x)=%s" % (d, l, len(xs)) print test_desc self.grid = Grid.createLinearGrid(d) self.grid_gen = self.grid.createGridGenerator() self.grid_gen.regular(l) alpha = DataVector([self.get_random_alpha() for i in xrange(self.grid.getSize())]) bb_ = BoundingBox(d) for d_k in xrange(d): dimbb = DimensionBoundary() dimbb.leftBoundary = bb[d_k][0] dimbb.rightBoundary = bb[d_k][1] bb_.setBoundary(d_k, dimbb) # Calculate the expected value without the bounding box expected_normal = [self.calc_exp_value_normal(x, d, bb, alpha) for x in xs] #expected_transposed = [self.calc_exp_value_transposed(x, d, bb, alpha) for x in xs] # Now set the bounding box self.grid.getStorage().setBoundingBox(bb_) dm = DataMatrix(len(xs), d) for k, x in enumerate(xs): dv = DataVector(x) dm.setRow(k, dv) multEval = createOperationMultipleEval(self.grid, dm) actual_normal = DataVector(len(xs)) #actual_transposed = DataVector(len(xs)) multEval.mult(alpha, actual_normal) #multEval.mult(alpha, actual_transposed) actual_normal_list = [] for k in xrange(len(xs)): actual_normal_list.append(actual_normal.__getitem__(k)) #actual_transposed_list = [] #for k in xrange(len(xs)): # actual_transposed_list.append(actual_transposed.__getitem__(k)) self.assertAlmostEqual(actual_normal_list, expected_normal) #self.assertAlmostEqual(actual_tranposed_list, expected_tranposed) del self.grid
def dehierarchize(grid, alpha): # dehierarchization gs = grid.getStorage() p = DataVector(gs.dim()) nodalValues = DataVector(gs.size()) A = DataMatrix(gs.size(), gs.dim()) for i in xrange(gs.size()): gs.get(i).getCoords(p) A.setRow(i, p) opEval = createOperationMultipleEval(grid, A) opEval.mult(alpha, nodalValues) return nodalValues
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 setUp(self): # # Grid # DIM = 2 LEVEL = 2 self.grid = Grid.createLinearGrid(DIM) self.grid_gen = self.grid.createGridGenerator() self.grid_gen.regular(LEVEL) # # trainData, classes, errors # xs = [] DELTA = 0.05 DELTA_RECI = int(1/DELTA) for i in xrange(DELTA_RECI): for j in xrange(DELTA_RECI): xs.append([DELTA*i, DELTA*j]) random.seed(1208813) ys = [ random.randint(-10, 10) for i in xrange(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) self.errors = DataVector(DELTA_RECI**2) coord = DataVector(DIM) for i in xrange(self.trainData.getNrows()): self.trainData.getRow(i, coord) self.errors.__setitem__ (i, abs(self.classes[i] - self.grid.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 learnDataWithFolding(self, ): self.notifyEventControllers( LearnerEvents.LEARNING_WITH_FOLDING_STARTED) self.specification.setBOperator( createOperationMultipleEval( self.grid, self.dataContainer.getPoints(DataContainer.TRAIN_CATEGORY))) # update folding self.updateFoldingPolicy() alphas = [] for dataset in self.foldingPolicy: alphas.append(self.learnDataWithTest(dataset)) self.notifyEventControllers( LearnerEvents.LEARNING_WITH_FOLDING_COMPLETE) return alphas
def dehierarchizeList(grid, alpha, gps): """ evaluate sparse grid function at grid points in gps @param grid: Grid @param alpha: DataVector @param gps: list of HashGridPoint """ dim = grid.getDimension() p = DataVector(dim) nodalValues = DataVector(len(gps)) A = DataMatrix(len(gps), dim) for i, gp in enumerate(gps): gs.getCoordinates(gp, p) A.setRow(i, p) opEval = createOperationMultipleEval(grid, A) opEval.mult(alpha, nodalValues) del opEval del A return nodalValues
def learnDataWithTest(self, dataset=None): self.notifyEventControllers( LearnerEvents.LEARNING_WITH_TESTING_STARTED) B = createOperationMultipleEval( self.grid, self.dataContainer.getPoints(DataContainer.TRAIN_CATEGORY)) self.specification.setBOperator(B) if dataset is None: dataset = self.dataContainer # learning step trainSubset = dataset.getTrainDataset() # testpoint = data.allPoint\points # testvalues = data.allValues\values testSubset = dataset.getTestDataset() while True: # repeat until policy says "stop" self.notifyEventControllers( LearnerEvents.LEARNING_WITH_TESTING_STEP_STARTED) self.alpha = self.doLearningIteration(trainSubset) # calculate avg. error for training and test data and avg. for refine alpha self.updateResults(self.alpha, trainSubset, testSubset) self.notifyEventControllers( LearnerEvents.LEARNING_WITH_TESTING_STEP_COMPLETE) self.iteration += 1 if (self.stopPolicy.isTrainingComplete(self)): break # refine grid self.refineGrid() self.notifyEventControllers( LearnerEvents.LEARNING_WITH_TESTING_COMPLETE) return self.alpha
def generateBBTMatrix(factory, training, verbose=False): from pysgpp import DataVector, DataMatrix storage = factory.getStorage() b = createOperationMultipleEval(factory, training) alpha = DataVector(storage.size()) erg = DataVector(len(alpha)) temp = DataVector(training.getNrows()) # create B matrix m = DataMatrix(storage.size(), storage.size()) for i in xrange(storage.size()): # apply unit vectors temp.setAll(0.0) erg.setAll(0.0) alpha.setAll(0.0) alpha[i] = 1.0 b.mult(alpha, temp) b.multTranspose(temp, erg) # Sets the column in m m.setColumn(i, erg) return m
def generateBBTMatrix(factory, training, verbose=False): from pysgpp import DataVector, DataMatrix storage = factory.getStorage() b = createOperationMultipleEval(factory, training) alpha = DataVector(storage.getSize()) erg = DataVector(len(alpha)) temp = DataVector(training.getNrows()) # create B matrix m = DataMatrix(storage.getSize(), storage.getSize()) for i in range(storage.getSize()): # apply unit vectors temp.setAll(0.0) erg.setAll(0.0) alpha.setAll(0.0) alpha[i] = 1.0 b.mult(alpha, temp) b.multTranspose(temp, erg) # Sets the column in m m.setColumn(i, erg) return m
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
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])
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 adaptiveGridToRegularGrid(numDims, level, refnums, f, numSamples=1000, plot=False, verbose=False): """ Converts a regular sparse grid function to a sparse grid in the combination technique and back. Arguments: numDims -- int number of dimensions level -- level of the sparse grid refnums -- int number of refinement steps f -- function to be interpolated numSamples -- int number of random samples on which we evaluate the different sparse grid functions to validate the grid conversion plot -- bool whether the sparse grid functions are plotted or not (just for numDims=1) verbose -- bool verbosity """ ## We generate a iid of uniform samples, which we are going to use to ## validate the grid conversion x = np.random.rand(numSamples, numDims) parameters = DataMatrix(x) ## We create a regular sparse grid as usual and... grid = Grid.createLinearGrid(numDims) grid.getGenerator().regular(level) alpha = interpolate(grid, f) ## ... refine it adaptively grid_adaptive = grid.clone() alpha_adaptive = DataVector(alpha) refineGrid(grid_adaptive, alpha_adaptive, f, refnums) ## We apply now both methods of the grid conversion on the ## adaptively refined grid. The first conversion considers all ## levels where at least one sparse grid point exists, while the ## second one considers just complete subspaces. treeStorage_all = convertHierarchicalSparseGridToCombigrid( grid_adaptive.getStorage(), GridConversionTypes_ALLSUBSPACES) treeStorage_complete = convertHierarchicalSparseGridToCombigrid( grid_adaptive.getStorage(), GridConversionTypes_COMPLETESUBSPACES) ## We initialize the CombigridOperation on a grid that spans the ## same function space as the original hierarchical sparse grid: ## hat basis on an equidistant grids without boundary points. func = multiFunc(f) opt_all = CombigridMultiOperation.createExpUniformLinearInterpolation( numDims, func) opt_complete = CombigridMultiOperation.createExpUniformLinearInterpolation( numDims, func) ## The CombigridOperation expects the points at which you want to ## evaluate the interpolant as DataMatrix with the shape (numDims ## x numSamples). We, therefore, need to transpose the samples and ## initialize the multi operation with them. To set the level ## structure we initialize the level manager of the operation with ## the storage we have obtained after the conversion. parameters.transpose() opt_all.setParameters(parameters) opt_all.getLevelManager().addLevelsFromStructure(treeStorage_all) opt_complete.setParameters(parameters) opt_complete.getLevelManager().addLevelsFromStructure(treeStorage_complete) parameters.transpose() ## If you want you can examine the levels of the combination ## technique... if verbose: print("-" * 80) print("just full levels:") print(opt_complete.getLevelManager().getSerializedLevelStructure()) print("-" * 80) print("all levels:") print(opt_all.getLevelManager().getSerializedLevelStructure()) print("-" * 80) ## We start to transform the grids from the combination technique ## back to their hierarchical formulation. We, again, create a ## grid with a piecewise d-linear basis and initialize the grid ## points in its storage by the ones available in the levels of ## the combination technique. We do it first for the combination ## grids that just contain just those levels where the original ## sparse grid had complete subpsaces... grid_complete = Grid.createLinearGrid(numDims) treeStorage_complete = opt_complete.getLevelManager().getLevelStructure() convertCombigridToHierarchicalSparseGrid(treeStorage_complete, grid_complete.getStorage()) ## ... and do the same for the version where we considered all ## subspaces where at least one grid point was located. grid_all = Grid.createLinearGrid(numDims) treeStorage_all = opt_all.getLevelManager().getLevelStructure() convertCombigridToHierarchicalSparseGrid(treeStorage_all, grid_all.getStorage()) ## we interpolate now f on the new grids and... alpha_complete = interpolate(grid_complete, f) alpha_all = interpolate(grid_all, f) ## ... evaluate all the surrogate functions we have so far y_sg_regular = DataVector(numSamples) createOperationMultipleEval(grid, parameters).eval(alpha, y_sg_regular) y_sg_adaptive = DataVector(numSamples) createOperationMultipleEval(grid_adaptive, parameters).eval(alpha_adaptive, y_sg_adaptive) y_sg_all = DataVector(numSamples) createOperationMultipleEval(grid_all, parameters).eval(alpha_all, y_sg_all) y_sg_complete = DataVector(numSamples) createOperationMultipleEval(grid_complete, parameters).eval(alpha_complete, y_sg_complete) y_ct_all = opt_all.getResult() y_ct_complete = opt_complete.getResult() ## For convenience we use flattened numpy arrays to test if the ## function values at each point are the same. y_sg_regular = y_sg_regular.array().flatten() y_sg_adaptive = y_sg_adaptive.array().flatten() y_ct_all = y_ct_all.array().flatten() y_ct_complete = y_ct_complete.array().flatten() y_sg_all = y_sg_all.array().flatten() y_sg_complete = y_sg_complete.array().flatten() ## If you want, you can plot the results if the problem is one dimensional if plot and numDims == 1: x = x.flatten() ixs = np.argsort(x) plt.figure() plt.plot(x[ixs], y_sg_regular[ixs], label="sg regular") plt.plot(x[ixs], y_sg_adaptive[ixs], label="sg adaptive") plt.plot(x[ixs], y_ct_complete[ixs], label="ct full") plt.plot(x[ixs], y_ct_all[ixs], label="ct all") plt.plot(x[ixs], y_sg_complete[ixs], label="sg full") plt.plot(x[ixs], y_sg_all[ixs], label="sg all") plt.legend() plt.show() ## All the function values should not be equivalent if... if grid_complete.getSize() < grid_all.getSize(): assert np.sum((y_ct_complete - y_ct_all)**2) > 1e-14 assert np.sum((y_sg_regular - y_sg_all)**2) > 1e-14 ## and should be equivalent if... if grid_complete.getSize() == grid.getSize(): assert np.sum((y_ct_complete - y_sg_regular)**2) < 1e-14 assert np.sum((y_sg_complete - y_sg_regular)**2) < 1e-14 ## For the grid sizes it must hold that assert grid_adaptive.getSize() > grid.getSize() assert grid_complete.getSize() <= grid_adaptive.getSize() assert grid_all.getSize() >= grid.getSize()
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 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])