def test_lab_cache(): c = Cache() x1, x2 = np.random.randn(10, 10), np.random.randn(10, 10) x2 = np.random.randn(10, 10) yield eq, id(c.pw_dists(x1, x2)), id(c.pw_dists(x1, x2)) yield neq, id(c.pw_dists(x1, x1)), id(c.pw_dists(x1, x2)) yield eq, id(c.matmul(x1, x2, tr_a=True)), id(c.matmul(x1, x2, tr_a=True)) yield neq, id(c.matmul(x1, x2, tr_a=True)), id(c.matmul(x1, x2))
def test_construction(): k = EQ() x = np.random.randn(10, 1) c = Cache() yield k, x yield k, x, c yield k, x, x yield k, x, x, c yield k, Observed(x) yield k, Observed(x), c yield k, Observed(x), Observed(x) yield k, Observed(x), Observed(x), c yield k, x, Observed(x) yield k, x, Observed(x), c yield k, Observed(x), x yield k, Observed(x), x, c yield k.elwise, x yield k.elwise, x, c yield k.elwise, x, x yield k.elwise, x, x, c yield k.elwise, Observed(x) yield k.elwise, Observed(x), c yield k.elwise, Observed(x), Observed(x) yield k.elwise, Observed(x), Observed(x), c yield k.elwise, x, Observed(x) yield k.elwise, x, Observed(x), c yield k.elwise, Observed(x), x yield k.elwise, Observed(x), x, c
def test_construction(): model = Graph() p = GP(EQ(), graph=model) x = np.random.randn(10, 1) c = Cache() yield p.mean, x yield p.mean, p(x) yield p.mean, x, c yield p.mean, p(x), c yield p.kernel, x yield p.kernel, p(x) yield p.kernel, x, c yield p.kernel, p(x), c yield p.kernel, x, x yield p.kernel, p(x), x yield p.kernel, x, p(x) yield p.kernel, p(x), p(x) yield p.kernel, x, x, c yield p.kernel, p(x), x, c yield p.kernel, x, p(x), c yield p.kernel, p(x), p(x), c yield p.kernel.elwise, x yield p.kernel.elwise, p(x) yield p.kernel.elwise, x, c yield p.kernel.elwise, p(x), c yield p.kernel.elwise, x, x yield p.kernel.elwise, p(x), x yield p.kernel.elwise, x, p(x) yield p.kernel.elwise, p(x), p(x) yield p.kernel.elwise, x, x, c yield p.kernel.elwise, p(x), x, c yield p.kernel.elwise, x, p(x), c yield p.kernel.elwise, p(x), p(x), c # Test resolution of kernel and mean. k = EQ() m = TensorProductMean(lambda x: x ** 2) yield assert_instance, GP(k, graph=model).mean, ZeroMean yield assert_instance, GP(k, 5, graph=model).mean, ScaledMean yield assert_instance, GP(k, 1, graph=model).mean, OneMean yield assert_instance, GP(k, 0, graph=model).mean, ZeroMean yield assert_instance, GP(k, m, graph=model).mean, TensorProductMean yield assert_instance, GP(k, graph=model).kernel, EQ yield assert_instance, GP(5, graph=model).kernel, ScaledKernel yield assert_instance, GP(1, graph=model).kernel, OneKernel yield assert_instance, GP(0, graph=model).kernel, ZeroKernel # Test construction of finite-dimensional distribution. d = GP(k, m, graph=model)(x) yield assert_allclose, d.var, k(x) yield assert_allclose, d.mean, m(x)
def test_construction(): m = TensorProductMean(lambda x: x**2) x = np.random.randn(10, 1) c = Cache() yield m, x yield m, x, c yield m, Observed(x) yield m, Observed(x), c
def test_cache_performance(): c = Cache() k1 = EQ() k2 = EQ() x = np.linspace(0, 1, 2000) dur1, y1 = benchmark(k1, (x, c), n=1, get_output=True) dur2, y2 = benchmark(k1, (x, c), n=1, get_output=True) dur3, y3 = benchmark(k2, (x, c), n=1, get_output=True) yield assert_allclose, y1, y2 yield assert_allclose, y1, y3 # Test performance of call cache. yield le, dur2, dur1 / 500 # Test performance of LAB cache. yield le, dur3, dur1 / 20
def test_ones_zeros(): c = Cache() # Nothing to check for kernels: ones and zeros are represented in a # structured way. # Test that ones and zeros are cached and that all signatures work. m = ZeroMean() yield eq, id(m(np.random.randn(10, 10), c)), \ id(m(np.random.randn(10, 10), c)) yield neq, id(m(np.random.randn(10, 10), c)), \ id(m(np.random.randn(5, 10), c)) yield eq, id(m(1, c)), id(m(1, c)) m = OneMean() yield eq, id(m(np.random.randn(10, 10), c)), \ id(m(np.random.randn(10, 10), c)) yield neq, id(m(np.random.randn(10, 10), c)), \ id(m(np.random.randn(5, 10), c)) yield eq, id(m(1, c)), id(m(1, c))
def test_elwise_cache_aliasing(): c = Cache() k = EQ() x1 = np.random.randn(10, 2) x2 = np.random.randn(10, 2) yield assert_allclose, k.elwise(x1, x2, c), Kernel.elwise(k, x1, x2, c)