Exemplo n.º 1
0
def setExptParams():
  #usage: [targetPredicate] [epochs]
  #get the command-line options for this experiment
  pred = 'hypernym' if len(sys.argv)<=1 else sys.argv[1]
  epochs = 30 if len(sys.argv)<=2 else int(sys.argv[2])
  # use comline.parseCommandLine to set up the program, etc
  optdict,args = comline.parseCommandLine([
      '--logging', 'warn',
      '--db', 'inputs/wnet.db|inputs/wnet.cfacts',
      '--prog','inputs/wnet-learned.ppr', '--proppr',
      '--train','inputs/wnet-train.dset|inputs/wnet-train.exam',
      '--test', 'inputs/wnet-test.dset|inputs/wnet-valid.exam'])

  prog = optdict['prog']
  # the weight vector is sparse - just the constants in the unary predicate rule
  prog.setRuleWeights(prog.db.vector(declare.asMode("rule(i)")))
  targetMode = 'i_%s/io' % pred if pred!='ALL' else None
  learner = plearn.ParallelFixedRateGDLearner(
      prog,epochs=epochs,parallel=40,regularizer=learn.L2Regularizer())
  return {'prog':prog,
          'trainData':optdict['trainData'],
          'testData':optdict['testData'],
          'targetMode':targetMode,
          'savedTestPredictions':'tmp-cache/%s-test.solutions.txt' % pred,
          'savedTrainExamples':'tmp-cache/wnet-train.examples',
          'savedTestExamples':'tmp-cache/wnet-test.examples',
          'learner':learner
    }, epochs
Exemplo n.º 2
0
def setExptParams():
    optdict, args = comline.parseCommandLine(
        '--prog smokers.ppr --proppr --db smokers.cfacts'.split())
    ti = interp.Interp(optdict['prog'])
    ti.prog.setRuleWeights()
    ti.prog.maxDepth = 99
    rows = []
    for line in open('query-entities.txt'):
        sym = line.strip()
        rows.append(ti.db.onehot(sym))
    X = mutil.stack(rows)
    return ti, X
Exemplo n.º 3
0
 def testTCToyIgnoringTypes(self):
   matrixdb.conf.ignore_types = True
   optdict,args = comline.parseCommandLine(
       ["--db", os.path.join(testtensorlog.TEST_DATA_DIR,"textcattoy3.cfacts"),
        "--prog", os.path.join(testtensorlog.TEST_DATA_DIR,"textcat3.ppr"),
        "--trainData", os.path.join(testtensorlog.TEST_DATA_DIR,"toytrain.exam"),
        "--testData", os.path.join(testtensorlog.TEST_DATA_DIR,"toytest.exam"),
        "--proppr"])
   for compilerClass in [tensorflowxcomp.DenseMatDenseMsgCrossCompiler,
                         tensorflowxcomp.SparseMatDenseMsgCrossCompiler]:
     xc = compilerClass(optdict['prog'])
     xc.runExpt(
         prog=optdict['prog'],
         trainData=optdict['trainData'],
         testData=optdict['testData'],
         targetMode=declare.asMode("predict/io"))
     pbDoc = xc.db.onehot('pb')
     self.checkXC(xc,'predict/io',pbDoc,collections.defaultdict(lambda:191))
     close_cross_compiler(xc)
