def test_eigvalsh():
    if not imported_scipy:
        raise SkipTest("Scipy needed for the geigvalsh op.")
    import scipy.linalg

    A = theano.tensor.dmatrix('a')
    B = theano.tensor.dmatrix('b')
    f = function([A, B], eigvalsh(A, B))

    rng = np.random.RandomState(utt.fetch_seed())
    a = rng.randn(5, 5)
    a = a + a.T
    for b in [10 * np.eye(5, 5) + rng.randn(5, 5)]:
        w = f(a, b)
        refw = scipy.linalg.eigvalsh(a, b)
        np.testing.assert_array_almost_equal(w, refw)

    # We need to test None separatly, as otherwise DebugMode will
    # complain, as this isn't a valid ndarray.
    b = None
    B = theano.tensor.NoneConst
    f = function([A], eigvalsh(A, B))
    w = f(a)
    refw = scipy.linalg.eigvalsh(a, b)
    np.testing.assert_array_almost_equal(w, refw)
Exemple #2
0
def test_eigvalsh():
    if not imported_scipy:
        raise SkipTest("Scipy needed for the geigvalsh op.")
    import scipy.linalg

    A = theano.tensor.dmatrix('a')
    B = theano.tensor.dmatrix('b')
    f = function([A, B], eigvalsh(A, B))

    rng = numpy.random.RandomState(utt.fetch_seed())
    a = rng.randn(5, 5)
    a = a + a.T
    for b in [10 * numpy.eye(5, 5) + rng.randn(5, 5)]:
        w = f(a, b)
        refw = scipy.linalg.eigvalsh(a, b)
        numpy.testing.assert_array_almost_equal(w, refw)

    # We need to test None separatly, as otherwise DebugMode will
    # complain, as this isn't a valid ndarray.
    b = None
    B = theano.tensor.NoneConst
    f = function([A], eigvalsh(A, B))
    w = f(a)
    refw = scipy.linalg.eigvalsh(a, b)
    numpy.testing.assert_array_almost_equal(w, refw)
def test_eigvalsh_grad():
    if not imported_scipy:
        raise SkipTest("Scipy needed for the geigvalsh op.")
    import scipy.linalg

    rng = numpy.random.RandomState(utt.fetch_seed())
    a = rng.randn(5, 5)
    a = a + a.T
    b = 10 * numpy.eye(5, 5) + rng.randn(5, 5)
    tensor.verify_grad(lambda a, b: eigvalsh(a, b).dot([1, 2, 3, 4, 5]), [a, b], rng=numpy.random)
def test_eigvalsh_grad():
    pytest.importorskip("scipy")

    rng = np.random.RandomState(utt.fetch_seed())
    a = rng.randn(5, 5)
    a = a + a.T
    b = 10 * np.eye(5, 5) + rng.randn(5, 5)
    tensor.verify_grad(
        lambda a, b: eigvalsh(a, b).dot([1, 2, 3, 4, 5]), [a, b], rng=np.random
    )
Exemple #5
0
def test_eigvalsh_grad():
    if not imported_scipy:
        raise SkipTest("Scipy needed for the geigvalsh op.")
    import scipy.linalg

    rng = numpy.random.RandomState(utt.fetch_seed())
    a = rng.randn(5, 5)
    a = a + a.T
    b = 10 * numpy.eye(5, 5) + rng.randn(5, 5)
    tensor.verify_grad(lambda a, b: eigvalsh(a, b).dot([1, 2, 3, 4, 5]),
                       [a, b], rng=numpy.random)
Exemple #6
0
def th_MvLDAN_cost(data, labels):
    Sw, Sb, n_components = th_MvLDAN_Sw_Sb(data, labels)

    from theano.tensor import slinalg
    evals = slinalg.eigvalsh(Sb, Sw)

    # n_components = data[0].shape[1] - back_step
    top_evals = evals[-n_components:]
    # top_evals = evals[(evals > 0.).nonzero()]
    thresh = T.min(top_evals) + config.threshold
    cost = -T.mean(top_evals[(top_evals <= thresh).nonzero()])
    return cost
Exemple #7
0
def objective(Xt, yt):
    """
    DeepLDA optimization target
    """

    # init groups
    groups = T.arange(0, n_classes)

    def compute_cov(group, Xt, yt):
        """
        Compute class covariance matrix for group
        """
        Xgt = Xt[T.eq(yt, group).nonzero()]
        Xgt_bar = Xgt - T.mean(Xgt, axis=0)
        m = T.cast(Xgt_bar.shape[0], 'float32')
        return (1.0 / (m - 1)) * T.dot(Xgt_bar.T, Xgt_bar)

    # scan over groups
    covs_t, updates = theano.scan(fn=compute_cov,
                                  outputs_info=None,
                                  sequences=[groups],
                                  non_sequences=[Xt, yt])

    # compute average covariance matrix (within scatter)
    Sw_t = T.mean(covs_t, axis=0)

    # compute total scatter
    Xt_bar = Xt - T.mean(Xt, axis=0)
    m = T.cast(Xt_bar.shape[0], 'float32')
    St_t = (1.0 / (m - 1)) * T.dot(Xt_bar.T, Xt_bar)

    # compute between scatter
    Sb_t = St_t - Sw_t

    # cope for numerical instability (regularize)
    Sw_t += T.identity_like(Sw_t) * r

    # compute eigenvalues
    evals_t = slinalg.eigvalsh(Sb_t, Sw_t)

    # get eigenvalues
    top_k_evals = evals_t[-n_components:]

    # maximize variance between classes
    # (k smallest eigenvalues below threshold)
    thresh = T.min(top_k_evals) + 1.0
    top_k_evals = top_k_evals[(top_k_evals <= thresh).nonzero()]
    costs = -T.mean(top_k_evals)

    return costs
