Example #1
0
 def dirichlet_expectation(self, alpha):
     if (len(alpha.shape) == 1):
         result = digam(alpha) - digam(numpy.sum(alpha))
     else:
         result = digam(alpha) - digam(numpy.sum(alpha, 1))[:,
                                                            numpy.newaxis]
     return result.astype(alpha.dtype)
Example #2
0
    def new_phi(gamma, beta, word, count):
        """
        Given gamma vector and complete beta, compute the phi for a word with a
        given count
        """

        phi =  beta[:,word] * numpy.array([exp(digam(i) - digam(sum(gamma))) for i in gamma])

        return ((phi / sum(phi)) * count)
Example #3
0
    def new_phi(gamma, beta, word, count):
        """
        Given gamma vector and complete beta, compute the phi for a word with a
        given count
        """

        phi = []
        for i in range(len(beta)):
            phi.append(beta[i][word] * exp(digam(gamma[i] + digam(sum(gamma)))) )
        return [float(i) * count/sum(phi) for i in phi]
Example #4
0
    def new_phi(gamma, beta, word, count):
        """
        Given gamma vector and complete beta, compute the phi for a word with a
        given count
        """

        phi = []
        for i in range(len(beta)):
            phi.append(beta[i][word] *
                       exp(digam(gamma[i] + digam(sum(gamma)))))
        return [float(i) * count / sum(phi) for i in phi]
    def new_phi(gamma, beta, word, count):
        """
        Given gamma vector and complete beta, compute the phi for a word with a
        given count
        """

        # TODO: Complete this function!
        v = zeros(len(gamma))
        sum_gamma = gamma.sum()
        v = beta[:, word] * exp(digam(gamma) - digam(sum_gamma))

        return count * v / v.sum()
Example #6
0
    def new_phi(gamma, beta, word, count):
        """
        Given gamma vector and complete beta, compute the phi for a word with a
        given count
        """
        doc_propensity = digam(gamma)
        doc_propensity -= digam(sum(gamma))
        log_phi = doc_propensity.copy()
        log_phi += log(beta[:, word].T)
        contrib = exp(log_phi)
        contrib *= count / sum(contrib)

        return contrib
Example #7
0
 def new_phi(gamma, beta, word, count):
     """
     Given gamma vector and complete beta, compute the phi for a word with a
     given count
     """
     phi = beta[:, word] * exp(digam(gamma))
     return phi * count / numpy.sum(phi)
    def new_phi(gamma, beta, word, count):
        """
        Given gamma vector and complete beta, compute the phi for a word with a
        given count
        """

        beta_word = beta[:, word]
        exp_term = digam(gamma) - digam(sum(gamma))
        exp_vec = exp(exp_term)
        unnorm = multiply(beta_word, exp_vec)
        if count == 0:
            count = 1

        norm_term = sum(unnorm) / count
        phi = unnorm / norm_term
        return phi
Example #9
0
 def new_phi(gamma, beta, word, count):
     """
     Given gamma vector and complete beta, compute the phi for a word with a
     given count
     """
     phi = beta[:, word] * exp(digam(gamma))
     return phi * count / numpy.sum(phi)
Example #10
0
    def new_phi(gamma, beta, word, count):
        """
        Given gamma vector and complete beta, compute the phi for a word with a
        given count
        """
        phi = ones(len(gamma))

        # TODO: Complete this function!
        gamma_sum = sum(gamma)

        # phi_n_i = Norm: Beta_i_v * exp(Psi(gamma_i) - Psi(Sum(gamma_j)))
        for i in range(0, len(gamma)):
            phi[i] = beta[i][word] * exp(digam(gamma[i]) - digam(gamma_sum))
        phi_sum = sum(phi)
        norm_phi = [float(i) * count / phi_sum for i in phi]
        return norm_phi
Example #11
0
    def new_phi(gamma, beta, word, count):
        """
        Given gamma vector and complete beta, compute the phi for a word with a
        given count
        """
        phi = ones(len(gamma))

        # TODO: Complete this function!
        gamma_sum = sum(gamma)


        # phi_n_i = Norm: Beta_i_v * exp(Psi(gamma_i) - Psi(Sum(gamma_j)))
        for i in range(0, len(gamma)):
            phi[i] = beta[i][word] * exp(digam(gamma[i])-digam(gamma_sum)) 
        phi_sum = sum(phi)
        norm_phi = [float(i)*count/phi_sum for i in phi]
        return norm_phi
