ASSIGN = 'assign'
class VarInfo(object):
class GoalInfo(object):
class BPCompiler(object):
  def __init__(self,v):
  def __repr__(self):
  def __init__(self,j):
  def __str__(self):
    def _ss(s): return "<" + ",".join(map(str,s)) + ">"
  def __repr__(self):
  def __init__(self,lhsMode,tensorlogProg,depth,rule):
  def getFunction(self):
  def showVars(self):
      def _gs(j):
  def showRule(self):
    def goalStr(j): return str(self.goalDict[j])
  def showOps(self):
  def inferredTypes(self):
  def compile(self):
  def reorderRHS(self,rule,lhsMode):
    def readyToExecute(goal):
    def varsIn(goal):
  def validateRuleBeforeAnalysis(self):
  def inferFlow(self):
  def inferTypes(self):
    def informativeType(t): return (t is not None) and (t != matrixdb.THING)
  def compileDefinedPredicates(self):
  def toMode(self,j):
    def argIOMode(x):
  def collectInputTypes(self,j):
  def generateOps(self):
    def addOp(op,traceDepth,msgFrom,msgTo):
      def jToGoal(msg): return str(self.goals[msg]) if type(msg)==type(0) else msg
    def makeMessageName(stem,v,j=None,j2=None):
    def msgGoal2Var(j,v,traceDepth):
        def hasOutputVarUsedElsewhere(gin):
    def msgVar2Goal(v,j,traceDepth):
def _only(c): def parseCommandLine(argv,extraArgConsumer=None,extraArgSpec=[],extraArgUsage=[]):
    def usage():
    def status(msg): logging.info('%s time %.3f sec mem %.3f Gb' % (msg,time.time()-startTime,util.memusage()))
def isUncachefromSrc(s): return s.find("|")>=0
def getCacheSrcPair(s): return s.split("|")
def makeCacheSrcPair(s1,s2): return "%s|%s" % (s1,s2)
def parseDatasetSpec(spec,db):
def parseDBSpec(spec):
def parseProgSpec(spec,db,proppr=False): class Dataset(object):
    def __init__(self,xDict,yDict):
    def isSinglePredicate(self):
    def extractMode(self,mode):
    def modesToLearn(self):
    def hasMode(self,mode):
    def getX(self,mode):
    def getY(self,mode):
    def size(self):
    def shuffle(self):
    def minibatchIterator(self,batchSize=100,shuffleFirst=True):
    def pprint(self):
    def serialize(self,dir):
    def deserialize(dir):
    def uncacheExamples(dsetFile,db,exampleFile,proppr=True):
    def uncacheMatrix(dsetFile,db,functorToLearn,functorInDB):
    def loadMatrix(db,functorToLearn,functorInDB):
    def _parseLine(line,proppr=True):
    def loadProPPRExamples(db,fileName):
    def loadExamples(db,fileName,proppr=False):
        def getId(typeName,symbol):
    def saveProPPRExamples(self,fileName,db,append=False,mode=None): def asMode(spec):
    def __init__(self,goal):
    def args(self):
    def arg(self,i):
    def getArity(self):
    def getFunctor(self):
    def __str__(self):
    def __repr__(self):
    def __hash__(self):
    def __eq__(self,other):
    def __init__(self,goal,strict=True):
    def isInput(self,i):
    def isOutput(self,i):
    def isConst(self,i):
    def __str__(self):
    def __init__(self,goal):
    def getType(self,i):
    def typeSet(self):
class AbstractDeclaration(object):
class ModeDeclaration(AbstractDeclaration):
class TypeDeclaration(AbstractDeclaration): NULL_ENTITY_NAME = dbschema.NULL_ENTITY_NAME
THING = dbschema.THING
OOV_ENTITY_NAME = dbschema.OOV_ENTITY_NAME
TRAINABLE_DECLARATION_FUNCTOR = 'trainable'
class MatrixDB(object):
  def __init__(self,initSchema=None):
  def checkTyping(self):
  def isTypeless(self):
  def _fillDefault(self,typeName):
  def dim(self,typeName=None):
  def onehot(self,s,typeName=None,outOfVocabularySymbolsAllowed=False):
  def zeros(self,numRows=1,typeName=None):
  def ones(self,typeName=None):
  def nullMatrix(self,numRows=1,typeName=None,numCols=0):
  def transposeNeeded(mode,transpose=False):
  def matrix(self,mode,transpose=False):
  def vector(self,mode):
  def matrixPreimage(self,mode):
  def matrixPreimageMat(self,mode):
  def matrixPreimageOnesType(self,mode):
  def matrixPreimageOnes(self,mode):
  def isParameter(self,mode):
  def markAsParam(self,functor,arity):
  def markAsParameter(self,functor,arity):
  def clearParameterMarkings(self):
  def getParameter(self,functor,arity):
  def parameterIsInitialized(self,functor,arity):
  def setParameter(self,functor,arity,replacement):
  def asSymbol(self,symbolId,typeName=None):
  def asSymbolId(self,symbol,typeName=None):
  def rowAsSymbolDict(self,row,typeName=None):
  def arrayAsSymbolDict(self,arr,typeName=None):
  def matrixAsSymbolDict(self,m,typeName=None):
  def matrixAsPredicateFacts(self,functor,arity,m):
  def inDB(self,functor,arity):
  def summary(self,functor,arity):
  def listing(self):
  def numMatrices(self):
  def size(self):
  def parameterSize(self):
  def createPartner(self):
  def serialize(self,direc):
  def serializeDataTo(self,fileLike,filter=None):
  def importSerializedDataFrom(self,fileLike):
  def deserializeDataFrom(fileLike):
  def _saveMatDictWithScipy(fileLike,d):
  def _restoreMatDictWithScipy(fileLike):
  def deserialize(direc):
  def uncache(dbFile,factFile,initSchema=None):
  def addLines(self,lines):
  def loadFile(filenames,initSchema=None):
  def startBuffers(self):
  def bufferFile(self,filename):
  def flushBuffers(self):
  def _flushBuffer(self,functor,arity):
  def _bufferTriplet(self,functor,arity,a1,a2,w,filename,k):
  def _bufferLine(self,line,filename,k):
    def _atof(s): class GradAccumulator(object):
