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), B.transpose(reversed(k)(x2, x1))) # 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 test_derivative_linear(): # Test derivative of kernel `Linear()`. k = Linear() x1 = B.randn(tf.float64, 10, 1) x2 = B.randn(tf.float64, 5, 1) # Test derivative with respect to first input. allclose(k.diff(0, None)(x1, x2), B.ones(tf.float64, 10, 5) * B.transpose(x2)) allclose(k.diff(0, None)(x1), B.ones(tf.float64, 10, 10) * B.transpose(x1)) # Test derivative with respect to second input. allclose(k.diff(None, 0)(x1, x2), B.ones(tf.float64, 10, 5) * x1) allclose(k.diff(None, 0)(x1), B.ones(tf.float64, 10, 10) * x1) # Test derivative with respect to both inputs. ref = B.ones(tf.float64, 10, 5) allclose(k.diff(0, 0)(x1, x2), ref) allclose(k.diff(0)(x1, x2), ref) ref = B.ones(tf.float64, 10, 10) allclose(k.diff(0, 0)(x1), ref) allclose(k.diff(0)(x1), ref)
def test_derivative_eq(): # Test derivative of kernel `EQ()`. k = EQ() x1 = B.randn(tf.float64, 10, 1) x2 = B.randn(tf.float64, 5, 1) # Test derivative with respect to first input. allclose(k.diff(0, None)(x1, x2), -k(x1, x2) * (x1 - B.transpose(x2))) allclose(k.diff(0, None)(x1), -k(x1) * (x1 - B.transpose(x1))) # Test derivative with respect to second input. allclose(k.diff(None, 0)(x1, x2), -k(x1, x2) * (B.transpose(x2) - x1)) allclose(k.diff(None, 0)(x1), -k(x1) * (B.transpose(x1) - x1)) # Test derivative with respect to both inputs. ref = k(x1, x2) * (1 - (x1 - B.transpose(x2)) ** 2) allclose(k.diff(0, 0)(x1, x2), ref) allclose(k.diff(0)(x1, x2), ref) ref = k(x1) * (1 - (x1 - B.transpose(x1)) ** 2) allclose(k.diff(0, 0)(x1), ref) allclose(k.diff(0)(x1), ref)