Exemplo n.º 4
0
  def testTCToyTypes(self):
    matrixdb.conf.ignore_types = False
    optdict,args = comline.parseCommandLine(
        ["--db", os.path.join(testtensorlog.TEST_DATA_DIR,"textcattoy3.cfacts"),
         "--prog", os.path.join(testtensorlog.TEST_DATA_DIR,"textcat3.ppr"),
         "--trainData", os.path.join(testtensorlog.TEST_DATA_DIR,"toytrain.exam"),
         "--testData", os.path.join(testtensorlog.TEST_DATA_DIR,"toytest.exam"),
         "--proppr"])
    for compilerClass in [tensorflowxcomp.DenseMatDenseMsgCrossCompiler,
                          tensorflowxcomp.SparseMatDenseMsgCrossCompiler]:
      xc = compilerClass(optdict['prog'])
      xc.runExpt(
          prog=optdict['prog'],
          trainData=optdict['trainData'],
          testData=optdict['testData'],
          targetMode=declare.asMode("predict/io"))

      # check trainability
      for (functor,arity) in xc.db.matEncoding:
        v = xc.parameterFromDBToVariable(functor,arity)
        if v is not None:
          vIsTrainable = (v in tf.trainable_variables())
          vIsParameter = ((functor,arity) in xc.db.paramSet)
          self.assertEqual(vIsTrainable,vIsParameter)

      pbDoc = xc.db.onehot('pb','doc')
      self.checkXC(xc,'predict/io',pbDoc,{'negPair':115,'posPair':115,'hasWord':59,'weighted':115,'label':5})
      # some checks on the output of pprint
      lines = xc.pprint('predict/io')
      self.assertTrue(lines[0].find("SoftMaxFunction") >= 0)
      self.assertTrue(lines[1].find("SumFunction") >= 0)
      self.assertEqual(len(lines), 16)
      # some checks on misc xcomp API
      self.assertEqual(xc.inferenceOutputType('predict/io'),'label')
      pbId = xc.asSymbolId('pb',typeName='doc')
      pbSym = xc.asSymbol(pbId,typeName='doc')
      self.assertEqual(pbSym,'pb')
      self.assertEqual(xc.asSymbolId('this does not appear in the data',typeName='doc'), -1)
      close_cross_compiler(xc)
Exemplo n.º 5
0
    settings['epochs'] = 30 if len(sys.argv) <= 2 else int(sys.argv[2])
    settings['maxDepth'] = 1 if len(sys.argv) <= 3 else int(sys.argv[3])
    settings['para'] = 30 if len(sys.argv) <= 4 else int(sys.argv[4])
    settings['rate'] = 0.1 if len(sys.argv) <= 5 else float(sys.argv[5])
    settings['batch'] = 100 if len(sys.argv) <= 6 else int(sys.argv[6])

    # first run eval set on untrained model:

    optdict, args = comline.parseCommandLine([
        '--logging',
        'debug',  # was: 'warn'
        '--db',
        'inputs/{0}.db|inputs/{0}-db.cfacts'.format(settings['dataset']),
        '--prog',
        'inputs/{0}.ppr'.format(settings['dataset']),
        '--proppr',
        '--train',
        'inputs/{0}-train.dset|inputs/{0}-train.exam'.format(
            settings['dataset']),
        #        '--test', 'inputs/eval.dset|inputs/eval.exam']) # using AMIE decade evaluation data
        '--test',
        'inputs/{0}-eval.dset|inputs/{0}-eval.exam'.format(
            settings['dataset'])  # using eval data partitioned from the KB
    ])
    learner = setup(optdict, settings)

    # configute the experiment
    params = {
        'prog':
        optdict['prog'],
        'trainData':
        optdict['trainData'],
Exemplo n.º 6
0
                                     open=True)
            self.treeOutputs[key] = funOutput
            self.treeDeltas[key] = funDelta
            self.populateTree(fun.children(), child)

    def mainloop(self):
        if not self.rendered:
            self.render()
        self.root.mainloop()

if __name__ == "__main__":

    def usage():
        print 'debug.py [usual tensorlog options] mode [inputs]'

    optdict, args = comline.parseCommandLine(sys.argv[1:])
    dset = optdict.get('trainData') or optdict.get('testData')
    if dset == None and len(args) < 2:
        usage()
        print 'debug on what input? specify --trainData or give a function input'
    elif len(args) < 1:
        usage()
    elif dset and len(args) > 2:
        print 'using --trainData not the function input given'
    elif dset:
        mode = declare.asMode(args[0])
        Debugger(optdict['prog'], mode, dset, gradient=True).mainloop()
    else:
        mode = declare.asMode(args[0])
        assert db.isTypeless(), 'cannot debug a database with declared types'
        X = optdict['prog'].db.onehot(args[1])
