Example #1
0
def match_rows(X, Y):
    """
    Permute the rows of _X_ to minimize error with Y
    @params X numpy.array - input matrix
    @params Y numpy.array - comparison matrix
    @return numpy.array - X with permuted rows
    """
    n, d = X.shape
    n_, d_ = Y.shape
    assert n == n_ and d == d_

    # Create a weight matrix to compare the two
    W = zeros((n, n))
    for i, j in it.product(xrange(n), xrange(n)):
        # Cost of 'assigning' j to i.
        W[i, j] = norm(X[j] - Y[i])

    matching = Munkres().compute( W )
    matching.sort()
    _, rowp = zip(*matching)
    rowp = array( rowp )
    # Permute the rows of B according to Bi
    X_ = X[ rowp ]

    return X_
Example #2
0
def match_rows_sign(X, Y):
    """
    Permute the rows of _X_ to minimize error with Y, ignoring signs of the columns
    @params X numpy.array - input matrix
    @params Y numpy.array - comparison matrix
    @return numpy.array - X with permuted rows
    """
    n, d = X.shape
    n_, d_ = Y.shape
    assert n == n_ and d == d_

    # Create a weight matrix to compare the two
    W = zeros((n, n))
    for i, j in it.product(xrange(n), xrange(n)):
        # Cost of 'assigning' j to i.
        W[i, j] = min(norm(X[j] - Y[i]), norm(X[j] + Y[i]) )

    matching = Munkres().compute( W )
    matching.sort()
    _, rowp = zip(*matching)
    rowp = array( rowp )
    # Permute the rows of B according to Bi
    X_ = X[ rowp ]
    # Change signs to minimize loss
    for row in xrange(n):
        if norm(X_[row] + Y[row]) < norm(X_[row] - Y[row]):
            X_[row] *= -1

    return X_
Example #3
0
def match_rows(X, Y):
    """
    Permute the rows of _X_ to minimize error with Y
    @params X numpy.array - input matrix
    @params Y numpy.array - comparison matrix
    @return numpy.array - X with permuted rows
    """
    n, d = X.shape
    n_, d_ = Y.shape
    assert n == n_ and d == d_

    # Create a weight matrix to compare the two
    W = zeros((n, n))
    for i, j in it.product(xrange(n), xrange(n)):
        # Cost of 'assigning' j to i.
        W[i, j] = norm(X[j] - Y[i])

    matching = Munkres().compute(W)
    matching.sort()
    _, rowp = zip(*matching)
    rowp = array(rowp)
    # Permute the rows of B according to Bi
    X_ = X[rowp]

    return X_
Example #4
0
def match_rows_sign(X, Y):
    """
    Permute the rows of _X_ to minimize error with Y, ignoring signs of the columns
    @params X numpy.array - input matrix
    @params Y numpy.array - comparison matrix
    @return numpy.array - X with permuted rows
    """
    n, d = X.shape
    n_, d_ = Y.shape
    assert n == n_ and d == d_

    # Create a weight matrix to compare the two
    W = zeros((n, n))
    for i, j in it.product(xrange(n), xrange(n)):
        # Cost of 'assigning' j to i.
        W[i, j] = min(norm(X[j] - Y[i]), norm(X[j] + Y[i]))

    matching = Munkres().compute(W)
    matching.sort()
    _, rowp = zip(*matching)
    rowp = array(rowp)
    # Permute the rows of B according to Bi
    X_ = X[rowp]
    # Change signs to minimize loss
    for row in xrange(n):
        if norm(X_[row] + Y[row]) < norm(X_[row] - Y[row]):
            X_[row] *= -1

    return X_
Example #5
0
    def _find_best_permutation(self, spectral, spatial, idx_constant):
        '''Finds the best permutation of classes in spectral and spatial model.

        Args:
            spectral (np.array): Conditional log-likelihood of spectral model 
                                 (as in Eq.19) in [1], shape (N,T,F)
            spatial (np.array): Conditional log-likelihood of spatial model 
                                (as in Eq.19) in [1], shape (N,T,F)
            idx_constant (tuple or int) indices of axis which have constant permutation
                Examples:
                    idx_constant = (1,2) 
                        -> for all time frames and frequency bins 
                           the permutation is constant 
                           (finding 1 global permutation)
                    idx_constant = 1
                        -> for all time frames the permutation is constant
                           (finding permutation for each frequency)

        Returns:
            permutations (dict): mapping tuples of time and frequency indices
                                 to the best permutation. For constant indices, 
                                 the map contains only index 0.
                Examples:
                    permutations = {(0,0) : [2, 0, 1]}
                        -> one global permutation (idx_constant = (1,2))
                        -> spectral comp. 0 corresponds to spatial comp. 2
                        -> spectral comp. 1 corresponds to spatial comp. 0
                        -> spectral comp. 2 corresponds to spatial comp. 1

        [1] Integration of variational autoencoder and spatial clustering 
            for adaptive multi-channel neural speech separation; 
            K. Zmolikova, M. Delcroix, L. Burget, T. Nakatani, J. Cernocky
        '''
        if isinstance(idx_constant, int):
            idx_constant = (idx_constant, )
        idx_constant = tuple([i + 1 for i in idx_constant])

        perm_scores = logsumexp(spectral[:, None, :, :] +
                                spatial[None, :, :, :],
                                axis=idx_constant)
        perm_scores = np.expand_dims(perm_scores, idx_constant)

        permutations = {}
        for i1, i2 in np.ndindex(perm_scores.shape[-2:]):
            idx_perm = Munkres().compute(
                make_cost_matrix(perm_scores[:, :, i1, i2]))
            idx_perm.sort(key=lambda x: x[0])
            permutations[i1, i2] = [i[1] for i in idx_perm]

        return permutations