Пример #1
0
def calculate_EZ_from_small_log_phis(log_phi1, log_phi2):
    """
        Accepts a two small phi matrices (like (NdxK) and (NcxJ))
        Calculates E[Zd].
        Returns the final vector (K+J).

        E[Z] = φ := (1/N)ΣNφn
    """
    Ndc = log_phi1.shape[0] + log_phi2.shape[0]
    ez = np.concatenate((logsumexp(log_phi1, axis=0), logsumexp(log_phi2, axis=0)), axis=1)
    return ez - np.log(Ndc)
Пример #2
0
def calculate_EZ_from_small_phis(phi1, phi2):
    """
        Accepts a two small phi matrices (like (NdxK) and (NcxJ))
        Calculates E[Zd].
        Returns the final vector (K+J).

        E[Z] = φ := (1/N)ΣNφn
    """
    Ndc = phi1.shape[0] + phi2.shape[0]
    ez = np.concatenate((np.sum(phi1, axis=0), np.sum(phi2, axis=0)), axis=1)
    return ez / Ndc
Пример #3
0
def tlc_e_step(global_iterations, v):
    total_local_i = 0
    dlocal_i, clocal_i, local_i, blocal_i  = 0,0,0,0
    for d, (document, comment) in enumerate(v.iterdocuments()):
        # todo: this should be more complicated in order to share strength
        dlocal_i = topiclib.lda_E_step_for_doc(global_iterations, 
                                                dlocal_i,
                                                d, document,
                                                v.alphaU, v.beta[:v.Ku],
                                                v.gammaD[d], v.phiD[d])

        alphaC = np.concatenate((v.alphaU, v.alphaS))
        clocal_i = topiclib.lda_E_step_for_doc(global_iterations,
                                                clocal_i,
                                                d, comment,
                                                alphaC, v.beta[:v.Kc],
                                                v.gammaC[d], v.phiC[d])
    total_local_i += dlocal_i
    total_local_i += clocal_i
        

    for l, (labeled_doc,y) in enumerate(v.iterlabeled()):
        alphaL = np.concatenate((v.alphaS, v.alphaB))
        local_i = topiclib.partial_slda_E_step_for_doc(global_iterations,
                                                        local_i,
                                                        l, labeled_doc, y,
                                                        alphaL, v.beta[-v.Kl:],
                                                        v.gammaL[l], v.phiL[l],
                                                        v.eta, v.sigma_squared)
    total_local_i += local_i

    for b, bg_doc in enumerate(v.iterbackground()):
        blocal_i = topiclib.lda_E_step_for_doc(global_iterations, 
                                                blocal_i,
                                                b, bg_doc,
                                                v.alphaB, v.beta[-v.Kb:],
                                                v.gammaB[b], v.phiB[b])
    total_local_i += blocal_i

    return total_local_i
Пример #4
0
def calculate_EZZT_from_small_log_phis(phi1, phi2):
    """
        Accepts a big phi matrix (like ((Nd+Nc) x (K+J))
        Calculates E[ZdZdT].
        Returns the final matrix ((K+J) x (K+J)).

        (Also, E[ZdZdT] = (1/N2)(ΣNΣm!=nφd,nφd,mT  +  ΣNdiag{φd,n})
    """
    Nd,K = phi1.shape
    Nc,J = phi2.shape
    (Ndc, KJ) = (Nd+Nc, K+J)
    inner_sum = np.zeros((KJ, KJ))

    p1 = np.matrix(phi1)
    p2 = np.matrix(phi2)

    for i in xrange(K):
        for j in xrange(K):
            m = logdotexp(np.matrix(p1[:,i]), np.matrix(p1[:,j]).T)
            m += np.diagonal(np.ones(Nd) * -1000)
            inner_sum[i,j] = logsumexp(m.flatten())

    for i in xrange(J):
        for j in xrange(J):
            m = logdotexp(np.matrix(p2[:,i]), np.matrix(p2[:,j]).T)
            m += np.diagonal(np.ones(Nc) * -1000)
            inner_sum[K+i,K+j] = logsumexp(m.flatten())

    for i in xrange(K):
        for j in xrange(J):
            m = logdotexp(np.matrix(p1[:,i]), np.matrix(p2[:,j]).T)
            inner_sum[i,K+j] = logsumexp(m.flatten())

    for i in xrange(J):
        for j in xrange(K):
            m = logdotexp(np.matrix(p2[:,i]), np.matrix(p1[:,j]).T)
            inner_sum[K+i,j] = logsumexp(m.flatten())

    big_phi_sum = np.concatenate((logsumexp(phi1, axis=0),
                                  logsumexp(phi2, axis=0)), axis=1)
    ensure(big_phi_sum.shape == (KJ,))
    for i in xrange(KJ):
        inner_sum[i,i] = logsumexp([inner_sum[i,i], big_phi_sum[i]])

    inner_sum -= np.log(Ndc * Ndc)
    return inner_sum
