Exemplo n.º 1
0
def setExptParams():
    print 'loading db....'
    db = comline.parseDBSpec("tmp-cache/fb15k.db|inputs/fb15k-valid.cfacts")
    print 'loading program....'
    prog = comline.parseProgSpec("inputs/fb15k.ppr", db)
    print 'loading queries....'
    queries = fbQueries(prog, db)
    modeSet = set(mode for (mode, _) in queries)
    return (db, prog, modeSet, queries)
Exemplo n.º 2
0
def setExptParams(num):
    db = comline.parseDBSpec('tmp-cache/train-%d.db|inputs/train-%d.cfacts' % (num,num))
    trainData = comline.parseDatasetSpec('tmp-cache/train-%d.dset|inputs/train-%d.exam'  % (num,num), db)
    testData = comline.parseDatasetSpec('tmp-cache/test-%d.dset|inputs/test-%d.exam'  % (num,num), db)
    prog = comline.parseProgSpec("theory.ppr",db,proppr=True)
    prog.setFeatureWeights()
    learner = plearn.ParallelFixedRateGDLearner(prog,regularizer=learn.L2Regularizer(),parallel=5,epochs=10)
    return {'prog':prog,
            'trainData':trainData,
            'testData':testData,
            'targetMode':'answer/io',
            'savedModel':'learned-model.db',
            'learner':learner
    }
Exemplo n.º 3
0
def setExptParams():
    db = comline.parseDBSpec('tmp-cache/cora.db|inputs/cora.cfacts')
    trainData = comline.parseDatasetSpec('tmp-cache/cora-train.dset|inputs/train.examples', db)
    testData = comline.parseDatasetSpec('tmp-cache/cora-test.dset|inputs/test.examples', db)
    prog = comline.parseProgSpec("cora.ppr",db,proppr=True)
    prog.setRuleWeights()
    prog.db.markAsParam('kaw',1)
    prog.db.markAsParam('ktw',1)
    prog.db.markAsParam('kvw',1)
    prog.maxDepth = 1
    learner = plearn.ParallelFixedRateGDLearner(prog,regularizer=learn.L2Regularizer(),parallel=5,epochs=30)
    return {'prog':prog,
            'trainData':trainData, 'testData':testData,
            'targetMode':'samebib/io',
            'savedModel':'tmp-cache/cora-trained.db',
            'savedTestPredictions':'tmp-cache/cora-test.solutions.txt',
            'savedTrainExamples':'tmp-cache/cora-train.examples',
            'savedTestExamples':'tmp-cache/cora-test.examples',
            'learner':learner
    }
Exemplo n.º 4
0
import logging

from tensorlog import expt
from tensorlog import learn
from tensorlog import plearn
from tensorlog import comline

if __name__=="__main__":
    logging.basicConfig(level=logging.INFO)
    logging.info('level is info')

    db = comline.parseDBSpec('tmp-cache/cora.db|inputs/cora.cfacts')
    trainData = comline.parseDatasetSpec('tmp-cache/cora-train.dset|inputs/train.examples', db)
    testData = comline.parseDatasetSpec('tmp-cache/cora-test.dset|inputs/test.examples', db)
    prog = comline.parseProgSpec("cora.ppr",db,proppr=True)
    prog.setRuleWeights()
    prog.db.markAsParam('kaw',1)
    prog.db.markAsParam('ktw',1)
    prog.db.markAsParam('kvw',1)
    prog.maxDepth = 1
#    learner = learn.FixedRateGDLearner(prog,regularizer=learn.L2Regularizer(),epochs=5)
    learner = plearn.ParallelFixedRateGDLearner(prog,regularizer=learn.L2Regularizer(),parallel=5,epochs=5)
    params = {'prog':prog,
              'trainData':trainData, 'testData':testData,
              'targetMode':'samebib/io',
              'savedModel':'tmp-cache/cora-trained.db',
              'savedTestPredictions':'tmp-cache/cora-test.solutions.txt',
              'savedTrainExamples':'tmp-cache/cora-train.examples',
              'savedTestExamples':'tmp-cache/cora-test.examples',
              'learner':learner
    }