class Tracer(object):
class EpochTracer(Tracer):
class Learner(object):
class OnePredFixedRateGDLearner(Learner):
class FixedRateGDLearner(Learner):
class FixedRateSGDLearner(FixedRateGDLearner):
class Regularizer(object):
class NullRegularizer(object):
class L2Regularizer(Regularizer):
    def __init__(self):
    def keys(self):
    def items(self):
    def __getitem__(self,paramName):
    def __setitem__(self,paramName,gradient):
    def accum(self,paramName,deltaGradient):
    def fitParameterShapes(self):
    def mapData(self,mapFun):
    def addedTo(self,other):
    def counter():
    def mergeCounters(counters):
    def accumToCounter(counter,otherCounter):
    def _mergeCountersWithInit(counters,init):
    def silent(learner,gradAccum,Y,P,**kw):
    def cheap(learner,gradAccum,Y,P,**kw):
    def default(learner,gradAccum,Y,P,**kw):
    def recordDefaults(learner,gradAccum,Y,P,**kw):
    def defaultPlusAcc(learner,gradAccum,Y,P,**kw):
    def _announce(gradAccum,keyValuePairList):
    def _record(gradAccum,keyValuePairList):
    def loss(learner,Y,P,kw):
    def accuracy(learner,Y,P,kw):
    def timing(learner,kw):
    def identification(learner,kw):
    def silent(learner,ctr,**kw):
    def cheap(learner,ctr,**kw):
    def default(learner,ctr,**kw):
    def __init__(self,prog,regularizer,tracer,epochTracer):
    def predict(self,mode,X,pad=None):
    def datasetPredict(self,dset,copyXs=True):
    def datasetAccuracy(goldDset,predictedDset):
    def datasetCrossEntropy(goldDset,predictedDset,perExample=True):
    def accuracy(Y,P):
        def allZerosButArgmax(d):
    def crossEntropy(Y,P,perExample=False):
    def crossEntropyGrad(self,mode,X,Y,tracerArgs={},pad=None):
    def meanUpdate(self,functor,arity,delta,n,totalN=0):
    def applyUpdate(self,paramGrads,rate):
    def __init__(self,prog,epochs=10,rate=0.1,regularizer=None,tracer=None,epochTracer=None):
    def train(self,mode,X,Y):
    def __init__(self,prog,epochs=10,rate=0.1,regularizer=None,tracer=None,epochTracer=None):
    def train(self,dset):
    def __init__(self,prog,epochs=10,rate=0.1,regularizer=None,tracer=None,miniBatchSize=100):
    def train(self,dset):
    def regularizeParams(self,prog,n):
    def regularizationCost(self,prog):
    def regularizeParams(self,prog,n):
    def regularizationCost(self,prog):
    def __init__(self,regularizationConstant=0.01):
    def regularizeParams(self,prog,n):
    def regularizationCost(self,prog):
            X = dset.getX(mode)
            Y = goldDset.getY(mode)
            P = predictedDset.getY(mode)
            Y = goldDset.getY(mode)
            P = predictedDset.getY(mode)
        P = self.predict(mode,X,pad) NONETYPE=type(None) 
        D = SS.csr_matrix((mat.data,mat.indices-loIndex,mat.indptr),shape=newShape,dtype='float32').todense()
    D1 = SS.csr_matrix((m1.data,m1.indices-loIndex,m1.indptr),shape=newShape1,dtype='float32').todense()
    D2 = SS.csr_matrix((m2.data,m2.indices-loIndex,m2.indptr),shape=newShape2,dtype='float32').todense()
def summary(mat):
def pprintSummary(mat):
def checkCSR(mat,context='unknown'):
def checkNoNANs(mat,context='unknown'):
def maxValue(mat):
def densify(mat,maxExpandFactor=-1,maxExpandIntercept=-1):
def denseSize(m,loIndex,hiIndex):
def sparseSize(m,loIndex,hiIndex):
def codensify(m1,m2,maxExpandFactor=-1,maxExpandIntercept=-1):
def undensify(denseMat, info):
def mean(mat):
def rowsum(mat):
def mapData(dataFun,mat):
def stack(mats):
def numRows(m):
def numCols(m):
def nzCols(m,i):
def repeat(row,n):
def alterMatrixRows(mat,alterationFun):
def softmax(db,mat):
        def softMaxAlteration(data,lo,hi,unused):
