Skip to content

Interpreter

Tom Smoker edited this page Aug 31, 2018 · 2 revisions

Using TensorLog interactively

For the below tutorial, make sure you're on the `dev` branch. It has all the latest toys.

To issue queries interactive to a TensorLog program, put the tensorlog source directory on your PYTHONPATH and then use the command:

 python -i -m tensorlog.interp --prog foo.ppr [--proppr] --db bar.cfacts

For example in the Tensorlog/tensorlog (source) directory you can say:

 python -i -m tensorlog.interp --prog test-data/textcat.ppr --proppr --db test-data/textcattoy.cfacts

This is just a Python shell, but a variable ti has been bound to a tensorlog.Interp instance, and ti.prog and ti.db are also bound. You can get help with the method <coded>ti.help()&lt;/code&gt;</coded>, and you can inspect the source code for a predicate, or the function that it's compiled to, eg:

 >>> ti.list("predict/2")
 predict(X,Pos) :- assign(Pos,pos), hasWord(X,W), posPair(W,F), weighted(F).
 predict(X,Neg) :- assign(Neg,neg), hasWord(X,W), negPair(W,F), weighted(F).
 >>> ti.list("predict/io")
 SoftmaxFunction
 | SumFunction
 | | w_Pos = OpSeqFunction(['X']) # predict(X,Pos) :- assign(Pos,pos), hasWord(X,W), posPair(W,F), weighted(F).
 | | | f_1_Pos = U_[pos] # assign(Pos,pos) -> Pos
 | | | f_2_W = X * M_[hasWord(i,o)] # hasWord(X,W) -> W
 | | | f_3_F = f_2_W * M_[posPair(i,o)] # posPair(W,F) -> F
 | | | b_4_F = V_[weighted(i)] # weighted(F) -> F
 | | | fb_F = f_3_F o b_4_F # F -> PSEUDO
 | | | w_Pos = f_1_Pos * fb_F.sum() # fb_F -> PSEUDO
 | | w_Neg = OpSeqFunction(['X']) # predict(X,Neg) :- assign(Neg,neg), hasWord(X,W), negPair(W,F), weighted(F).
 | | | f_1_Neg = U_[neg] # assign(Neg,neg) -> Neg
 | | | f_2_W = X * M_[hasWord(i,o)] # hasWord(X,W) -> W
 | | | f_3_F = f_2_W * M_[negPair(i,o)] # negPair(W,F) -> F
 | | | b_4_F = V_[weighted(i)] # weighted(F) -> F
 | | | fb_F = f_3_F o b_4_F # F -> PSEUDO
 | | | w_Neg = f_1_Neg * fb_F.sum() # fb_F -> PSEUDO

You can't evaluate the program yet - in this case we have an untrained program with undefined rule weights, which you need to initialize, which you can usually get by doing using the following command, which will provide reasonable initial weights:

 ti.prog.setAllWeights()

You can then run a function with the eval command:

 >>> ti.eval("predict/io","dh")
 {'neg': 0.49999979211790663, 'pos': 0.49999979211790663, '__NULL__': 4.1576418669185313e-07}

You can also use the debug method which pops up a TkInter window that lets you inspect the compiled function and the messages passed around for this input, and, if they existed, the deltas (errors that are backpropagated in training). If you specify trainData or testData options you can also debug them with the debugDset method.

The command-line parser is fairly smart: for instance, a db option of foo.db&#124;foo.cfacts will tell the interpreter to build the serialized database foo.db from foo.cfacts if it needs to, and use the cached version otherwise. You can do the same for datasets, and several .cfacts or .ppr files can be specified by using a colon-separated list. python &#45;m tensorlog &#45;&#45;help will summarize these options.

Clone this wiki locally