Skip to content

Commit 84e4a0a

Browse files
author
William Cohen
committed
better access to debugger
1 parent 619c695 commit 84e4a0a

2 files changed

Lines changed: 37 additions & 21 deletions

File tree

src/debug.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
# support for debugging/visualization
55
#
66

7+
import sys
78
import Tkinter as TK
89
import ttk
910
import tkFont
1011
import time
1112

13+
import tensorlog
1214
import dataset
1315
import matrixdb
1416
import tensorlog
@@ -46,7 +48,7 @@ def __init__(self,initProgram,targetPred,trainData,gradient=False):
4648

4749
# evaluate the gradient so that's cached
4850
if gradient:
49-
self.learner = learn.Learner(prog)
51+
self.learner = learn.OnePredFixedRateGDLearner(self.prog)
5052
self.grad = self.learner.crossEntropyGrad(self.mode,self.X,self.Y)
5153

5254
def render(self):
@@ -161,22 +163,24 @@ def mainloop(self):
161163

162164
if __name__ == "__main__":
163165

164-
#TODO more useful main?
165-
166-
TRAINED = True
167-
168-
if not TRAINED:
169-
db = matrixdb.MatrixDB.uncache('tlog-cache/textcat.db','test/textcattoy.cfacts')
166+
def usage():
167+
print 'debug.py [usual tensorlog options] mode [inputs]'
168+
169+
optdict,args = tensorlog.parseCommandLine(sys.argv[1:])
170+
dset = optdict.get('trainData') or optdict.get('testData')
171+
if dset==None and len(args)<2:
172+
usage()
173+
print 'debug on what input? specify --trainData or give a function input'
174+
elif len(args)<1:
175+
usage()
176+
elif dset and len(args)>2:
177+
print 'using --trainData not the function input given'
178+
elif dset:
179+
mode = declare.asMode(args[0])
180+
Debugger(optdict['prog'],mode,dset,gradient=True).mainloop()
170181
else:
171-
db = matrixdb.MatrixDB.deserialize('toy-trained.db')
172-
trainData = dataset.Dataset.uncacheMatrix('tlog-cache/train.dset',db,'predict/io','train')
173-
prog = tensorlog.ProPPRProgram.load(["test/textcat.ppr"],db=db)
174-
if not TRAINED:
175-
prog.setWeights(db.ones())
176-
177-
db = Debugger(prog,"predict/io",trainData,gradient=False)
178-
# default_font = tkFont.nametofont("TkDefaultFont")
179-
# default_font.configure(size=14)
180-
#ttk.Style().theme_use('clam')
181-
db.mainloop()
182+
mode = declare.asMode(args[0])
183+
X = optdict['prog'].db.onehot(args[1])
184+
dset = dataset.Dataset({mode:X},{mode:optdict['prog'].db.zeros()})
185+
Debugger(optdict['prog'],mode,dset,gradient=False).mainloop()
182186

src/tensorlog.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import mutil
1919
import debug
2020

21-
VERSION = "1.0"
21+
VERSION = "1.0.01"
2222

2323
DEFAULT_MAXDEPTH=10
2424
DEFAULT_NORMALIZE='softmax'
@@ -210,16 +210,19 @@ def loadRules(fileNames,db):
210210
class Interp(object):
211211
"""High-level interface to tensor log."""
212212

213-
def __init__(self,prog):
213+
def __init__(self,prog,trainData=None,testData=None):
214214
self.prog = prog
215215
self.db = self.prog.db
216+
self.trainData = trainData
217+
self.testData = testData
216218

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

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

279+
def debugDset(self,modeSpec,test=False):
280+
fullDataset = self.testData if test else self.trainData
281+
if fullDataset==None:
282+
print 'train/test dataset is not specified on command line?'
283+
else:
284+
mode = declare.asMode(modeSpec)
285+
dset = fullDataset.extractMode(mode)
286+
debug.Debugger(self.prog,mode,dset,gradient=True).mainloop()
287+
276288
#
277289
# utilities for reading command lines
278290
#
@@ -367,7 +379,7 @@ def parseProgSpec(spec,db,proppr=False):
367379
print "Tensorlog v%s (C) William W. Cohen and Carnegie Mellon University, 2016" % VERSION
368380

369381
optdict,args = parseCommandLine(sys.argv[1:])
370-
ti = Interp(optdict['prog'])
382+
ti = Interp(optdict['prog'],trainData=optdict.get('trainData'),testData=optdict.get('testData'))
371383

372384
try:
373385
if sys.ps1: interactiveMode = True

0 commit comments

Comments
 (0)