Skip to content

Commit

Permalink
better access to debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
William Cohen committed Jul 1, 2016
1 parent 619c695 commit 84e4a0a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
40 changes: 22 additions & 18 deletions src/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
# support for debugging/visualization
#

import sys
import Tkinter as TK
import ttk
import tkFont
import time

import tensorlog
import dataset
import matrixdb
import tensorlog
Expand Down Expand Up @@ -46,7 +48,7 @@ def __init__(self,initProgram,targetPred,trainData,gradient=False):

# evaluate the gradient so that's cached
if gradient:
self.learner = learn.Learner(prog)
self.learner = learn.OnePredFixedRateGDLearner(self.prog)
self.grad = self.learner.crossEntropyGrad(self.mode,self.X,self.Y)

def render(self):
Expand Down Expand Up @@ -161,22 +163,24 @@ def mainloop(self):

if __name__ == "__main__":

#TODO more useful main?

TRAINED = True

if not TRAINED:
db = matrixdb.MatrixDB.uncache('tlog-cache/textcat.db','test/textcattoy.cfacts')
def usage():
print 'debug.py [usual tensorlog options] mode [inputs]'

optdict,args = tensorlog.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:
db = matrixdb.MatrixDB.deserialize('toy-trained.db')
trainData = dataset.Dataset.uncacheMatrix('tlog-cache/train.dset',db,'predict/io','train')
prog = tensorlog.ProPPRProgram.load(["test/textcat.ppr"],db=db)
if not TRAINED:
prog.setWeights(db.ones())

db = Debugger(prog,"predict/io",trainData,gradient=False)
# default_font = tkFont.nametofont("TkDefaultFont")
# default_font.configure(size=14)
#ttk.Style().theme_use('clam')
db.mainloop()
mode = declare.asMode(args[0])
X = optdict['prog'].db.onehot(args[1])
dset = dataset.Dataset({mode:X},{mode:optdict['prog'].db.zeros()})
Debugger(optdict['prog'],mode,dset,gradient=False).mainloop()

18 changes: 15 additions & 3 deletions src/tensorlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import mutil
import debug

VERSION = "1.0"
VERSION = "1.0.01"

DEFAULT_MAXDEPTH=10
DEFAULT_NORMALIZE='softmax'
Expand Down Expand Up @@ -210,16 +210,19 @@ def loadRules(fileNames,db):
class Interp(object):
"""High-level interface to tensor log."""

def __init__(self,prog):
def __init__(self,prog,trainData=None,testData=None):
self.prog = prog
self.db = self.prog.db
self.trainData = trainData
self.testData = testData

def help(self):
print "ti.list(foo): foo can be a compiled function, eg \"foo/io\", a predicate definition, eg"
print " \"foo/2\", or a database predicate, also specified as \"foo/2\"."
print "ti.list(): list everything."
print "ti.eval(\"functor/mode\",\"c\"): evaluate a function on a database constant c"
print "ti.debug(\"functor/mode\",\"c\"): debug the corresponding eval command"
print "ti.debugDset(\"functor/mode\"[,test=True]): run debugger on a dataset"

def list(self,str=None):
if str==None:
Expand Down Expand Up @@ -273,6 +276,15 @@ def debug(self,modeSpec,sym):
dset = dataset.Dataset({mode:X},{mode:self.db.zeros()})
debug.Debugger(self.prog,mode,dset,gradient=False).mainloop()

def debugDset(self,modeSpec,test=False):
fullDataset = self.testData if test else self.trainData
if fullDataset==None:
print 'train/test dataset is not specified on command line?'
else:
mode = declare.asMode(modeSpec)
dset = fullDataset.extractMode(mode)
debug.Debugger(self.prog,mode,dset,gradient=True).mainloop()

#
# utilities for reading command lines
#
Expand Down Expand Up @@ -367,7 +379,7 @@ def parseProgSpec(spec,db,proppr=False):
print "Tensorlog v%s (C) William W. Cohen and Carnegie Mellon University, 2016" % VERSION

optdict,args = parseCommandLine(sys.argv[1:])
ti = Interp(optdict['prog'])
ti = Interp(optdict['prog'],trainData=optdict.get('trainData'),testData=optdict.get('testData'))

try:
if sys.ps1: interactiveMode = True
Expand Down

0 comments on commit 84e4a0a

Please sign in to comment.