Skip to content
wwcohen edited this page May 18, 2017 · 49 revisions

Background

TensorLog is different from ProPPR but uses many of the same ideas.

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-dir/textcat.ppr --proppr --db test-dir/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.

Running a learning experiment

You can run a very simple experiment from the command line, eg

 python -m expt --prog test/textcat.ppr --proppr --db test/textcattoy.cfacts --trainData test/toytrain.exam --testData test/toytest.exam

In version 1.2.3, you can add some additional arguments like this:

 python -m expt --prog test/textcat.ppr --proppr --db test/textcattoy.cfacts --trainData test/toytrain.exam --testData test/toytest.exam +++ --savedModel xxx --learner yyyy --learnedOpts '{zzz:...}'

This does a train and test cycle and saves the result to a new database, expt&#45;model.db. The new database includes the trained parameters (as well as all the fixed ones!) so you can try it out with commands like:

 python -i -m tensorlog --prog test/textcat.ppr --proppr --db expt-model.db
 ti.debug("predict/io","dh")

More generally, you'll want to configure the parameters for an experiment and run it from Python. Here's one example, from the datasets/wordnet directory. I'm using the tensorlog.parseCommandLine method, which is used to parse the command line when you invoke the interpreter, to initialize the program and such. It returns not the string options from the command line but their interpretations as data structures: eg optdict, under the key 'prog', contains a Python object which is an instance of the class tensorlog.Program.

In version 1.1, there is a parallel version of TensorLog. To use it configure one of the parallel learners in plearn.py. It uses multiprocessing (i.e. process, not thread-level) to get around Python's global interpreter lock.

Clone this wiki locally