def denseSoftmax(m):
def broadcastAndComponentwiseMultiply(m1,m2):
def multiplyByBroadcastRowVec(m,v):
def broadcastAndWeightByRowSum(m1,m2):
def shuffleRows(m,shuffledRowNums=None):
def selectRows(m,lo,hi): def isProcessedConstant(a):
def isProcessedVariable(a):
def isVariableAtom(a):
    def __init__(self,functor,args):
    def _setArgs(self,args):
    def __str__(self):
    def __repr__(self):
    def __init__(self,lhs,rhs,features=None,findall=None):
    def variabilize(self):
            def convertArgs(args):
            def convertGoal(g):
    def __str__(self):
    def asString(self,syntax=None):
    def __init__(self,syntax=None):
    def _key(self,g):
    def add(self,r):
    def size(self):
    def rulesFor(self,g):
    def mapRules(self,mapfun):
    def listing(self):
    def __iter__(self):
    def equals(self,other):
  def __init__(self,syntax=None):
  def setSyntax(self,syntax):
  def _convertGoal(self,ptree):
  def _convertRule(self,ptree):
  def parseGoal(self,s):
  def parseGoalList(self,s):
  def parseRule(self,s):
  def parseQuery(self,s):
  def parseFile(self,filename,rules=None):
  def parseStream(self,fileLike,rules=None):
class Goal(object):
class Rule(object):
class RuleCollection(object):
class Parser(object): class Program(object):
class ProPPRProgram(Program):
class Plugins(object):
    def __init__(self, db=None, rules=parser.RuleCollection(), plugins=None, calledFromProPPRProgram=False):
        def checkRule(r):
    def clearFunctionCache(self):
    def findPredDef(self,mode):
    def compile(self,mode,depth=0):
    def getPredictFunction(self,mode):
    def getParamList(self):
    def getFunction(self,mode):
    def evalSymbols(self,mode,symbols,typeName=None):
    def eval(self,mode,inputs):
    def evalGradSymbols(self,mode,symbols):
    def evalGrad(self,mode,inputs):
    def setAllWeights(self):
    def setFeatureWeights(self,epsilon=1.0):
    def setRuleWeights(self,weights=None,epsilon=1.0):
    def _loadRules(fileNames):
    def loadRules(fileNames,db,plugins=None):
    def serialize(self,direc):
    def deserialize(direc):
    def serializeRulesTo(self,fileLike):
    def deserializeRulesFrom(fileLike):
    def __init__(self, db=None, rules=parser.RuleCollection(), weights=None, plugins=None):
    def setRuleWeights(self,weights=None,epsilon=1.0,ruleIdPred=None):
    def getRuleWeights(self):
    def setFeatureWeights(self,epsilon=1.0):
        def possibleModes(rule):
    def _setFeatureWeightsForTypelessDB(self,epsilon=1.0):
            def typeOfWeights(mode):
    def setFeatureWeight(self,paramName,arity,weight):
    def _moveFeaturesToRHS(self,rule0):
    def loadRules(fileNames,db,plugins=None):
  def __init__(self):
  def isempty(self):
  def define(self,mode,outputFun,outputTypeFun=None):
  def isDefined(self,mode=None,functor=None,arity=None):
  def definition(self,mode):
  def outputType(self,mode,inputTypes): class Compiler(object):
