Ejemplo n.º 1
0
    def learn_clf(self, facts, pos, neg, bk):
        '''learns classifier'''

        self.trees = {}
        for target in self.targets:
            data = Utils.setTrainingData(target=target,
                                         facts=facts,
                                         pos=pos,
                                         neg=neg,
                                         bk=bk,
                                         regression=self.regression,
                                         sampling_rate=self.sampling_rate)
            trees = []
            for i in range(self.numberOfTrees):
                print('=' * 20, "learning tree", str(i + 1), '=' * 20)
                node.setMaxDepth(self.treeDepth)
                node.learnTree(data)
                trees.append(node.learnedDecisionTree)
                Boosting.updateGradients(data, trees)
            self.trees[target] = trees
        for target in self.targets:
            if target in self.trees:
                number_of_trees = len(self.trees[target])
                counter = 1
                for tree in self.trees[target]:
                    print('=' * 30, "tree", str(counter), '=' * 30)
                    for clause in tree:
                        print(clause)
                    counter += 1
Ejemplo n.º 2
0
 def learn(self, facts, examples, bk):
     """Learn the clause."""
     for target in self.targets:
         data = Utils.setTrainingData(target=target,
                                      facts=facts,
                                      examples=examples,
                                      bk=bk,
                                      regression=self.regression,
                                      sampling_rate=self.sampling_rate)
         trees = []
         treeLCAs = []
         for i in range(self.numberOfTrees):
             print('=' * 20, "learning tree", str(i), '=' * 20)
             node.setMaxDepth(self.treeDepth)
             node.learnTree(data)
             trees.append(node.learnedDecisionTree)
             treeLCAs.append(node.LCAS)
             Boosting.updateGradients(data, trees, loss=self.loss)
     self.trees[target] = trees
     self.treeLCAs[target] = treeLCAs
     for tree in trees:
         print('=' * 30, "tree", str(trees.index(tree)), '=' * 30)
         for clause in tree:
             print(clause)
     '''
Ejemplo n.º 3
0
    def learn(self, facts=None, examples=None, bk=None, pos=None, neg=None):
        '''learns the regression tree per target
           before learning, make sure that the training and test
           and bk files are all in the default format.
           change all formats as required as required during runtime.
	'''
        for target in self.targets:  #for every target learn
            data = Utils.setTrainingData(target=target,
                                         facts=facts,
                                         examples=examples,
                                         bk=bk,
                                         regression=self.regression,
                                         sampling_rate=self.sampling_rate,
                                         pos=pos,
                                         neg=neg)
            trees = []  #initialize place holder for trees
            for i in range(self.numberOfTrees):
                print('=' * 20, "learning tree", str(i), '=' * 20)
                node.setMaxDepth(
                    self.treeDepth)  #set max depth of individual tree learned
                node.learnTree(data)  #learn the regression tree
                trees.append(node.learnedDecisionTree)
                Boosting.updateGradients(data, trees, loss=self.loss)
        self.trees[target] = trees
        for tree in trees:  #print each tree learned
            print('=' * 30, "tree", str(trees.index(tree)), '=' * 30)
            for clause in tree:
                print(clause)
Ejemplo n.º 4
0
def main():
    '''main method'''
    targets = argv[argv.index("-target") + 1][1:-1].split(
        ',')  #read targets from input
    regression, advice = False, False
    if "-reg" in argv:
        regression = True
    if "-expAdvice" in argv:
        advice = True
    for target in targets:
        data = Utils.readTrainingData(target, regression,
                                      advice)  #read training data
        numberOfTrees = 10  #number of trees for boosting
        trees = []  #initialize place holder for trees
        for i in range(numberOfTrees):  #learn each tree and update gradient
            print('=' * 20, "learning tree", str(i), '=' * 20)
            node.setMaxDepth(2)
            node.learnTree(data)  #learn RRT
            trees.append(node.learnedDecisionTree)
            Boosting.updateGradients(data, trees)
        for tree in trees:
            print('=' * 30, "tree", str(trees.index(tree)), '=' * 30)
            for clause in tree:
                print(clause)
        testData = Utils.readTestData(target, regression)  #read testing data
        Boosting.performInference(testData,
                                  trees)  #get probability of test examples
Ejemplo n.º 5
0
Archivo: main.py Proyecto: irokin/RFGB
def main():
    '''main method'''
    parameters = Arguments().args
    #print(parameters)

    for target in parameters.target:

        # Read the training data.
        data = Utils.readTrainingData(target,
                                      path=parameters.train,
                                      regression=parameters.reg,
                                      advice=parameters.expAdvice)

        # Initialize an empty place holder for the trees.
        trees = []

        # Learn each tree and update the gradient.
        for i in range(parameters.trees):

            if parameters.verbose:
                print('=' * 20, "learning tree", str(i), '=' * 20)

            node.setMaxDepth(2)
            node.learnTree(data)  # Learn relational regression tree
            trees.append(node.learnedDecisionTree)
            Boosting.updateGradients(data, trees)

        for tree in trees:

            if parameters.verbose:
                print('=' * 30, "tree", str(trees.index(tree)), '=' * 30)

            for clause in tree:
                print(clause)

        # Read the testing data.
        testData = Utils.readTestData(target,
                                      path=parameters.test,
                                      regression=parameters.reg)
        # Get the probability of the test examples.
        Boosting.performInference(testData, trees)

        if parameters.reg:
            # View test example values (for regression)
            print(testData.examples)
        else:
            # View test query probabilities (for classification)
            print(testData.pos)
            print(testData.neg)