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 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
class TestOnlinePredictiveRefinementDimension(unittest.TestCase): 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_1(self): storage = self.grid.getStorage() gridSize = self.grid.getSize() numDim = storage.getDimension() print("######") print("Expected result:") print("######") expected = {} for j in range(gridSize): HashGridPoint = storage.getPoint(j) HashGridPoint.setLeaf(False) print("Point: ", j, " (", HashGridPoint.toString(), ")") for d in range(numDim): # # Get left and right child # leftChild = HashGridPoint(HashGridPoint) rightChild = HashGridPoint(HashGridPoint) storage.left_child(leftChild, d) storage.right_child(rightChild, d) # # Check if point is refinable # if storage.isContaining(leftChild) or storage.isContaining( rightChild): continue # # Insert children temporarily # storage.insert(leftChild) storage.insert(rightChild) val1 = self.calc_indicator_value(leftChild) val2 = self.calc_indicator_value(rightChild) storage.deleteLast() storage.deleteLast() print("Dimension: ", d) print("Left Child: ", val1) print("Right Child: ", val2) print("") expected[(j, d)] = val1 + val2 print("") for k, v in list(expected.items()): print((k, v)) print("######") print("Actual result:") print("######") actual = refinement_map({}) self.strategy.collectRefinablePoints(storage, 10, actual) for k, v in list(actual.items()): print((k, v)) # # Assertions # for k, v in list(expected.items()): self.assertEqual(actual[k], v) def calc_indicator_value(self, index): numData = self.trainData.getNrows() numCoeff = self.grid.getSize() seq = self.grid.getStorage().seq(index) num = 0 denom = 0 tmp = DataVector(numCoeff) self.multEval.multTranspose(self.errors, tmp) num = tmp.__getitem__(seq) num **= 2 alpha = DataVector(numCoeff) col = DataVector(numData) alpha.__setitem__(seq, 1.0) self.multEval.mult(alpha, col) col.sqr() denom = col.sum() if denom == 0: print("Denominator is zero") value = 0 else: value = num / denom return value
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
class TestOnlinePredictiveRefinementDimension(unittest.TestCase): 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 test_1(self): storage = self.grid.getStorage() gridSize = self.grid.getSize() numDim = storage.dim() print "######" print "Expected result:" print "######" expected = {} for j in xrange(gridSize): HashGridIndex = storage.get(j) HashGridIndex.setLeaf(False) print "Point: ", j, " (", HashGridIndex.toString(), ")" for d in xrange(numDim): # # Get left and right child # leftChild = HashGridIndex(HashGridIndex) rightChild = HashGridIndex(HashGridIndex) storage.left_child(leftChild, d) storage.right_child(rightChild, d) # # Check if point is refinable # if storage.has_key(leftChild) or storage.has_key(rightChild): continue # # Insert children temporarily # storage.insert(leftChild) storage.insert(rightChild) val1 = self.calc_indicator_value(leftChild) val2 = self.calc_indicator_value(rightChild) storage.deleteLast() storage.deleteLast() print "Dimension: ", d print "Left Child: ", val1 print "Right Child: ", val2 print "" expected[(j, d)] = val1 + val2 print "" for k, v in expected.iteritems(): print(k, v) print "######" print "Actual result:" print "######" actual = refinement_map({}) self.strategy.collectRefinablePoints(storage, 10, actual) for k, v in actual.iteritems(): print(k, v) # # Assertions # for k, v in expected.iteritems(): self.assertEqual(actual[k], v) def calc_indicator_value(self, index): numData = self.trainData.getNrows() numCoeff = self.grid.getSize() seq = self.grid.getStorage().seq(index) num = 0 denom = 0 tmp = DataVector(numCoeff) self.multEval.multTranspose(self.errors, tmp) num = tmp.__getitem__(seq) num **= 2 alpha = DataVector(numCoeff) col = DataVector(numData) alpha.__setitem__(seq, 1.0) self.multEval.mult(alpha, col) col.sqr() denom = col.sum() if denom == 0: print "Denominator is zero" value = 0 else: value = num/denom return value
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