def lower_half(mat):
    # Takes the lower half of the matrix, and half the diagonal.
    # Necessary since numpy only uses lower half of covariance matrix.
    if len(mat.shape) == 2:
        return 0.5 * (np.tril(mat) + np.triu(mat, 1).T)
    elif len(mat.shape) == 3:
        return 0.5 * (np.tril(mat) + np.swapaxes(np.triu(mat, 1), 1,2))
    else:
        raise ArithmeticError
예제 #2
0
 def unpack_params(params):
     """Unpacks parameter vector into the proportions, means and covariances
     of each mixture component.  The covariance matrices are parametrized by
     their Cholesky decompositions."""
     log_proportions    = parser.get(params, 'log proportions')
     normalized_log_proportions = log_proportions - logsumexp(log_proportions)
     means              = parser.get(params, 'means')
     lower_tris = np.tril(parser.get(params, 'lower triangles'), k=-1)
     diag_chols = np.exp( parser.get(params, 'log diagonals'))
     chols = []
     for lower_tri, diag in zip(lower_tris, diag_chols):
         chols.append(np.expand_dims(lower_tri + np.diag(diag), 0))
     chols = np.concatenate(chols, axis=0)
     return normalized_log_proportions, means, chols