コード例 #1
0
    def propprInferenceCheck(self, weightVec, ruleStrings, modeString,
                             inputSymbol, expectedResultDict):
        print 'testing inference for mode', modeString, 'on input', inputSymbol, 'with proppr rules:'
        rules = parser.RuleCollection()
        for r in ruleStrings:
            rules.add(parser.Parser.parseRule(r))
        prog = tensorlog.ProPPRProgram(db=self.db,
                                       rules=rules,
                                       weights=weightVec)
        mode = tensorlog.ModeDeclaration(modeString)
        fun = prog.compile(mode)

        print 'native computation'
        y1 = self.only(prog.eval(mode, [inputSymbol]))
        self.checkDicts(self.db.rowAsSymbolDict(y1), expectedResultDict)
        print 'theano computation'
        thFun = prog.theanoPredictFunction(mode, ['x'])
        y2 = self.only(thFun(self.db.onehot(inputSymbol)))
        self.checkDicts(self.db.rowAsSymbolDict(y2), expectedResultDict)
コード例 #2
0
ファイル: tensorlog.py プロジェクト: zhangmin1953/TensorLog
 def __init__(self, db=None, rules=parser.RuleCollection()):
     self.db = db
     self.program = []
     self.function = {}
     self.rules = rules
コード例 #3
0
ファイル: tensorlog.py プロジェクト: zhangmin1953/TensorLog
 def __init__(self, db=None, rules=parser.RuleCollection(), weights=None):
     super(ProPPRProgram, self).__init__(db=db, rules=rules)
     self.weights = ops.columnVectorParam(weights, "weights")
コード例 #4
0
ファイル: tensorlog.py プロジェクト: zhangmin1953/TensorLog
            assert featureLHS.arity == 1 and featureLHS.functor == 'all', 'non-constant features must be of the form {all(X):-...}'
            outputVar = featureLHS.args[0]
            return parser.Rule(parser.Goal('feature__', [inputVar, outputVar]),
                               rule.findall)


#
# sample main: python tensorlog.py test/fam.cfacts 'rel(i,o)' 'rel(X,Y):-spouse(X,Y).' william
#
if __name__ == "__main__":
    if len(sys.argv) < 4:
        print 'usage factfile rule1 ... mode x1 ...'
    else:
        db = matrixdb.MatrixDB.loadFile(sys.argv[1])
        mode = None
        rules = parser.RuleCollection()
        xs = []
        for a in sys.argv[2:]:
            if a.find(":-") >= 0:
                rules.add(parser.Parser.parseRule(a))
            elif a.find("(") >= 0:
                assert mode == None, 'only one mode allowed'
                mode = ModeDeclaration(a)
            else:
                xs.append(a)
        p = Program(db=db, rules=rules)
        p.functionListing()
        assert mode, 'mode must be defined'
        f = p.compile(mode)
        p.functionListing()