class Builder(object):
class RuleCollectionWrapper(parser.RuleCollection):
class DBWrapper(object):
class SchemaWrapper(dbschema.TypedSchema):
class RuleWrapper(parser.Rule):
class Options(object):
class Experiment(Options):
  def __init__(self,target='tensorflow',db=None,prog=None,rule_features=True,autoset_db_params=True,summary_file=None):
  def get_cross_compiler(self):
  def get_program(self):
  def get_database(self):
  def proof_count(self,mode,inputs=None):
  def inference(self,mode,inputs=None):
  def loss(self,mode,inputs=None):
  def mark_db_predicate_trainable(self,mode):
  def trainable_db_variables(self,mode,for_optimization=False):
  def input_placeholder_name(self,mode):
  def input_placeholder(self,mode):
  def target_output_placeholder_name(self,mode):
  def target_output_placeholder(self,mode):
  def db_param_list(self):
  def db_param_is_set(self,param_id):
  def get_db_param_value(self,param_id):
  def set_db_param_value(self,param_id,value):
  def set_all_db_params_to_learned_values(self,session):
  def serialize_db(self,dir_name):
  def serialize_program(self,dir_name):
  def _as_dataset(self,spec):
  def load_dataset(self,dataset_spec):
  def load_small_dataset(self,dataset_spec):
  def annotate_small_dataset(self,dset):
    def wrapped_xy_pair(mode): return (self.xc.wrapInput(dset.getX(mode)), self.xc.wrapInput(dset.getY(mode)))
  def modes_to_learn(self,dataset_obj):
  def minibatches(self,dataset_obj,batch_size=100,shuffle_first=True):
  def load_big_dataset(self,dataset_spec,verbose=True):
  def annotate_big_dataset(self,dset):
      def rows_per_gigabyte(c): return (1024.0*1024.0*1024.0) / (c*4.0)
  def __init__(self):
  def __setattr__(self,name,value):
  def _split(comma_or_space_sep_names):
  def variable(variable_name):
  def variables(space_sep_variable_names):
  def types(space_sep_type_names):
  def rule_id(type_name,rule_id):
  def rule_ids(type_name,space_sep_rule_ids):
    def goal_builder(rule_id):
  def predicate(predicate_name):
  def predicates(space_sep_predicate_names):
    def goal_builder(pred_name):
      def builder(*args):
  def __iadd__(self,other):
  def __init__(self):
  def __iadd__(self,other):
  def __init__(self,builder):
  def _set_to_value(self,other):
  def __iadd__(self,other):
  def __init__(self,builder):
  def __iadd__(self,other):
  def _combine(x,y):
  def __and__(self,other):
  def __or__(self,other):
  def __floordiv__(self,other):
  def __le__(self,other):
  def __repr__(self):
  def __init__(self):
  def set_from_command_line(self,argv):
  def as_dictionary(self):
  def option_usage(self):
  def __init__(self):
  def run(self):
    UX,UY = test[self.mode] TEST_DATA_DIR = os.path.join(os.path.dirname(__file__),"test-data/")
    X,Y = matrixAsTrainingData(trainingData,*trainSpec)
    P0 = learner.predict(mode,X)
    X = mutil.stack([prog.db.onehot(s) for s in input_symbols])
    X,Y = matrixAsTrainingData(self.labeledData,'train',2)
      X = dset.getX(mode)
      Y = dset.getY(mode)
    P0 = learner.datasetPredict(dset)
    P1 = learner.datasetPredict(dset)
    P2 = learner.datasetPredict(Udset)
    X,Y = matrixAsTrainingData(self.labeledData,'train',2)
    P0 = learner.predict(mode,X)
    P1 = learner.predict(mode,X)
    TX,TY = matrixAsTrainingData(self.labeledData,'test',2)
    P2 = learner.predict(mode,TX)
      Y = dset.getY(mode)
      X1 = dset1.getX(mode)
      Y1 = dset1.getY(mode)
      X2 = dset2.getX(mode)
      Y2 = dset2.getY(mode)
def softmax_normalize(expected_result_dict):
def rules_from_strings(rule_strings):
  def __init__(self,db):
  def add_data_symbols(self,sx,symbol_list):
  def get_data(self):
  def get_x(self):
  def get_y(self):
def matrixAsTrainingData(db,functor,arity):
  def test_hash(self):
  def setUp(self):
  def test_list(self):
  def setUp(self):
  def test_if(self):
  def test_failure(self):
  def test_reverse_if(self):
  def test_or(self):
  def test_chain(self):
  def test_mid(self):
  def test_nest(self):
  def test_back1(self):
  def test_back2(self):
  def test_reorder(self):
  def test_rec1(self):
  def test_const_output(self):
  def test_const_chain1(self):
  def test_const_chain2(self):
  def test_alt_chain(self):
  def test_proppr1(self):
  def test_proppr2(self):
  def test_reuse1(self):
  def inference_check(self,rule_strings,mode_string,inputSymbol,expected_result_dict):
  def proppr_inference_check(self,weightVec,rule_strings,mode_string,inputSymbol,expected_result_dict):
  def only(self,group):
  def check_dicts(self,actual, expected):
  def setUp(self):
  def testThing(self):
  def compareCheck(self,rule_strings,mode_string,input_symbols,expected_result_dicts):
  def inference_check(self,rule_strings,mode_string,input_symbols,expected_result_dicts):
  def predictCheck(self,rule_strings,mode_string,input_symbols,expected_result_dicts):
  def check_dicts(self,actual, expected):
  def setUp(self):
  def testRec0(self):
  def testRec1(self):
  def testRecBound(self):
  def mat_inference_check(self,rule_strings,mode_string,input_symbols,expected_result_dict,max_depth=None):
  def check_dict_of_dicts(self,actual,expected):
  def setUp(self):
  def test_if(self):
  def test_if2(self):
  def test_reverse_if(self):
  def test_chain1(self):
  def test_chain2(self):
  def test_call1(self):
  def test_call2(self):
  def test_split(self):
  def test_or(self):
  def test_weighted_vec(self):
  def grad_check(self,rule_strings,mode_string,params,xyPairs,expected):
  def check_directions(self,actual_grad,expected_direction):
  def grad_updates(self,rule_strings,mode,params,xyPairs):
  def setUp(self):
    def moveToPartner(db,partner,functor,arity):
  def testDBKeys(self):
  def testNativeRow(self):
  def testNativeMatrix(self):
  def testGradMatrix(self):
    def checkGrad(i,x,psign,nsign):
  def testLabeledData(self):
  def testMultiLearn1(self):
  def testLearn(self):
  def checkClass(self,d,sym,lab,expected):
  def loadExamples(self,filename,db):
  def check_dicts(self,actual, expected, delta=0.05):
  def setUp(self):
  def cacheFile(self,fileName):
  def testFullSerialization(self):
  def testRulesSerialization(self):
  def setUp(self):
  def cacheFile(self,fileName):
  def testMToyExpt(self):
  def testMToyExptMatParam(self):
  def testMToyParallel(self):
  def testTCToyExpt(self):
  def testTCToyExpt2(self):
  def runTCToyExpt(self):
  def runTCToyExpt2(self):
  def testTCToyIgnoringTypes(self):
  def testTCToyTypes(self):
  def runMToyExpt1(self):
  def runMToyExpt2(self):
  def runMToyExpt3(self):
  def runMToyParallel(self):
  def runMToyMatParam(self):
  def setUp(self):
  def testProPPRLoadExamples(self):
  def testLoadExamples(self):
  def testNormalize(self):
    def totalRowWeightOfY():
  def checkMatchExamples(self,filename,proppr):
  def check_dicts(self,actual,expected):
  def setUp(self):
  def testRepeat(self):
  def setUp(self):
  def testSerialization(self):
  def testPartialSerialization(self):
  def testUntypedSerialization(self):
  def testStabs(self):
  def testDeclarations(self):
  def testTCToyExptTypes(self):
  def setUp(self):
  def testTypeInference(self):
  def testIt(self):
  def testIt(self):
    def equalGoal(g1,g2):
    def equalRule(r1,r2):
  def testIt(self):