Пример #5
0
def calculate_EZZT_from_small_phis(phi1, phi2):
    """
        Accepts a big phi matrix (like ((Nd+Nc) x (K+J))
        Calculates E[ZdZdT].
        Returns the final matrix ((K+J) x (K+J)).

        (Also, E[ZdZdT] = (1/N2)(ΣNΣm!=nφd,nφd,mT  +  ΣNdiag{φd,n})
    """
    Nd,K = phi1.shape
    Nc,J = phi2.shape
    (Ndc, KJ) = (Nd+Nc, K+J)
    inner_sum = np.zeros((KJ, KJ))

    p1 = np.matrix(phi1)
    p2 = np.matrix(phi2)

    for i in xrange(K):
        for j in xrange(K):
            m = np.dot(np.matrix(p1[:,i]), np.matrix(p1[:,j]).T)
            inner_sum[i,j] = np.sum(m) - np.sum(np.diagonal(m))

    for i in xrange(J):
        for j in xrange(J):
            m = np.dot(np.matrix(p2[:,i]), np.matrix(p2[:,j]).T)
            inner_sum[K+i,K+j] = np.sum(m) - np.sum(np.diagonal(m))

    for i in xrange(K):
        for j in xrange(J):
            m = np.dot(np.matrix(p1[:,i]), np.matrix(p2[:,j]).T)
            inner_sum[i,K+j] = np.sum(m)

    for i in xrange(J):
        for j in xrange(K):
            m = np.dot(np.matrix(p2[:,i]), np.matrix(p1[:,j]).T)
            inner_sum[K+i,j] = np.sum(m)

    big_phi_sum = np.concatenate((np.sum(phi1, axis=0),
                                  np.sum(phi2, axis=0)), axis=1)
    ensure(big_phi_sum.shape == (KJ,))
    inner_sum += np.diagonal(big_phi_sum)

    inner_sum /= (Ndc * Ndc)
    return inner_sum
Пример #6
0
            return np.array([np.sum(matrix[:,i]) for i in xrange(ncols)])
    else:
        rowsums = np.sum(matrix, axis=1)
        return rowsums

def np_concatenate((a, b), axis=1):
    """Accepts a 1-d array and axis (only 1) and concatenates them.
    """
    assert axis in [1]
    if ispypy():
        n = np.zeros((len(a) + len(b),))
        n[:len(a)] = a
        n[len(a):] = b
        return n
    else:
        return np.concatenate(a, b, axis=1)


def matrix_multiply(a, b):
    """Takes two matrices and does a complicated matrix multiply.  Yes that one.
    NOTE: THIS APPEARS TO BE VERY BROKEN
    """
    if len(a.shape) == 1:
        nrows, = a.shape
        a = np.zeros((nrows, 1))

    if len(b.shape) == 1:
        bc, = b.shape
        if bc == a.shape[1]:
            b = np.zeros((bc, 1))
        else: