def test_cached_kernel(self): nchunks = 5 n = 50 * nchunks d = Dataset(np.random.randn(n, 132)) d.sa.chunks = np.random.randint(nchunks, size=n) # We'll compare against an Rbf just because it has a parameter to change rk = npK.RbfKernel(sigma=1.5) # Assure two kernels are independent for this test ck = CachedKernel(kernel=npK.RbfKernel(sigma=1.5)) ck.compute(d) # Initial cache of all data self.assertTrue(ck._recomputed, 'CachedKernel was not initially computed') # Try some splitting for chunk in [d[d.sa.chunks == i] for i in range(nchunks)]: rk.compute(chunk) ck.compute(chunk) self.kernel_equiv(rk, ck) #, accuracy=1e-12) self.failIf(ck._recomputed, "CachedKernel incorrectly recomputed it's kernel") # Test what happens when a parameter changes ck.params.sigma = 3.5 ck.compute(d) self.assertTrue(ck._recomputed, "CachedKernel doesn't recompute on kernel change") rk.params.sigma = 3.5 rk.compute(d) self.assertTrue(np.all(rk._k == ck._k), 'Cached and rbf kernels disagree after kernel change') # Now test handling new data d2 = Dataset(np.random.randn(32, 43)) ck.compute(d2) self.assertTrue( ck._recomputed, "CachedKernel did not automatically recompute new data") ck.compute(d) self.assertTrue(ck._recomputed, "CachedKernel did not recompute old data which had\n" + \ "previously been computed, but had the cache overriden")
def test_rbf_sg(self): d1 = np.random.randn(105, 32) d2 = np.random.randn(41, 32) sk = sgK.RbfSGKernel() nk = npK.RbfKernel() sigmavals = np.logspace(-2, 5, num=10) for s in sigmavals: sk.params.sigma = s nk.params.sigma = s sk.compute(d1, d2) nk.compute(d1, d2) self.kernel_equiv(nk, sk)