Exemplo n.º 1
0
 def setAllWeights(self):
     """ Set all parameter weights to a plausible value - mostly useful for proppr programs,
     where parameters are known. """
     logging.debug('setting feature weights %.3f Gb' % util.memusage())
     self.setFeatureWeights()
     logging.debug('setting rule weights %.3f Gb' % util.memusage())
     self.setRuleWeights()
     self.db.checkTyping()
Exemplo n.º 2
0
 def __init__(self,db,summaryFile=None):
   logging.debug('SparseMatDenseMsgCrossCompiler calling %r %.3f Gb' % (super(SparseMatDenseMsgCrossCompiler,self).__init__,util.memusage()))
   super(SparseMatDenseMsgCrossCompiler,self).__init__(db,summaryFile=summaryFile)
   logging.debug('SparseMatDenseMsgCrossCompiler finished super.__init__ %.3f Gb' % util.memusage())
   # we will need to save the original indices/indptr representation
   # of each sparse matrix
   self.sparseMatInfo = {}
   logging.debug('SparseMatDenseMsgCrossCompiler initialized %.3f Gb' % util.memusage())
Exemplo n.º 3
0
  def runExpt(self,prog=None,trainData=None,testData=None,targetMode=None,
              savedTestPredictions=None,savedTestExamples=None,savedTrainExamples=None,savedModel=None,
              optimizer=None, epochs=10, minibatchSize=0):
    """Similar to tensorlog.expt.Expt().run()
    """
    assert targetMode is not None,'targetMode must be specified'
    assert prog is not None,'prog must be specified'
    logging.debug('runExpt calling setAllWeights %.3f Gb' % util.memusage())
    prog.setAllWeights()
    logging.debug('runExpt finished setAllWeights %.3f Gb' % util.memusage())

    expt.Expt.timeAction('compiling and cross-compiling', lambda:self.ensureCompiled(targetMode,inputs=None))

    assert optimizer is None,'optimizers not supported yet'
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
    train_step = optimizer.minimize(self.ws.dataLossExpr, var_list=self.getParamVariables(targetMode))
    X = trainData.getX(targetMode)
    Y = trainData.getY(targetMode)
    X,Y = self._ensureWrapped(X,Y,False)
    TX = testData.getX(targetMode)
    TY = testData.getY(targetMode)
    TX,TY = self._ensureWrapped(TX,TY,False)

    lossFun = self.dataLossFunction(targetMode,wrapInputs=False,unwrapOutputs=False)
    def printLoss(msg,X,Y): print msg,lossFun(X,Y)
    def printAccuracy(msg,X,Y): print msg,self.accuracy(targetMode,X,Y,wrapped=True)

    expt.Expt.timeAction('computing train loss',lambda:printLoss('initial train loss',X,Y))
    expt.Expt.timeAction('computing test loss',lambda:printLoss('initial test loss',TX,TY))
    expt.Expt.timeAction('computing train accuracy',lambda:printAccuracy('initial train accuracy',X,Y))
    expt.Expt.timeAction('computing test accuracy',lambda:printAccuracy('initial test accuracy',TX,TY))

    expt.Expt.timeAction('training', lambda:self.optimizeDataLoss(targetMode,optimizer,X,Y,epochs=epochs,minibatchSize=minibatchSize,wrapped=True))

    expt.Expt.timeAction('computing train loss',lambda:printLoss('final train loss',X,Y))
    expt.Expt.timeAction('computing test loss',lambda:printLoss('final test loss',TX,TY))
    expt.Expt.timeAction('computing train accuracy',lambda:printAccuracy('final train accuracy',X,Y))
    expt.Expt.timeAction('computing test accuracy',lambda:printAccuracy('final test accuracy',TX,TY))

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

    def savePredictions(fileName):
      inferenceFun = self.inferenceFunction(targetMode,wrapInputs=False,unwrapOutputs=True)
      Y_ = inferenceFun(TX)
      # Y_ is unwrapped, but need to get unwrapped version of TX from testData
      expt.Expt.predictionAsProPPRSolutions(fileName,targetMode.functor,prog.db,testData.getX(targetMode),Y_)

    if savedTestPredictions:
      expt.Expt.timeAction('saving test predictions', lambda:savePredictions(savedTestPredictions))
    if savedTestExamples:
      expt.Expt.timeAction('saving test examples', lambda:testData.saveProPPRExamples(savedTestExamples,prog.db))
    if savedTrainExamples:
      expt.Expt.timeAction('saving train examples',lambda:trainData.saveProPPRExamples(savedTrainExamples,prog.db))
    if savedTestPredictions and savedTestExamples:
      print 'ready for commands like: proppr eval %s %s --metric auc --defaultNeg' % (savedTestExamples,savedTestPredictions)
Exemplo n.º 4
0
 def ensureSessionInitialized(self):
   """ Make sure the varables in the session have been initialized,
   initializing them if needed
   """
   if self.session is None:
     logging.debug('creating session %.3f Gb' % util.memusage())
     self.session = tf.Session()
     logging.debug('session created %.3f Gb' % util.memusage())
   if not self.sessionInitialized:
     logging.debug('initializing session %.3f Gb' % util.memusage())
     for var in self.tfVarsToInitialize:
       self.session.run(var.initializer)
     self.sessionInitialized = True
     logging.debug('session initialized %.3f Gb' % util.memusage())
Exemplo n.º 5
0
 def __init__(self, prog):
     # We need to create variables in different namespaces for
     # different instances of an OpSeqFunction, so that the variables
     # used to represent OpSeqFunction intermediate values don't clash.
     # namespaces are defined by integer ids, and we allocate a new one
     # for every distinct OpSeqFunction that gets compiled.
     self._nextNamespaceId = 0
     # holds outputs of compilation, indexed by mode
     self._wsDict = {}
     # holds output of current compilation process
     self.ws = None
     # pointers back to the program and matrixdb
     self.prog = prog
     self.db = prog.db
     # when a db is 'typeless', ie all entities are of type
     # matrixdb.THING, then _onlyType is set to THING
     self._onlyType = None
     # maps typeName to the vector used to introduce NULL entities,
     # with low weight, into a vector of type typeName
     self._nullSmoother = {}
     # set after vectors are allocated for the nullSmoother's
     self._globalsSet = None
     # Cache 'handle expressions' for some of the objects in the
     # tensorlog database.  The handle expressions are indexed by a
     # (functor,arity) pair. Handle expressions must be inserted by
     # calling insertHandleExpr().
     self._handleExpr = {}
     # For each handle expression, there is some underlying variable
     # with a gradient that is optimized.  Often this is the same as the
     # handle expression, but not always.  These are indexed by
     # functor,arity key.
     self._handleExprVar = {}
     logging.debug('AbstractCrossCompiler initialized %.3f Gb' %
                   util.memusage())
Exemplo n.º 6
0
 def __init__(self,db,summaryFile=None):
   """If summaryFile is provided, save some extra information to pass on
   to tensorboard.
   """
   super(TensorFlowCrossCompiler,self).__init__(db)
   self.tfVarsToInitialize = []
   self.summaryFile = summaryFile
   self.session = None
   self.sessionInitialized = None
   logging.debug('TensorFlowCrossCompiler initialized %.3f Gb' % util.memusage())
Exemplo n.º 7
0
 def status(msg):
     logging.info('%s time %.3f sec mem %.3f Gb' %
                  (msg, time.time() - startTime, util.memusage()))