Esempio n. 1
0
def test_callback():
    n_samples, n_features, n_dims = 10, 5, 3
    X = [rng_global.randn(n_features, n_dims) for i in range(n_samples)]
    n_kernels = 8

    def my_callback(loc):
        _ = loc["dict_obj"]

    dico = MultivariateDictLearning(
        n_kernels=n_kernels,
        random_state=0,
        max_iter=2,
        n_nonzero_coefs=1,
        callback=my_callback,
    )
    code = dico.fit(X).transform(X[0])
    assert len(code[0]) <= 1
    dico = MiniBatchMultivariateDictLearning(
        n_kernels=n_kernels,
        random_state=0,
        n_iter=2,
        n_nonzero_coefs=1,
        callback=my_callback,
    )
    code = dico.fit(X).transform(X[0])
    assert len(code[0]) <= 1
Esempio n. 2
0
def test_dict_init():
    n_samples, n_features, n_dims = 10, 5, 3
    X = [rng_global.randn(n_features, n_dims) for i in range(n_samples)]
    n_kernels = 8
    d = [rng_global.randn(n_features, n_dims) for i in range(n_kernels)]
    for i in range(len(d)):
        d[i] /= np.linalg.norm(d[i], "fro")
    dico = MultivariateDictLearning(
        n_kernels=n_kernels,
        random_state=0,
        max_iter=1,
        n_nonzero_coefs=1,
        learning_rate=0.0,
        dict_init=d,
        verbose=5,
    ).fit(X)
    dico = dico.fit(X)
    for i in range(n_kernels):
        assert_array_almost_equal(dico.kernels_[i], d[i])
    # code = dico.fit(X).transform(X[0])
    # assert (len(code[0]) > 1)

    dico = MiniBatchMultivariateDictLearning(
        n_kernels=n_kernels,
        random_state=0,
        n_iter=1,
        n_nonzero_coefs=1,
        dict_init=d,
        verbose=1,
        learning_rate=0.0,
    ).fit(X)
    dico = dico.fit(X)
    for i in range(n_kernels):
        assert_array_almost_equal(dico.kernels_[i], d[i])
Esempio n. 3
0
def test_mdla_dict_update():
    n_kernels = 10
    # n_samples, n_features, n_dims = 100, 5, 3
    n_samples, n_features, n_dims = 80, 5, 3
    X = [rng_global.randn(n_features, n_dims) for i in range(n_samples)]
    dico = MultivariateDictLearning(
        n_kernels=n_kernels, random_state=0, max_iter=10, n_jobs=-1
    ).fit(X)
    first_epoch = list(dico.kernels_)
    dico = dico.fit(X)
    second_epoch = list(dico.kernels_)
    for k, c in zip(first_epoch, second_epoch):
        assert (k - c).sum() != 0.0

    dico = MiniBatchMultivariateDictLearning(
        n_kernels=n_kernels, random_state=0, n_iter=10, n_jobs=-1
    ).fit(X)
    first_epoch = list(dico.kernels_)
    dico = dico.fit(X)
    second_epoch = list(dico.kernels_)
    for k, c in zip(first_epoch, second_epoch):
        assert (k - c).sum() != 0.0

    dico = MiniBatchMultivariateDictLearning(
        n_kernels=n_kernels, random_state=0, n_iter=10, n_jobs=-1
    ).partial_fit(X)
    first_epoch = list(dico.kernels_)
    dico = dico.partial_fit(X)
    second_epoch = list(dico.kernels_)
    for k, c in zip(first_epoch, second_epoch):
        assert (k - c).sum() != 0.0
Esempio n. 4
0
def test_mdla_dict_update():
    n_kernels = 10
    # n_samples, n_features, n_dims = 100, 5, 3
    n_samples, n_features, n_dims = 80, 5, 3
    X = [rng_global.randn(n_features, n_dims) for i in range(n_samples)]
    dico = MultivariateDictLearning(n_kernels=n_kernels, random_state=0,
                                    max_iter=10, n_jobs=-1).fit(X)
    first_epoch = list(dico.kernels_)
    dico = dico.fit(X)
    second_epoch = list(dico.kernels_)
    for k, c in zip(first_epoch, second_epoch):
        assert_true((k-c).sum() != 0.)

    dico = MiniBatchMultivariateDictLearning(n_kernels=n_kernels,
                random_state=0, n_iter=10, n_jobs=-1).fit(X)
    first_epoch = list(dico.kernels_)
    dico = dico.fit(X)
    second_epoch = list(dico.kernels_)
    for k, c in zip(first_epoch, second_epoch):
        assert_true((k-c).sum() != 0.)

    dico = MiniBatchMultivariateDictLearning(n_kernels=n_kernels,
                random_state=0, n_iter=10, n_jobs=-1).partial_fit(X)
    first_epoch = list(dico.kernels_)
    dico = dico.partial_fit(X)
    second_epoch = list(dico.kernels_)
    for k, c in zip(first_epoch, second_epoch):
        assert_true((k-c).sum() != 0.)
