예제 #1
0
def test_kernel_cache_add_remove():
    kernel = RbfKernel(gamma=0.1)
    kcache = KernelCache(kernel, 20, capacity, 0, 0)

    for i in xrange(3):
        kcache.add_sv(i)

    assert_equal(kcache.n_sv(), 3)

    kcache.remove_sv(1)
    assert_equal(kcache.n_sv(), 2)
예제 #2
0
def test_kernel_cache_column():
    K = pairwise_kernels(X, metric="rbf", gamma=0.1)
    kernel = RbfKernel(gamma=0.1)
    kcache = KernelCache(kernel, 20, capacity, 0, 0)
    out = np.zeros(20, dtype=np.float64)

    # Compute a first column.
    kcache.compute_column(X, X, 12, out)
    assert_array_almost_equal(K[:, 12], out)
    assert_equal(kcache.get_size(), 160)

    # Check that the works.
    kcache.compute_column(X2, X2, 12, out)
    assert_array_almost_equal(K[:, 12], out)
    assert_equal(kcache.get_size(), 160)

    # Compute more columns.
    kcache.compute_column(X, X, 13, out)
    assert_array_almost_equal(K[:, 13], out)
    assert_equal(kcache.get_size(), 320)

    kcache.compute_column(X, X, 14, out)
    assert_array_almost_equal(K[:, 14], out)
    assert_equal(kcache.get_size(), 480)

    kcache.compute_column(X, X, 15, out)
    assert_array_almost_equal(K[:, 15], out)
    assert_equal(kcache.get_size(), 640)

    # Maximum size reached.
    kcache.compute_column(X, X, 16, out)
    assert_array_almost_equal(K[:, 16], out)
    assert_equal(kcache.get_size(), 480)

    # Check that cache works.
    kcache.compute_column(X2, X2, 16, out)
    assert_array_almost_equal(K[:, 16], out)
    assert_equal(kcache.get_size(), 480)
예제 #3
0
def test_kernel_cache_compute():
    kernel = RbfKernel(gamma=0.1)
    kcache = KernelCache(kernel, 20, capacity, 0, 0)
    assert_equal(kcache.compute(X, 0, X, 0), 1.0)
예제 #4
0
def test_kernel_cache_column_sv():
    K = pairwise_kernels(X, metric="rbf", gamma=0.1)
    kernel = RbfKernel(gamma=0.1)
    kcache = KernelCache(kernel, 20, capacity, 0, 0)
    out = np.zeros(20, dtype=np.float64)

    size = 0

    # Add 3 SVs.
    kcache.add_sv(6)
    kcache.add_sv(12)
    kcache.add_sv(3)
    # That's a limitation of the current implementation:
    # it allocates a full column.
    size += 20 * 8

    # Compute values.
    kcache.compute_column_sv(X, X, 7, out)
    assert_almost_equal(K[6, 7], out[6])
    assert_almost_equal(K[12, 7], out[12])
    assert_almost_equal(K[3, 7], out[3])
    assert_equal(size, kcache.get_size())

    # Check that the works.
    kcache.compute_column_sv(X2, X2, 7, out)
    assert_almost_equal(K[6, 7], out[6])
    assert_almost_equal(K[12, 7], out[12])
    assert_almost_equal(K[3, 7], out[3])
    assert_equal(size, kcache.get_size())

    # Add one more SV.
    kcache.add_sv(17)

    # Compute values.
    kcache.compute_column_sv(X, X, 7, out)
    assert_almost_equal(K[6, 7], out[6])
    assert_almost_equal(K[12, 7], out[12])
    assert_almost_equal(K[3, 7], out[3])
    assert_almost_equal(K[17, 7], out[17])
    assert_equal(size, kcache.get_size())

    # Check that the works.
    kcache.compute_column_sv(X2, X2, 7, out)
    assert_almost_equal(K[6, 7], out[6])
    assert_almost_equal(K[12, 7], out[12])
    assert_almost_equal(K[3, 7], out[3])
    assert_almost_equal(K[17, 7], out[17])
    assert_equal(size, kcache.get_size())

    # Compute the entire same column.
    kcache.compute_column(X, X, 7, out)
    assert_array_almost_equal(K[:, 7], out)
    assert_equal(size, kcache.get_size())

    # Compute an entire new column.
    kcache.compute_column(X, X, 8, out)
    size += 20 * 8
    assert_array_almost_equal(K[:, 8], out)
    assert_equal(size, kcache.get_size())

    # Retrieve the SV only.
    out *= 0
    kcache.compute_column_sv(X, X, 8, out)
    assert_almost_equal(K[6, 8], out[6])
    assert_almost_equal(K[12, 8], out[12])
    assert_almost_equal(K[3, 8], out[3])
    assert_almost_equal(K[17, 8], out[17])
    assert_equal(size, kcache.get_size())

    # Remove SV.
    out *= 0
    kcache.remove_sv(12)
    kcache.compute_column_sv(X, X, 8, out)
    assert_almost_equal(K[6, 8], out[6])
    assert_almost_equal(0, out[12])
    assert_almost_equal(K[3, 8], out[3])
    assert_almost_equal(K[17, 8], out[17])
    assert_equal(size, kcache.get_size())

    for i in (12, 6, 3):
        kcache.remove_sv(i)

    assert_equal(kcache.n_sv(), 1)

    # Add back some new SV.
    kcache.add_sv(15)
    assert_equal(kcache.n_sv(), 2)

    out *= 0
    kcache.compute_column_sv(X, X, 8, out)
    assert_almost_equal(0, out[6])
    assert_almost_equal(0, out[12])
    assert_almost_equal(0, out[3])
    assert_almost_equal(K[15, 8], out[15])
    assert_almost_equal(K[17, 8], out[17])
    assert_equal(size, kcache.get_size())

    # Remove non-existing column.
    kcache.remove_column(19)
    assert_equal(size, kcache.get_size())

    # Remove existing column.
    kcache.remove_column(8)
    size -= 20 * 8
    assert_equal(size, kcache.get_size())