def standard_kernel_tests(k, shapes=None, dtype=np.float64): if shapes is None: shapes = [((10, 2), (5, 2)), ((10, 1), (5, 1)), ((10,), (5,)), ((10,), ()), ((), (5,)), ((), ())] # Check various shapes of arguments. for shape1, shape2 in shapes: x1 = B.randn(dtype, *shape1) x2 = B.randn(dtype, *shape2) # Check that the kernel computes consistently. allclose(k(x1, x2), reversed(k)(x2, x1).T) # Check `elwise`. x2 = B.randn(dtype, *shape1) allclose(k.elwise(x1, x2)[:, 0], B.diag(k(x1, x2))) allclose(k.elwise(x1, x2), Kernel.elwise(k, x1, x2)) # The element-wise computation is more accurate, which is why we allow # a discrepancy a bit larger than the square root of the machine # epsilon. allclose(k.elwise(x1)[:, 0], B.diag(k(x1)), desc='', atol=1e-6, rtol=1e-6) allclose(k.elwise(x1), Kernel.elwise(k, x1))
def kernel_generator(k): # Check that the kernel computes. x1 = np.random.randn(10, 2) x2 = np.random.randn(5, 2) yield assert_allclose, k(x1, x2), k(x2, x1).T # Check `elwise`. x1 = np.random.randn(10, 2) x2 = np.random.randn(10, 2) yield assert_allclose, k.elwise(x1, x2)[:, 0], B.diag(k(x1, x2)) yield assert_allclose, k.elwise(x1, x2), Kernel.elwise(k, x1, x2) yield assert_allclose, k.elwise(x1)[:, 0], B.diag(k(x1)) yield assert_allclose, k.elwise(x1)[:, 0], B.diag(k(x1)) yield assert_allclose, k.elwise(x1), Kernel.elwise(k, x1)
def test_corner_cases(): with pytest.raises(RuntimeError): Kernel()(1.)
def test_corner_cases(): yield raises, RuntimeError, lambda: Kernel()(1.)