Esempio n. 5
0
def test_mdla_nonzero_coefs():
    n_kernels = 8
    dico = MultivariateDictLearning(n_kernels=n_kernels, random_state=0,
                                max_iter=3, n_nonzero_coefs=3, verbose=5)
    code = dico.fit(X).transform(X[0])
    assert_true(len(code[0]) <= 3)

    dico = MiniBatchMultivariateDictLearning(n_kernels=n_kernels,
                    random_state=0, n_iter=3, n_nonzero_coefs=3, verbose=5)
    code = dico.fit(X).transform(X[0])
    assert_true(len(code[0]) <= 3)
Esempio n. 6
0
def test_X_array():
    n_kernels = 8
    X = rng_global.randn(n_samples, n_features, n_dims)
    dico = MultivariateDictLearning(n_kernels=n_kernels, random_state=0,
                                max_iter=3, n_nonzero_coefs=3, verbose=5)
    code = dico.fit(X).transform(X[0])
    assert_true(len(code[0]) <= 3)
    
    dico = MiniBatchMultivariateDictLearning(n_kernels=n_kernels,
                    random_state=0, n_iter=3, n_nonzero_coefs=3, verbose=5)
    code = dico.fit(X).transform(X[0])
    assert_true(len(code[0]) <= 3)
Esempio n. 7
0
def test_X_array():
    n_samples, n_features, n_dims = 10, 5, 3
    n_kernels = 8
    X = rng_global.randn(n_samples, n_features, n_dims)
    dico = MultivariateDictLearning(
        n_kernels=n_kernels, random_state=0, max_iter=3, n_nonzero_coefs=3, verbose=5
    )
    code = dico.fit(X).transform(X[0])
    assert len(code[0]) <= 3

    dico = MiniBatchMultivariateDictLearning(
        n_kernels=n_kernels, random_state=0, n_iter=3, n_nonzero_coefs=3, verbose=5
    )
    code = dico.fit(X).transform(X[0])
    assert len(code[0]) <= 3
Esempio n. 8
0
def test_callback():
    n_kernels = 8
    def my_callback(loc):
        d = loc['dict_obj']
        
    dico = MultivariateDictLearning(n_kernels=n_kernels, random_state=0,
                                    max_iter=2, n_nonzero_coefs=1,
                                    callback=my_callback)
    code = dico.fit(X).transform(X[0])
    assert_true(len(code[0]) <= 1)
    dico = MiniBatchMultivariateDictLearning(n_kernels=n_kernels,
                random_state=0, n_iter=2, n_nonzero_coefs=1,
                callback=my_callback)
    code = dico.fit(X).transform(X[0])
    assert_true(len(code[0]) <= 1)
Esempio n. 9
0
def test_sparse_encode():
    n_kernels = 8
    dico = MultivariateDictLearning(n_kernels=n_kernels, random_state=0,
                                    max_iter=2, n_nonzero_coefs=1)
    dico = dico.fit(X)
    _, code = multivariate_sparse_encode(X, dico, n_nonzero_coefs=1,
                                        n_jobs=-1, verbose=3)
    assert_true(len(code[0]) <= 1)
Esempio n. 10
0
def test_sparse_encode():
    n_samples, n_features, n_dims = 10, 5, 3
    X = [rng_global.randn(n_features, n_dims) for i in range(n_samples)]
    n_kernels = 8
    dico = MultivariateDictLearning(
        n_kernels=n_kernels, random_state=0, max_iter=2, n_nonzero_coefs=1
    )
    dico = dico.fit(X)
    _, code = multivariate_sparse_encode(X, dico, n_nonzero_coefs=1, n_jobs=-1, verbose=3)
    assert len(code[0]) <= 1
Esempio n. 11
0
def test_mdla_nonzero_coefs():
    n_samples, n_features, n_dims = 10, 5, 3
    X = [rng_global.randn(n_features, n_dims) for i in range(n_samples)]
    n_kernels = 8
    dico = MultivariateDictLearning(n_kernels=n_kernels,
                                    random_state=0,
                                    max_iter=3,
                                    n_nonzero_coefs=3,
                                    verbose=5)
    code = dico.fit(X).transform(X[0])
    assert_true(len(code[0]) <= 3)

    dico = MiniBatchMultivariateDictLearning(n_kernels=n_kernels,
                                             random_state=0,
                                             n_iter=3,
                                             n_nonzero_coefs=3,
                                             verbose=5)
    code = dico.fit(X).transform(X[0])
    assert_true(len(code[0]) <= 3)
Esempio n. 12
0
def test_dict_init():
    n_kernels = 8
    d = [rng_global.randn(n_features, n_dims) for i in range(n_kernels)]
    for i in range(len(d)):
        d[i] /= np.linalg.norm(d[i], 'fro')
    dico = MultivariateDictLearning(n_kernels=n_kernels, random_state=0,
                                    max_iter=1, n_nonzero_coefs=1, learning_rate=0.,
                                    dict_init=d, verbose=5).fit(X)
    dico = dico.fit(X)
    for i in range(n_kernels):
        assert_array_almost_equal(dico.kernels_[i], d[i])
    # code = dico.fit(X).transform(X[0])
    # assert_true(len(code[0]) > 1)
    
    dico = MiniBatchMultivariateDictLearning(n_kernels=n_kernels,
                random_state=0, n_iter=1, n_nonzero_coefs=1,
                dict_init=d, verbose=1, learning_rate=0.).fit(X)
    dico = dico.fit(X)
    for i in range(n_kernels):
        assert_array_almost_equal(dico.kernels_[i], d[i])