Exemplo n.º 1
0
 def memo_factorial_term_for_merge(self, LP, mPairIDs):
   if mPairIDs is None:
     logFactZMerge = NumericHardUtil.colwisesumLogFactorial_allpairs(
                             LP['hard_asgn'])
   else:
     logFactZMerge = NumericHardUtil.colwisesumLogFactorial_specificpairs(
                             LP['hard_asgn'], mPairIDs)
   return logFactZMerge
Exemplo n.º 2
0
 def get_hard_word_variational(self, Data, LP):
   ''' Update and return word-topic assignment variational parameters
   '''
   LP['word_variational'] = NumericHardUtil.toHardAssignmentMatrix(
                                                   LP['word_variational'])
   return LP
   """
Exemplo n.º 3
0
 def calc_local_params(self, Data, LP, **kwargs):
     """ Calculate local parameters for each data item and each component.    
     This is part of the E-step.
     
     Args
     -------
     Data : bnpy data object with Data.nObs observations
     LP : local param dict with fields
           E_log_soft_ev : Data.nObs x K array
               E_log_soft_ev[n,k] = log p(data obs n | comp k)
     
     Returns
     -------
     LP : local param dict with fields
           resp : Data.nObs x K array whose rows sum to one
           resp[n,k] = posterior responsibility that comp. k has for data n                
 """
     lpr = LP["E_log_soft_ev"]
     lpr += self.Elogw
     LP["resp"] = NumericHardUtil.toHardAssignmentMatrix(lpr)
     assert np.allclose(LP["resp"].sum(axis=1), 1)
     return LP
Exemplo n.º 4
0
  def calc_local_params(self, Data, LP, 
                              nCoordAscentItersLP=20, convThrLP=0.01,   
                              doOnlySomeDocsLP=True, **kwargs):
    ''' Calculate document-specific quantities (E-step) using hard assignments.

        Alternate updates to two terms until convergence
            (1) Approx posterior on topic-token assignment
                 q(word_variational | word_token_variables)
            (2) Approx posterior on doc-topic probabilities
                 q(doc_variational | document_topic_variables)
        Returns
        -------
        LP : local params dict
    '''
    # First, run soft assignments for nCoordAscentIters
    LP = self.calc_local_params_fast(Data, LP, 
                                        nCoordAscentItersLP,
                                        convThrLP,
                                        doOnlySomeDocsLP,
                                     )

    # Next, find hard assignments
    LP['hard_asgn'] = NumericHardUtil.findMode_Mult(
                                      Data.word_count,
                                      LP['word_variational']
                                      )

    # Update DocTopicCount to use hard assignments
    for d in xrange(Data.nDoc):
      start = Data.doc_range[d,0]
      stop = Data.doc_range[d,1]
      LP['DocTopicCount'][d,:] = np.sum(LP['hard_asgn'][start:stop], axis=0)

    # Update alphPi to use hard assignments
    LP = self.get_doc_variational(Data, LP)
    LP = self.calc_ElogPi(LP)

    return LP
Exemplo n.º 5
0
 def get_hard_word_variational(self, Data, LP):
     """ Update and return word-topic assignment variational parameters
 """
     LP["word_variational"] = NumericHardUtil.toHardAssignmentMatrix(LP["word_variational"])
     return LP
     """
Exemplo n.º 6
0
 def E_logfactorialZ(self, Data, LP):
   logFactData = NumericHardUtil.colwisesumLogFactorial(Data.word_count)
   logFactHardAsgn = NumericHardUtil.colwisesumLogFactorial(LP['hard_asgn'])
   return logFactData, logFactHardAsgn