def testForLaplacianKernel(self): kernel = utils._get_kernel('laplace') self.assertIsInstance(kernel, laplacian_kernel.LaplacianKernel) kernel = utils._get_kernel('laplace_Kernel') self.assertIsInstance(kernel, laplacian_kernel.LaplacianKernel) kernel = utils._get_kernel('laplacian') self.assertIsInstance(kernel, laplacian_kernel.LaplacianKernel) kernel = utils._get_kernel('laplacian_kernel') self.assertIsInstance(kernel, laplacian_kernel.LaplacianKernel) kernel = utils._get_kernel(laplacian_kernel.LaplacianKernel()) self.assertIsInstance(kernel, laplacian_kernel.LaplacianKernel) kernel_length = 3 kernel = utils._get_kernel(laplacian_kernel.LaplacianKernel(kernel_length)) self.assertIsInstance(kernel, laplacian_kernel.LaplacianKernel) self.assertEqual(kernel.kernel_length, kernel_length)
def testLaplacianKernel(self): # Under certain conditions, the kernel output resembles the identity # (within the tolerance of the checks). # Specifically, this is true when the minimum distance, d, between any two # elements in the input is greater than D for D = -ln(tol) * kernel_length # where: # - tol: assertion tolerance (these tests ues 1e-6) # - d: minimum{norm(x_i - y_i) # for x_i = x[i, :], y_j = y[j, :] for all i neq j} # - kernel_length: argument to kernel initialization # # In the test below, we use max(kernel_length) == 0.1 which gives us # max(D)~=1.38. Given this bound, we sometines use d=2 (meaning that each # element is at least 2 greater or smaller than every other element in the # input). When we do this, we expect the output to be close to the identity. for kernel_length in [0.05, 0.075, 0.1]: laplacian_kernel_fn = laplacian_kernel.LaplacianKernel( kernel_length) kernel_val = laplacian_kernel_fn(tf.constant([[2.0], [0.0]])) self.assertAllClose(kernel_val, tf.eye(2)) kernel_val = laplacian_kernel_fn(tf.constant([[1.0], [8.0], [3.0]])) self.assertAllClose(kernel_val, tf.eye(3)) kernel_val = laplacian_kernel_fn( tf.constant([[1.0, 3.0], [5.0, 7.0], [9.0, 11.0]])) self.assertAllClose(kernel_val, tf.eye(3)) # Vector where all elements are equal should result in matrix of all 1s. for val in [0.2, 1.0, 2.79]: # Arbitrary values. kernel_val = laplacian_kernel_fn( tf.constant([[val, val], [val, val]])) # self.assertAllClose(kernel_val, tf.eye(3)) self.assertAllClose(kernel_val, [[1.0, 1.0], [1.0, 1.0]]) # If the two tensors are equal, it should be the same as a single tensor. # Note that we pick d < 1 so that the result is not the identity. tensor = tf.constant([[1.0], [1.01]]) single_kernel_val = laplacian_kernel_fn(tensor) # Assert that the result is nontrivial (not the identity). self.assertNotAllClose(tf.eye(2), single_kernel_val) double_kernel_val = laplacian_kernel_fn(tensor, tensor) self.assertAllClose(single_kernel_val, double_kernel_val) self.assertIsNot(single_kernel_val, double_kernel_val) # If the delta is the same, then the result should be the same. # Note that we pick d < 1 so that the result is not the identity. tensor1 = tf.constant([[1.0], [7.0]]) tensor2 = tf.constant([[1.0], [5.3]]) delta = tf.constant([[0.0], [0.2]]) kernel_val1 = laplacian_kernel_fn(tensor1, tensor1 + delta) # Assert that the result is nontrivial (not the identity). self.assertNotAllClose(tf.eye(2), kernel_val1) kernel_val2 = laplacian_kernel_fn(tensor2, tensor2 + delta) self.assertAllClose(kernel_val1, kernel_val2)
def testGetAndFromConfig(self): kernel_length = 5 # Arbitrary value. kernel = laplacian_kernel.LaplacianKernel(kernel_length) kernel_from_config = laplacian_kernel.LaplacianKernel.from_config( kernel.get_config()) self.assertIsInstance(kernel_from_config, laplacian_kernel.LaplacianKernel) self.assertEqual(kernel_from_config.kernel_length, kernel_length)
def testSerialization(self): kernel_length = 5 # Arbitrary value. kernel = laplacian_kernel.LaplacianKernel(kernel_length) serialized_kernel = tf.keras.utils.serialize_keras_object(kernel) deserialized_kernel = tf.keras.utils.deserialize_keras_object( serialized_kernel) self.assertIsInstance(deserialized_kernel, laplacian_kernel.LaplacianKernel) self.assertEqual(deserialized_kernel.kernel_length, kernel_length)
def testkernelAttributes(self): loss = CustomLoss(membership_kernel='gauss', predictions_kernel='laplace') self.assertIsInstance(loss.membership_kernel, gaussian_kernel.GaussianKernel) self.assertIsInstance(loss.predictions_kernel, laplacian_kernel.LaplacianKernel) kernel = gaussian_kernel.GaussianKernel() loss = CustomLoss(predictions_kernel=kernel) self.assertIs(loss.predictions_kernel, kernel) self.assertIsNone(loss.membership_kernel) kernel = laplacian_kernel.LaplacianKernel() loss = CustomLoss(membership_kernel=kernel) self.assertIs(loss.membership_kernel, kernel) self.assertIsNone(loss.predictions_kernel)