class DataBuffer(object):
class TestModeDeclaration(unittest.TestCase):
class TestInterp(unittest.TestCase):
class TestSmallProofs(unittest.TestCase):
class TestMultiRowOps(unittest.TestCase):
class TestMatrixRecursion(unittest.TestCase):
class TestGrad(unittest.TestCase):
class TestProPPR(unittest.TestCase):
class TestProgramSerialization(unittest.TestCase):
class TestExpt(unittest.TestCase):
class TestDataset(unittest.TestCase):
class TestMatrixUtils(unittest.TestCase):
class TestTypes(unittest.TestCase):
class TestTypeSemantics(unittest.TestCase):
class TestTrainableDeclarations(unittest.TestCase):
class TestParser(unittest.TestCase):
class TestExampleLoading(unittest.TestCase): class Function(object):
class OpSeqFunction(Function):
class NullFunction(Function):
class LogFunction(Function):
class SumFunction(Function):
class SoftmaxFunction(Function):
    def __init__(self):
    def eval(self,db,values,pad):
    def backprop(self,delta,gradAccum,pad):
    def _checkDuplications(self):
    def install(self,nextId=1):
    def copy(self):
    def pprint(self,depth=0):
    def pprintSummary(self):
    def pprintComment(self):
    def children(self):
    def __init__(self,opInputs,opOutput,ops,rule=None,inputTypes=None,outputType=None):
    def __repr__(self):
    def pprintSummary(self):
    def pprintComment(self):
    def _doEval(self,db,values,pad):
    def _doBackprop(self,delta,gradAccum,pad):
    def children(self):
    def copy(self):
    def __init__(self,lhsMode):
    def __repr__(self):
    def pprintSummary(self):
    def _doEval(self,db,values,pad):
    def _doBackprop(self,delta,gradAccum,pad):
    def children(self):
    def copy(self):
    def __init__(self,fun):
    def __repr__(self):
    def pprintSummary(self):
    def _doEval(self,db,values,pad):
    def _doBackprop(self,delta,gradAccum,pad):
    def children(self):
    def copy(self):
    def __init__(self,funs):
    def __repr__(self):
    def pprintSummary(self):
    def _doEval(self,db,values,pad):
    def _doBackprop(self,delta,gradAccum,pad):
    def children(self):
    def copy(self):
    def __init__(self,fun):
    def __repr__(self):
    def pprintSummary(self):
    def _doEval(self,db,values,pad):
    def _doBackprop(self,delta,pad):
    def children(self):
    def copy(self): class Op(opfunutil.OperatorOrFunction):
class DefinedPredOp(Op):
class AssignPreimageToVar(Op):
class AssignVectorToVar(Op):
class AssignOnehotToVar(Op):
class VecMatMulOp(Op):
class CallPlugin(Op):
class ComponentwiseVecMulOp(Op):
class WeightedVec(Op):
  def __init__(self,dst):
  def setMessage(self,msgFrom,msgTo):
  def eval(self,env,pad):
  def backprop(self,env,gradAccum,pad):
  def pprint(self,depth=0):
  def pprintSummary(self):
  def pprintComment(self):
  def _ppLHS(self):
  def children(self):
  def install(self,nextId):
  def copy(self):
  def __init__(self,tensorlogProg,dst,src,mode,depth):
  def __repr__(self):
  def _ppLHS(self):
  def _doEval(self,env,pad):
  def _doBackprop(self,env,gradAccum,pad):
  def pprint(self,depth=-1):
  def install(self,nextId):
  def copy(self):
  def children(self):
  def __init__(self,dst,matMode,dstType=None):
  def __repr__(self):
  def _ppLHS(self):
  def _doEval(self,env,pad):
  def _doBackprop(self,env,gradAccum,pad):
  def copy(self):
  def __init__(self,dst,matMode,dstType=None):
  def __repr__(self):
  def _ppLHS(self):
  def _doEval(self,env,pad):
  def _doBackprop(self,env,gradAccum,pad):
  def copy(self):
  def __init__(self,dst,mode):
  def __repr__(self):
  def _ppLHS(self):
  def _doEval(self,env,pad):
  def _doBackprop(self,env,gradAccum,pad):
  def copy(self):
  def __init__(self,dst,src,matMode,transpose=False):
  def __repr__(self):
  def _ppLHS(self):
  def _doEval(self,env,pad):
  def _doBackprop(self,env,gradAccum,pad):
  def copy(self):
  def __init__(self,dst,srcs,mode,dstType=None):
  def __repr__(self):
  def _ppLHS(self):
  def _doEval(self,env,pad):
  def _doBackprop(self,env,gradAccum,pad):
  def copy(self):
  def __init__(self,dst,src,src2):
  def __repr__(self):
  def _ppLHS(self):
  def _doEval(self,env,pad):
  def _doBackprop(self,env,gradAccum,pad):
  def copy(self):
  def __init__(self,dst,weighter,vec):
  def __repr__(self):
  def _ppLHS(self):
  def _doEval(self,env,pad):
  def _doBackprop(self,env,gradAccum,pad):
  def copy(self): class XLearner(L.Learner):