Exemplo n.º 7
0
    usageLines = [
        'expt-specific options, given after the argument +++:',
        '    --savedModel e      # where e is a filename',
        '    --learner f         # where f is the name of a learner class',
        '    --learnerOpts g     # g is a string that "evals" to a python dict',
        '    --weightEpsilon eps # parameter weights multiplied by eps',
        '    --params p1/k1,..   # comma-sep list of functor/arity pairs'
    ]
    argSpec = [
        "learner=", "savedModel=", "learnerOpts=", "targetMode=",
        "savedTestPredictions=", "savedTestExamples=", "savedTrainExamples=",
        "params=", "weightEpsilon="
    ]
    optdict, args = comline.parseCommandLine(sys.argv[1:],
                                             extraArgConsumer="expt",
                                             extraArgSpec=argSpec,
                                             extraArgUsage=usageLines)

    weightEpsilon = float(optdict.get('--weightEpsilon', 1.0))
    print 'weightEpsilon = ', weightEpsilon
    if '--params' in optdict:
        paramSpecs = optdict['--params'].split(",")
        for spec in paramSpecs:
            functor, arity = spec.split("/")
            optdict['db'].markAsParameter(functor, int(arity))
    optdict['prog'].setFeatureWeights(epsilon=weightEpsilon)
    optdict['prog'].setRuleWeights(epsilon=weightEpsilon)
    learner = None
    if 'learner' in optdict:
        try:
            optdict['learner'] = eval(optdict['learner'])
Exemplo n.º 8
0
if __name__=="__main__":
    #usage: fb-expt.py [processes] [epochs] [targetMode] 
    
    processes = int(sys.argv[1]) if len(sys.argv)>1 else 40
    epochs = int(sys.argv[2]) if len(sys.argv)>2 else 30
    targetMode = sys.argv[3] if len(sys.argv)>3 else None
    if targetMode and (not targetMode.endswith("(i,o)")): 
        targetMode += "(i,o)"

    optdict,args = expt.Expt.timeAction(
        'parsing command line and loading data',
        lambda:comline.parseCommandLine([
            '--logging','info',
            '--db', 'inputs/fb.db|inputs/fb.cfacts',
            '--prog','inputs/learned-rules.ppr', '--proppr',
            '--train','inputs/train.dset|inputs/train.exam',
            '--test', 'inputs/valid.dset|inputs/valid.exam'
        ]))

    prog = optdict['prog']
    prog.setRuleWeights(weights=prog.db.vector(declare.asMode("ruleid(i)")))
    if processes==0:
        learner = learn.FixedRateGDLearner(
            prog,
            epochs=epochs,
            #        regularizer=learn.L2Regularizer(),
        )
    else:
        learner = plearn.ParallelFixedRateGDLearner(
            prog,
Exemplo n.º 9
0
from tensorlog import comline
from tensorlog import learn
from tensorlog import plearn

if __name__ == "__main__":
    #usage: [stem] [epochs]

    #get the command-line options for this experiment
    stem = 'citeseer' if len(sys.argv) <= 1 else sys.argv[1]
    epochs = 10 if len(sys.argv) <= 2 else int(sys.argv[2])

    # use comline.parseCommandLine to set up the program, etc
    optdict, args = comline.parseCommandLine([
        '--logging', 'info', '--db',
        'inputs/%s.db|inputs/%s-corpus.cfacts' % (stem, stem), '--prog',
        'inputs/%s-textcat.ppr' % stem, '--proppr', '--train',
        'inputs/%s-train.dset|inputs/%s-train.exam' % (stem, stem), '--test',
        'inputs/%s-test.dset|inputs/%s-test.exam' % (stem, stem)
    ])

    # prog is shortcut to the output optdict, for convenience.
    prog = optdict['prog']

    # the weight vector is sparse - just the constants in the unary predicate rule
    prog.setFeatureWeights()

    # use a non-default learner, overriding the tracing function,
    # number of epochs, and regularizer
    #    learner = learn.FixedRateGDLearner(prog,regularizer=learn.L2Regularizer(),traceFun=learn.Learner.cheapTraceFun,epochs=epochs)
    #    learner = plearn.ParallelFixedRateGDLearner(prog,epochs=epochs,parallel=40,regularizer=learn.L2Regularizer())
    learner = plearn.ParallelFixedRateGDLearner(