Example #12
0
    def update_dir_prior(self, prior, N, logphat):
        """
        Updates a given prior using Newton's method, described in
        **Huang: Maximum Likelihood Estimation of Dirichlet Distribution Parameters.**
        http://jonathan-huang.org/research/dirichlet/dirichlet.pdf
        """
        dprior = numpy.copy(prior)
        gradf = N * (digam(numpy.sum(prior)) - digam(prior) + logphat)

        c = N * polygamma(1, numpy.sum(prior))
        q = -N * polygamma(1, prior)

        b = numpy.sum(gradf / q) / (1 / c + numpy.sum(1 / q))

        dprior = -(gradf - b) / q

        return numpy.average(prior + dprior)
    def new_phi(gamma, beta, word, count):
        """
        Given gamma vector and complete beta, compute the phi for a word with a
        given count
        """

        # TODO: Complete this function
        phi = (beta[:,word] * exp(digam(gamma)))
        return  phi * (count / sum(phi))
Example #14
0
 def update_alpha(self, current_alpha=None, gamma=None):
     """
     Update the scalar parameter alpha based on a gamma matrix.  If
     no gamma argument is supplied, use the current setting of
     gamma.
     Refer to http://jonathan-huang.org/research/dirichlet/dirichlet.pdf
     & https://github.com/piskvorky/gensim/blob/master/gensim/models/ldamodel.py
     """
     if current_alpha is None:
         current_alpha = self._alpha
     if gamma is None:
         gamma = self._gamma
     n, k = gamma.shape
     alpha_vec = numpy.zeros(k)
     alpha_vec.fill(current_alpha)
     logphat = numpy.sum(dirichlet_log_expectation(g) for g in gamma) / n
     gradf = n * (digam(numpy.sum(alpha_vec)) - digam(alpha_vec) + logphat)
     c = n * polygamma(1, numpy.sum(alpha_vec))
     q = -n * polygamma(1, alpha_vec)
     b = numpy.sum(gradf / q) / (1. / c + numpy.sum(1. / q))
     dalpha = numpy.mean(-(gradf - b) / q)
     return dalpha + current_alpha if dalpha + current_alpha > 0 else current_alpha
    def update_alpha(self, current_alpha=None, gamma=None):
        """
        Update the scalar parameter alpha based on a gamma matrix.  If
        no gamma argument is supplied, use the current setting of
        gamma.
        """

        if current_alpha is None:
            current_alpha = self._alpha
        if gamma is None:
            gamma = self._gamma

        # Update below line
        if self.ualpha:
            first_order_derivative = (self._num_docs) * (self._num_topics) * (
                derivdigam(1, (self._num_topics) * current_alpha) -
                derivdigam(1, current_alpha))
            psi_gamma = digam(gamma)
            second_term = numpy.subtract(psi_gamma,
                                         digam(gamma.sum(axis=1))[:, None])
            first_order_derivative += (second_term).sum(axis=0)
            # print (len(first_order_derivative))
            # exit()
            second_order_derivative = (self._num_docs) * (self._num_topics) * (
                (self._num_topics) *
                (derivdigam(2, (self._num_topics) * current_alpha)) -
                (derivdigam(2, current_alpha)))

            denominator = second_order_derivative * current_alpha
            denominator += first_order_derivative

            # print (denominator)
            update_delta = first_order_derivative / (denominator)
            new_alpha = current_alpha / exp(update_delta)

        else:
            new_alpha = current_alpha

        return new_alpha
Example #16
0
 def update_alpha(self, current_alpha=None, gamma=None):
     """
     Update the scalar parameter alpha based on a gamma matrix.  If
     no gamma argument is supplied, use the current setting of
     gamma.
     Refer to http://jonathan-huang.org/research/dirichlet/dirichlet.pdf
     & https://github.com/piskvorky/gensim/blob/master/gensim/models/ldamodel.py
     """
     if current_alpha is None:
         current_alpha = self._alpha
     if gamma is None:
         gamma = self._gamma
     n, k = gamma.shape
     alpha_vec = numpy.zeros(k)
     alpha_vec.fill(current_alpha)
     logphat = numpy.sum(dirichlet_log_expectation(g) for g in gamma) / n
     gradf = n * (digam(numpy.sum(alpha_vec)) - digam(alpha_vec) + logphat)
     c = n * polygamma(1, numpy.sum(alpha_vec))
     q = -n * polygamma(1, alpha_vec)
     b = numpy.sum(gradf / q) / (1. / c + numpy.sum(1. / q))
     dalpha = numpy.mean(-(gradf - b) / q)
     return dalpha + current_alpha if dalpha + current_alpha > 0 else current_alpha
Example #17
0
def dirichlet_log_expectation(alpha):
    """
    For a vector `theta~Dir(alpha)`, compute `E[log(theta)]`.
    """
    return digam(alpha) - digam(numpy.sum(alpha))
Example #18
0
def dirichlet_log_expectation(alpha):
    """
    For a vector `theta~Dir(alpha)`, compute `E[log(theta)]`.
    """
    return digam(alpha) - digam(numpy.sum(alpha))