def test_eigvalsh():
    scipy = pytest.importorskip("scipy")

    A = theano.tensor.dmatrix("a")
    B = theano.tensor.dmatrix("b")
    f = function([A, B], eigvalsh(A, B))

    rng = np.random.RandomState(utt.fetch_seed())
    a = rng.randn(5, 5)
    a = a + a.T
    for b in [10 * np.eye(5, 5) + rng.randn(5, 5)]:
        w = f(a, b)
        refw = scipy.linalg.eigvalsh(a, b)
        np.testing.assert_array_almost_equal(w, refw)

    # We need to test None separatly, as otherwise DebugMode will
    # complain, as this isn't a valid ndarray.
    b = None
    B = theano.tensor.NoneConst
    f = function([A], eigvalsh(A, B))
    w = f(a)
    refw = scipy.linalg.eigvalsh(a, b)
    np.testing.assert_array_almost_equal(w, refw)
Exemple #9
0
def objective(Xt, yt):
    """
    DeepLDA optimization target
    """

    # init groups
    groups = T.arange(0, n_classes)

    def compute_cov(group, Xt, yt):
        """
        Compute class covariance matrix for group
        """
        Xgt = Xt[T.eq(yt, group).nonzero()]
        Xgt_bar = Xgt - T.mean(Xgt, axis=0)
        m = T.cast(Xgt_bar.shape[0], 'float32')
        return (1.0 / (m - 1)) * T.dot(Xgt_bar.T, Xgt_bar)

    # scan over groups
    covs_t, updates = theano.scan(fn=compute_cov, outputs_info=None,
                                  sequences=[groups], non_sequences=[Xt, yt])

    # compute average covariance matrix (within scatter)
    Sw_t = T.mean(covs_t, axis=0)

    # compute total scatter
    Xt_bar = Xt - T.mean(Xt, axis=0)
    m = T.cast(Xt_bar.shape[0], 'float32')
    St_t = (1.0 / (m - 1)) * T.dot(Xt_bar.T, Xt_bar)

    # compute between scatter
    Sb_t = St_t - Sw_t

    # cope for numerical instability (regularize)
    Sw_t += T.identity_like(Sw_t) * r

    # compute eigenvalues
    evals_t = slinalg.eigvalsh(Sb_t, Sw_t)

    # get eigenvalues
    top_k_evals = evals_t[-n_components:]

    # maximize variance between classes
    # (k smallest eigenvalues below threshold)
    thresh = T.min(top_k_evals) + 1.0
    top_k_evals = top_k_evals[(top_k_evals <= thresh).nonzero()]
    costs = -T.mean(top_k_evals)

    return costs
Exemple #10
0
    def inner_mcca_objective(y_true, y_pred):
        D = y_pred.shape[1] // N
        modality_range = [(D * i, (i + 1) * D) for i in range(N)]
        #X = np.dstack((X_[:, i:j] for i,j in modality_range))
        m = y_pred.shape[0]
        #X = y_pred.T

        Xbar = y_pred.T - (1.0 / m) * T.dot(y_pred.T, T.ones([m, m]))
        X_Rw = (1.0 / (m - 1)) * T.dot(Xbar, Xbar.T)
        Rw_ = T.zeros([D, D])
        Xsum = T.zeros([D, m])
        for i, j in modality_range:
            Rw_ = Rw_ + X_Rw[i:j, i:j]
            Xsum = Xsum + y_pred.T[i:j, :]
        Xmean = Xsum / N
        # total cov
        Xmean_bar = Xmean - (1.0 / m) * T.dot(Xmean, T.ones([m, m]))
        Rt_ = ((N * N * 1.0) / (m - 1)) * T.dot(Xmean_bar, Xmean_bar.T)
        Rb_ = (Rt_ - Rw_) / (N - 1)

        # -- just simple regularization: Rw_ = Rw_ + r1 * T.eye(D)
        # shrinkage regularize - gamma
        Rw_reg_ = ((1 - gamma) * Rw_) + (gamma *
                                         (Rw_.diagonal().mean()) * T.eye(D))
        ISC_ = eigvalsh(Rb_, Rw_reg_)

        l = T.nlinalg.eigh(Rw_reg_, 'L')

        # do Cholesky to do Generalized Eig Problem
        L = T.slinalg.cholesky(Rw_reg_)
        C_ = T.dot(T.nlinalg.matrix_inverse(L), Rb_)
        C = T.dot(C_, T.nlinalg.matrix_inverse(L).T)
        C_eigval, C_eigvec = T.nlinalg.eig(C)
        indx_ = T.argsort(C_eigval)[::-1]
        W_ = T.dot(T.nlinalg.matrix_inverse(L).T, C_eigvec)[:, indx_]
        d_ = T.diag(1.0 / T.sqrt((W_ * W_).sum(axis=0)))
        W_ = T.dot(W_, d_)
        # recompute ISC
        ISC = T.diag(T.dot(T.dot(W_.T, Rb_), W_)) / T.diag(
            T.dot(T.dot(W_.T, Rw_), W_))
        corr = T.sqrt(T.sum(ISC * ISC))

        return -1 * ISC[0]  #-corr