class BatchEpochsLearner(XLearner):
  def __init__(self,prog,xc,compilerClass=None,regularizer=None,tracer=None,epochTracer=None):
  def predict(self,mode,X,pad=None):
  def crossEntropyGrad(self,mode,X,Y,tracerArgs={},pad=None):
  def applyUpdate(self, paramGrads, rate):
  def meanUpdate(self, functor, arity, delta, n, totalN=0):
  def train(self,dset):
  def __init__(self,prog,xc,epochs=10,compilerClass=None,regularizer=None,tracer=None,epochTracer=None):
  def trainMode(self,mode,X,Y,epochs=-1):
  def train(self,dset): class Expt(object):

    def __init__(self,configDict):
        self.config = configDict

    def run(self):
        print(('Expt configuration:',self.config))
        return self._run(**self.config)

    def _run(self,
             prog=None, trainData=None, testData=None, targetMode=None,
             savedTestPredictions=None, savedTestExamples=None, savedTrainExamples=None, savedModel=None,
             learner=None):

        """ Run an experiment.

        The stages are
        - if targetMode is specified, extract just the examples from that mode from trainData and testData
        - evaluate the untrained program on the train and test data and print results
        - train on the trainData
        - if savedModel is given, write the learned database, including the trained parameters,
          to that directory.
        - if savedTestPredictions is given, write the test-data predictions in ProPPR format
        - if savedTestExamples (savedTrainExamples) is given, save the training/test examples in ProPPR format
        """

        if targetMode:
            targetMode = declare.asMode(targetMode)
            trainData = trainData.extractMode(targetMode)
            if testData is not None: 
                testData = testData.extractMode(targetMode)

        if not learner: learner = learn.FixedRateGDLearner(prog)

        
        TP0 = Expt.timeAction(
            'running untrained theory on train data',
            lambda:learner.datasetPredict(trainData))
        Expt.printStats('untrained theory','train',trainData,TP0)
        if testData is not None:
          UP0 = Expt.timeAction(
              'running untrained theory on test data',
              lambda:learner.datasetPredict(testData))
          Expt.printStats('untrained theory','test',testData,UP0)

        Expt.timeAction('training %s' % fulltype(learner), lambda:learner.train(trainData))

        TP1 = Expt.timeAction(
            'running trained theory on train data',
            lambda:learner.datasetPredict(trainData))
        if testData is not None:
          UP1 = Expt.timeAction(
              'running trained theory on test data',
              lambda:learner.datasetPredict(testData))

        Expt.printStats('..trained theory','train',trainData,TP1)
        if testData is not None:
          testAcc,testXent = Expt.printStats('..trained theory','test',testData,UP1)
        else:
          testAcc,testXent = None,None

        if savedModel:
            Expt.timeAction('saving trained model', lambda:prog.db.serialize(savedModel))

        if savedTestPredictions and testData:
            #todo move this logic to a dataset subroutine
            open(savedTestPredictions,"w").close() # wipe file first
            def doit():
                qid=0
                for mode in testData.modesToLearn():
                    qid+=Expt.predictionAsProPPRSolutions(savedTestPredictions,mode.functor,prog.db,UP1.getX(mode),UP1.getY(mode),True,qid)
            Expt.timeAction('saving test predictions', doit)

        if savedTestExamples and testData:
            Expt.timeAction('saving test examples',
                            lambda:testData.saveProPPRExamples(savedTestExamples,prog.db))

        if savedTrainExamples:
            Expt.timeAction('saving train examples',
                            lambda:trainData.saveProPPRExamples(savedTrainExamples,prog.db))

        if savedTestPredictions and savedTestExamples and testData:
            print(('ready for commands like: proppr eval %s %s --metric auc --defaultNeg' \
                % (savedTestExamples,savedTestPredictions)))

        return testAcc,testXent


    @staticmethod
    def predictionAsProPPRSolutions(fileName,theoryPred,db,X,P,append=False,start=0):
        """Print X and P in the ProPPR solutions.txt format."""
        fp = open(fileName,'a' if append else 'w')
        dx = db.matrixAsSymbolDict(X,typeName=db.schema.getDomain(theoryPred,2))
        dp = db.matrixAsSymbolDict(P,typeName=db.schema.getRange(theoryPred,2))
        n=max(dx.keys())
        for i in range(n+1):
            #assert i in dp, "keys dp: %s\nkeys dx: %s" % (dp.keys(),dx.keys())
            dix = dx[i]
            dip = {}
            if i in dp:
                dip = dp[i]
            assert len(list(dix.keys()))==1,'X %s row %d is not onehot: %r' % (theoryPred,i,dix)
            x = list(dix.keys())[0]
            fp.write('# proved %d\t%s(%s,X1).\t999 msec\n' % (i+1+start,theoryPred,x))
            scoresdPs = reversed(sorted([(py,y) for (y,py) in list(dip.items())]))
            for (r,(py,y)) in enumerate(scoresdPs):
                fp.write('%d\t%.18f\t%s(%s,%s).\n' % (r+1,py,theoryPred,x,y))
        return n

    @staticmethod
    def timeAction(msg, act):
        """Do an action encoded as a callable function, return the result,
        while printing the elapsed time to stdout."""
        print((msg,'...'))
        start = time.time()
        result = act()
        print((msg,'... done in %.3f sec' % (time.time()-start)))
        return result

    @staticmethod
    def printStats(modelMsg,testSet,goldData,predictedData):
        """Print accuracy and crossEntropy for some named model on a named eval set."""
        acc = learn.Learner.datasetAccuracy(goldData,predictedData)
        xent = learn.Learner.datasetCrossEntropy(goldData,predictedData,perExample=True)
        print(('eval',modelMsg,'on',testSet,': acc',acc,'xent/ex',xent))
        return (acc,xent)