Exemplo n.º 5
0
        print "  without mode specified: lists the relations in the database"
        print "  with mode specified: lists the facts in one relation in .cfacts format"
        print "usage: python -m list --prog progspec [--ruleIds]"
        print "  list the all rule ids"

    argspec = ["db=", "mode=", "prog=", "ruleIds"]
    try:
        optlist, args = getopt.getopt(sys.argv[1:], 'x', argspec)
    except getopt.GetoptError:
        usage()
        raise
    optdict = dict(optlist)

    db = comline.parseDBSpec(optdict['--db']) if '--db' in optdict else None
    if db and (not '--mode' in optdict):
        db.listing()
    elif db and ('--mode' in optdict):
        functor, rest = optdict['--mode'].split("/")
        arity = int(rest)
        m = db.matEncoding.get((functor, arity))
        assert m is not None, 'mode should be of the form functor/arity for something in the database'
        for goal, weight in db.matrixAsPredicateFacts(functor, arity,
                                                      m).items():
            print '\t'.join([goal.functor] + goal.args + ['%g' % (weight)])
    elif '--prog' in optdict:
        prog = comline.parseProgSpec(optdict['--prog'], db, proppr=True)
        for rid in prog.ruleIds:
            print '\t'.join(['ruleid', rid])
    else:
        usage()
Exemplo n.º 6
0
    def __init__(self,
                 target='tensorflow',
                 db=None,
                 prog=None,
                 rule_features=True,
                 autoset_db_params=True,
                 summary_file=None):
        """Create an object with a simple interface that wraps a tensorlog compiler.
    Args:

      target: a string indicating the target language, currently
      'tensorflow' or 'theano'

      db: specifies the database used by tensorflow. Either a
        tensorlog.matrixdb.MatrixDB object, or a string that can be
        converted to one by tensorlog.comline.parseDBSpec.  The common
        cases of the latter are (a) a serialized tensorlog database,
        usually with extension .db or (b) a colon-separated list of
        files containing facts and type declarations (one per line).
        Facts are tab-separated and are of the form
        "binary_relation_name TAB head TAB tail [TAB weight]" or
        "unary_relation_name TAB head".  Type declarations are of one
        of these forms:

         # :- binary_relation_name(type_name1,type_name2)
         # :- unary_relation_name(type_name)

        where type_names are identifiers, which denote disjoint sets
        of DB entities.  Fact files usually have extension .cfacts.

        A db string can also be of the form "foo.db|bar.cfacts" in
        which case the serialized database foo.db will be used if it
        exists, and otherwise bar.cfacts will be loaded, parsed, and
        serialized in foo.db for later.

      prog: specifies a tensorlog program.  Either a
        tensorlog.program.Program object or a string that can be
        converted to one by tensorlog.comline.parseProgSpec, which
        currently would be a single filename.

      rule_features: if True, then the loaded program contains control
        features {...} on every rule (i.e., it will be
        tensorlog.program.ProPPRProgram object).

      autoset_db_params: if True, try and set parameter values
        automatically. This usually works for rule_features but but
        others.

      summary_file: if not None, and if target=='tensorflow', this
        location will be used as to hold summary data for tensorboard
        on the tensorlog operations.
    """

        # parse the db argument
        if isinstance(db, matrixdb.MatrixDB):
            self.db = db
        elif isinstance(db, DBWrapper):
            self.db = db.inner_db
        elif isinstance(db, str):
            self.db = comline.parseDBSpec(db)
        else:
            assert False, 'cannot convert %r to a database' % db

        # parse the program argument
        if isinstance(prog, program.Program):
            self.prog = prog
        elif isinstance(prog, Builder):
            self.prog = program.ProPPRProgram(db=self.db, rules=prog.rules)
        elif isinstance(prog, parser.RuleCollection):
            self.prog = program.ProPPRProgram(db=self.db, rules=prog)
        elif isinstance(prog, str):
            self.prog = comline.parseProgSpec(prog,
                                              self.db,
                                              proppr=rule_features)
        else:
            assert False, 'cannot convert %r to a program' % prog

        # set weights
        if autoset_db_params:
            self.prog.setAllWeights()

        # parse the target argument
        self.target = target
        if target == 'tensorflow':
            self.xc = tensorflowxcomp.SparseMatDenseMsgCrossCompiler(
                self.prog, summaryFile=summary_file)
        elif target == 'theano':
            self.xc = theanoxcomp.SparseMatDenseMsgCrossCompiler(self.prog)
        else:
            assert False, 'illegal target %r: valid targets are "tensorflow" and "theano"' % target