def test_max_rank():

    X = np.random.standard_normal((100, 200))
    nt.assert_raises(ValueError, FM.compute_iterative_svd, X, max_rank=2)

    U, D, VT = FM.compute_iterative_svd(X)
    nt.assert_true(np.linalg.norm(np.dot(U.T, U) - np.identity(100)) < 1.e-6)
    nt.assert_true(np.linalg.norm(np.dot(VT, VT.T) - np.identity(100)) < 1.e-6)

    U, D, VT = FM.compute_iterative_svd(X, max_rank=200)
    nt.assert_true(np.linalg.norm(np.dot(U.T, U) - np.identity(100)) < 1.e-6)
    nt.assert_true(np.linalg.norm(np.dot(VT, VT.T) - np.identity(100)) < 1.e-6)
Esempio n. 2
0
def test_max_rank():

    X = np.random.standard_normal((100, 200))
    nt.assert_raises(ValueError, FM.compute_iterative_svd, X, max_rank=2)

    U, D, VT = FM.compute_iterative_svd(X)
    nt.assert_true(np.linalg.norm(np.dot(U.T, U) - np.identity(100)) < 1.e-6)
    nt.assert_true(np.linalg.norm(np.dot(VT, VT.T) - np.identity(100)) < 1.e-6)

    U, D, VT = FM.compute_iterative_svd(X, max_rank=200)
    nt.assert_true(np.linalg.norm(np.dot(U.T, U) - np.identity(100)) < 1.e-6)
    nt.assert_true(np.linalg.norm(np.dot(VT, VT.T) - np.identity(100)) < 1.e-6)
def test_stopping_rule():
    '''
    use a stopping rule in compute_iterative_svd
    '''

    def soft_threshold_rule(L):
        return lambda D: np.fabs(D).min() <= L

    L = 30
    svt_rule = soft_threshold_rule(L)

    U, D, VT = FM.compute_iterative_svd(X, initial_rank=3, stopping_rule=svt_rule, tol=1.e-12, debug=True)

    D2 = (D - L) * (D > L)
    D1 = np.linalg.svd(X)[1]
    D1 = (D1 - L) * (D1 > L)
    rank = (D2 > 0).sum()
    all_close(D1[:rank], D2[:rank], 'stopping_rule', None)
def test_stopping_rule():
    '''
    use a stopping rule in compute_iterative_svd
    '''
    def soft_threshold_rule(L):
        return lambda D: np.fabs(D).min() <= L

    L = 30
    svt_rule = soft_threshold_rule(L)

    U, D, VT = FM.compute_iterative_svd(X,
                                        initial_rank=3,
                                        stopping_rule=svt_rule,
                                        tol=1.e-12,
                                        debug=True)

    D2 = (D - L) * (D > L)
    D1 = np.linalg.svd(X)[1]
    D1 = (D1 - L) * (D1 > L)
    rank = (D2 > 0).sum()
    all_close(D1[:rank], D2[:rank], 'stopping_rule', None)