import logging
import numpy as np
import os
import unittest
import sys
import collections
import tempfile
  import tensorflow as tf
  import theano
from tensorlog import xctargets
  from tensorlog import tensorflowxcomp
  from tensorlog import theanoxcomp
from tensorlog import bpcompiler
from tensorlog import comline
from tensorlog import dataset
from tensorlog import declare
from tensorlog import matrixdb
from tensorlog import learn
from tensorlog import mutil
from tensorlog import parser
from tensorlog import program
from tensorlog import simple
from tensorlog import testtensorlog
from tensorlog import funs
from tensorlog import ops
from tensorlog import learnxcomp as learnxc
from tensorlog.expt import Expt
      for mode,(TX,TY) in tlog.minibatches(trainData,batch_size=2):
        print('.', end=' ')
        train_minibatch_fd = {tlog.input_placeholder_name(mode):TX, tlog.target_output_placeholder_name(mode):TY}
        session.run(train_step, feed_dict=train_minibatch_fd)
      print('epoch',i+1,'finished')
    acc1 = session.run(accuracy, feed_dict=test_batch_fd)
    print('final accuracy',acc1)
    self.assertTrue(acc1>=0.9)
    session.close()

  def testBuilder1(self):
    b = simple.Builder()
    X,Y,Z = b.variables("X Y Z")
    aunt,parent,sister,wife = b.predicates("aunt parent sister wife")
    uncle = b.predicate("uncle")
    b += aunt(X,Y) <= uncle(X,Z) & wife(Z,Y)
    b += aunt(X,Y) <= parent(X,Z) & sister(Z,Y)
    r1 = b.rule_id("ruleid_t","r1")
    r2 = b.rule_id("ruleid_t","r2")
    b += aunt(X,Y) <= uncle(X,Z) & wife(Z,Y) // r1
    b += aunt(X,Y) <= parent(X,Z) & sister(Z,Y) // r2
    feature,description = b.predicates("feature description")
    weight = b.predicate("weight")
    F = b.variable("F")
    D = b.variable("D")
    b += aunt(X,Y) <= uncle(X,Z) & wife(Z,Y) // (weight(F) | description(X,D) & feature(X,F))
    b.rules.listing()
    rs = b.rules.rulesFor(parser.Goal('aunt',[X,Y]))
    self.assertEqual(str(rs[0]), "aunt(X,Y) :- uncle(X,Z), wife(Z,Y).")
    self.assertEqual(str(rs[1]), "aunt(X,Y) :- parent(X,Z), sister(Z,Y).")
    self.assertEqual(str(rs[2]), "aunt(X,Y) :- uncle(X,Z), wife(Z,Y) {weight(R1) : assign(R1,r1,ruleid_t)}.")
    self.assertEqual(str(rs[3]), "aunt(X,Y) :- parent(X,Z), sister(Z,Y) {weight(R2) : assign(R2,r2,ruleid_t)}.")
    self.assertEqual(str(rs[4]), "aunt(X,Y) :- uncle(X,Z), wife(Z,Y) {weight(F) : description(X,D),feature(X,F)}.")

  def testBuilder2(self):
    b = simple.Builder()
    predict,assign,weighted,hasWord,posPair,negPair = b.predicates("predict assign weighted hasWord posPair negPair")
    X,Pos,Neg,F,W = b.variables("X Pos Neg F W")
    b += predict(X,Pos) <= assign(Pos,'pos','label') // (weighted(F) | hasWord(X,W) & posPair(W,F))
    b += predict(X,Neg) <= assign(Neg,'neg','label') // (weighted(F) | hasWord(X,W) & negPair(W,F))
    dbSpec = os.path.join(testtensorlog.TEST_DATA_DIR,"textcattoy3.cfacts")
    self.runTextCatLearner(simple.Compiler(db=dbSpec,prog=b.rules))

  def testBuilder3(self):
    b = simple.Builder()
    predict,assign,weighted,hasWord,posPair,negPair,label = b.predicates("predict assign weighted hasWord posPair negPair label")
    doc_t,label_t,word_t,labelWordPair_t = b.types("doc_t label_t word_t labelWordPair_t")

    b.schema += predict(doc_t,label_t)
    b.schema += hasWord(doc_t,word_t)
    b.schema += posPair(word_t,labelWordPair_t)
    b.schema += negPair(word_t,labelWordPair_t)
    b.schema += label(label_t)

    X,Pos,Neg,F,W = b.variables("X Pos Neg F W")
    b.rules += predict(X,Pos) <= assign(Pos,'pos','label_t') // (weighted(F) | hasWord(X,W) & posPair(W,F))
    b.rules += predict(X,Neg) <= assign(Neg,'neg','label_t') // (weighted(F) | hasWord(X,W) & negPair(W,F))

    # use the untyped version of the facts to make sure the schema works
    b.db = os.path.join(testtensorlog.TEST_DATA_DIR,"textcattoy.cfacts")

    self.runTextCatLearner(simple.Compiler(db=b.db, prog=b.rules))

class TestReparameterizationAndTypedLoading(unittest.TestCase):

  def testBugWasFixed(self):
    # use the untyped version of the facts to make sure the schema works
    db = matrixdb.MatrixDB()
    db.addLines(["# :- r(lo_or_hi_t)\n",
                 "\t".join("r low 0.1".split()) + "\n",
                 "\t".join("r hi 0.9".split()) + "\n"])
    db.markAsParameter('r',1)
    prog = program.Program(db=db)
    typeName = db.schema.getArgType("r",1,0)
    idLow = db.schema.getId(typeName,"low")
    idHi = db.schema.getId(typeName,"hi")
    db_r = db.matEncoding[('r',1)]
    self.approxEqual(db_r[0,idLow], 0.1)
    self.approxEqual(db_r[0,idHi], 0.9)

    xc = tensorflowxcomp.SparseMatDenseMsgCrossCompiler(prog)
    v_r = xc._vector(declare.asMode("r(i)"))

    session = tf.Session()
    session.run(tf.global_variables_initializer())
    xc.exportAllLearnedParams()
    print('exported to xc',db.matEncoding[('r',1)])
    db_r = db.matEncoding[('r',1)]
    self.approxEqual(db_r[0,idLow], 0.1)
    self.approxEqual(db_r[0,idHi], 0.9)

  def approxEqual(self,a,b):
    self.assertTrue(abs(float(a)-b) < 0.0001)

class TestPlugins(unittest.TestCase):

  def test_identity_io(self):
    ruleStrings = ['predict(X,Y) :- assign(Pos,pos,label),udp1(Pos,Y) {weighted(F): hasWord(X,W),posPair(W,F)}.',
                   'predict(X,Y) :- assign(Neg,neg,label),udp1(Neg,Y) {weighted(F): hasWord(X,W),negPair(W,F)}.']
    plugins = program.Plugins()
    plugins.define('udp1/io', lambda x:x, lambda inputType:'label')
    self.check_learning_with_udp(ruleStrings,plugins)

  def test_identity_oi(self):
    ruleStrings = ['predict(X,Y) :- assign(Pos,pos,label),udp2(Y,Pos) {weighted(F): hasWord(X,W),posPair(W,F)}.',
                   'predict(X,Y) :- assign(Neg,neg,label),udp2(Y,Neg) {weighted(F): hasWord(X,W),negPair(W,F)}.']
    plugins = program.Plugins()
    plugins.define('udp2/oi', lambda x:x, lambda inputType:'label')
    self.check_learning_with_udp(ruleStrings,plugins)

  def test_double_io1(self):
    ruleStrings = ['predict(X,Y) :- assign(Pos,pos,label),udp3(Pos,Y) {weighted(F): hasWord(X,W),posPair(W,F)}.',
                   'predict(X,Y) :- assign(Neg,neg,label),udp3(Neg,Y) {weighted(F): hasWord(X,W),negPair(W,F)}.']
    plugins = program.Plugins()
    plugins.define('udp3/io', lambda x:2*x, lambda inputType:'label')
    self.check_learning_with_udp(ruleStrings,plugins)

  def test_double_io2(self):
    ruleStrings = ['predict(X,Pos) :- assign(Pos,pos,label) {weighted(F): hasWord(X,W),double(W,W2),posPair(W2,F)}.',
                   'predict(X,Neg) :- assign(Neg,neg,label) {weighted(F2): hasWord(X,W),negPair(W,F),double(F,F2)}.']

    plugins = program.Plugins()