Python utils.gaussian_kernel_matrix() Examples
The following are 30
code examples of utils.gaussian_kernel_matrix().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
utils
, or try the search function
.
Example #1
Source File: losses.py From DOTA_models with Apache License 2.0 | 6 votes |
def mmd_loss(source_samples, target_samples, weight, scope=None): """Adds a similarity loss term, the MMD between two representations. This Maximum Mean Discrepancy (MMD) loss is calculated with a number of different Gaussian kernels. Args: source_samples: a tensor of shape [num_samples, num_features]. target_samples: a tensor of shape [num_samples, num_features]. weight: the weight of the MMD loss. scope: optional name scope for summary tags. Returns: a scalar tensor representing the MMD loss value. """ sigmas = [ 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1, 5, 10, 15, 20, 25, 30, 35, 100, 1e3, 1e4, 1e5, 1e6 ] gaussian_kernel = partial( utils.gaussian_kernel_matrix, sigmas=tf.constant(sigmas)) loss_value = maximum_mean_discrepancy( source_samples, target_samples, kernel=gaussian_kernel) loss_value = tf.maximum(1e-4, loss_value) * weight assert_op = tf.Assert(tf.is_finite(loss_value), [loss_value]) with tf.control_dependencies([assert_op]): tag = 'MMD Loss' if scope: tag = scope + tag tf.summary.scalar(tag, loss_value) tf.losses.add_loss(loss_value) return loss_value
Example #2
Source File: losses.py From DOTA_models with Apache License 2.0 | 6 votes |
def maximum_mean_discrepancy(x, y, kernel=utils.gaussian_kernel_matrix): r"""Computes the Maximum Mean Discrepancy (MMD) of two samples: x and y. Maximum Mean Discrepancy (MMD) is a distance-measure between the samples of the distributions of x and y. Here we use the kernel two sample estimate using the empirical mean of the two distributions. MMD^2(P, Q) = || \E{\phi(x)} - \E{\phi(y)} ||^2 = \E{ K(x, x) } + \E{ K(y, y) } - 2 \E{ K(x, y) }, where K = <\phi(x), \phi(y)>, is the desired kernel function, in this case a radial basis kernel. Args: x: a tensor of shape [num_samples, num_features] y: a tensor of shape [num_samples, num_features] kernel: a function which computes the kernel in MMD. Defaults to the GaussianKernelMatrix. Returns: a scalar denoting the squared maximum mean discrepancy loss. """ with tf.name_scope('MaximumMeanDiscrepancy'): # \E{ K(x, x) } + \E{ K(y, y) } - 2 \E{ K(x, y) } cost = tf.reduce_mean(kernel(x, x)) cost += tf.reduce_mean(kernel(y, y)) cost -= 2 * tf.reduce_mean(kernel(x, y)) # We do not allow the loss to become negative. cost = tf.where(cost > 0, cost, 0, name='value') return cost
Example #3
Source File: losses_test.py From g-tensorflow-models with Apache License 2.0 | 5 votes |
def test_mmd_name(self): with self.test_session(): x = tf.random_uniform((2, 3), seed=1) kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([1.])) loss = losses.maximum_mean_discrepancy(x, x, kernel) self.assertEquals(loss.op.name, 'MaximumMeanDiscrepancy/value')
Example #4
Source File: losses.py From object_detection_with_tensorflow with MIT License | 5 votes |
def mmd_loss(source_samples, target_samples, weight, scope=None): """Adds a similarity loss term, the MMD between two representations. This Maximum Mean Discrepancy (MMD) loss is calculated with a number of different Gaussian kernels. Args: source_samples: a tensor of shape [num_samples, num_features]. target_samples: a tensor of shape [num_samples, num_features]. weight: the weight of the MMD loss. scope: optional name scope for summary tags. Returns: a scalar tensor representing the MMD loss value. """ sigmas = [ 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1, 5, 10, 15, 20, 25, 30, 35, 100, 1e3, 1e4, 1e5, 1e6 ] gaussian_kernel = partial( utils.gaussian_kernel_matrix, sigmas=tf.constant(sigmas)) loss_value = maximum_mean_discrepancy( source_samples, target_samples, kernel=gaussian_kernel) loss_value = tf.maximum(1e-4, loss_value) * weight assert_op = tf.Assert(tf.is_finite(loss_value), [loss_value]) with tf.control_dependencies([assert_op]): tag = 'MMD Loss' if scope: tag = scope + tag tf.summary.scalar(tag, loss_value) tf.losses.add_loss(loss_value) return loss_value
Example #5
Source File: losses.py From g-tensorflow-models with Apache License 2.0 | 5 votes |
def mmd_loss(source_samples, target_samples, weight, scope=None): """Adds a similarity loss term, the MMD between two representations. This Maximum Mean Discrepancy (MMD) loss is calculated with a number of different Gaussian kernels. Args: source_samples: a tensor of shape [num_samples, num_features]. target_samples: a tensor of shape [num_samples, num_features]. weight: the weight of the MMD loss. scope: optional name scope for summary tags. Returns: a scalar tensor representing the MMD loss value. """ sigmas = [ 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1, 5, 10, 15, 20, 25, 30, 35, 100, 1e3, 1e4, 1e5, 1e6 ] gaussian_kernel = partial( utils.gaussian_kernel_matrix, sigmas=tf.constant(sigmas)) loss_value = maximum_mean_discrepancy( source_samples, target_samples, kernel=gaussian_kernel) loss_value = tf.maximum(1e-4, loss_value) * weight assert_op = tf.Assert(tf.is_finite(loss_value), [loss_value]) with tf.control_dependencies([assert_op]): tag = 'MMD Loss' if scope: tag = scope + tag tf.summary.scalar(tag, loss_value) tf.losses.add_loss(loss_value) return loss_value
Example #6
Source File: losses.py From g-tensorflow-models with Apache License 2.0 | 5 votes |
def maximum_mean_discrepancy(x, y, kernel=utils.gaussian_kernel_matrix): r"""Computes the Maximum Mean Discrepancy (MMD) of two samples: x and y. Maximum Mean Discrepancy (MMD) is a distance-measure between the samples of the distributions of x and y. Here we use the kernel two sample estimate using the empirical mean of the two distributions. MMD^2(P, Q) = || \E{\phi(x)} - \E{\phi(y)} ||^2 = \E{ K(x, x) } + \E{ K(y, y) } - 2 \E{ K(x, y) }, where K = <\phi(x), \phi(y)>, is the desired kernel function, in this case a radial basis kernel. Args: x: a tensor of shape [num_samples, num_features] y: a tensor of shape [num_samples, num_features] kernel: a function which computes the kernel in MMD. Defaults to the GaussianKernelMatrix. Returns: a scalar denoting the squared maximum mean discrepancy loss. """ with tf.name_scope('MaximumMeanDiscrepancy'): # \E{ K(x, x) } + \E{ K(y, y) } - 2 \E{ K(x, y) } cost = tf.reduce_mean(kernel(x, x)) cost += tf.reduce_mean(kernel(y, y)) cost -= 2 * tf.reduce_mean(kernel(x, y)) # We do not allow the loss to become negative. cost = tf.where(cost > 0, cost, 0, name='value') return cost
Example #7
Source File: losses_test.py From HumanRecognition with MIT License | 5 votes |
def test_mmd_is_zero_when_distributions_are_same(self): with self.test_session(): x = tf.random_uniform((1000, 10), seed=1) y = tf.random_uniform((1000, 10), seed=3) kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([100.])) loss = losses.maximum_mean_discrepancy(x, y, kernel=kernel).eval() self.assertAlmostEqual(0, loss, delta=1e-4)
Example #8
Source File: losses_test.py From HumanRecognition with MIT License | 5 votes |
def test_fast_mmd_is_similar_to_slow_mmd(self): with self.test_session(): x = tf.constant(np.random.normal(size=(2, 3)), tf.float32) y = tf.constant(np.random.rand(2, 3), tf.float32) cost_old = MaximumMeanDiscrepancySlow(x, y, [1.]).eval() kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([1.])) cost_new = losses.maximum_mean_discrepancy(x, y, kernel).eval() self.assertAlmostEqual(cost_old, cost_new, delta=1e-5)
Example #9
Source File: losses_test.py From HumanRecognition with MIT License | 5 votes |
def test_mmd_is_zero_when_inputs_are_same(self): with self.test_session(): x = tf.random_uniform((2, 3), seed=1) kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([1.])) self.assertEquals(0, losses.maximum_mean_discrepancy(x, x, kernel).eval())
Example #10
Source File: losses_test.py From HumanRecognition with MIT License | 5 votes |
def test_mmd_name(self): with self.test_session(): x = tf.random_uniform((2, 3), seed=1) kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([1.])) loss = losses.maximum_mean_discrepancy(x, x, kernel) self.assertEquals(loss.op.name, 'MaximumMeanDiscrepancy/value')
Example #11
Source File: losses.py From HumanRecognition with MIT License | 5 votes |
def mmd_loss(source_samples, target_samples, weight, scope=None): """Adds a similarity loss term, the MMD between two representations. This Maximum Mean Discrepancy (MMD) loss is calculated with a number of different Gaussian kernels. Args: source_samples: a tensor of shape [num_samples, num_features]. target_samples: a tensor of shape [num_samples, num_features]. weight: the weight of the MMD loss. scope: optional name scope for summary tags. Returns: a scalar tensor representing the MMD loss value. """ sigmas = [ 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1, 5, 10, 15, 20, 25, 30, 35, 100, 1e3, 1e4, 1e5, 1e6 ] gaussian_kernel = partial( utils.gaussian_kernel_matrix, sigmas=tf.constant(sigmas)) loss_value = maximum_mean_discrepancy( source_samples, target_samples, kernel=gaussian_kernel) loss_value = tf.maximum(1e-4, loss_value) * weight assert_op = tf.Assert(tf.is_finite(loss_value), [loss_value]) with tf.control_dependencies([assert_op]): tag = 'MMD Loss' if scope: tag = scope + tag tf.summary.scalar(tag, loss_value) tf.losses.add_loss(loss_value) return loss_value
Example #12
Source File: losses.py From HumanRecognition with MIT License | 5 votes |
def maximum_mean_discrepancy(x, y, kernel=utils.gaussian_kernel_matrix): r"""Computes the Maximum Mean Discrepancy (MMD) of two samples: x and y. Maximum Mean Discrepancy (MMD) is a distance-measure between the samples of the distributions of x and y. Here we use the kernel two sample estimate using the empirical mean of the two distributions. MMD^2(P, Q) = || \E{\phi(x)} - \E{\phi(y)} ||^2 = \E{ K(x, x) } + \E{ K(y, y) } - 2 \E{ K(x, y) }, where K = <\phi(x), \phi(y)>, is the desired kernel function, in this case a radial basis kernel. Args: x: a tensor of shape [num_samples, num_features] y: a tensor of shape [num_samples, num_features] kernel: a function which computes the kernel in MMD. Defaults to the GaussianKernelMatrix. Returns: a scalar denoting the squared maximum mean discrepancy loss. """ with tf.name_scope('MaximumMeanDiscrepancy'): # \E{ K(x, x) } + \E{ K(y, y) } - 2 \E{ K(x, y) } cost = tf.reduce_mean(kernel(x, x)) cost += tf.reduce_mean(kernel(y, y)) cost -= 2 * tf.reduce_mean(kernel(x, y)) # We do not allow the loss to become negative. cost = tf.where(cost > 0, cost, 0, name='value') return cost
Example #13
Source File: losses_test.py From object_detection_with_tensorflow with MIT License | 5 votes |
def test_mmd_is_zero_when_distributions_are_same(self): with self.test_session(): x = tf.random_uniform((1000, 10), seed=1) y = tf.random_uniform((1000, 10), seed=3) kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([100.])) loss = losses.maximum_mean_discrepancy(x, y, kernel=kernel).eval() self.assertAlmostEqual(0, loss, delta=1e-4)
Example #14
Source File: losses_test.py From object_detection_with_tensorflow with MIT License | 5 votes |
def test_fast_mmd_is_similar_to_slow_mmd(self): with self.test_session(): x = tf.constant(np.random.normal(size=(2, 3)), tf.float32) y = tf.constant(np.random.rand(2, 3), tf.float32) cost_old = MaximumMeanDiscrepancySlow(x, y, [1.]).eval() kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([1.])) cost_new = losses.maximum_mean_discrepancy(x, y, kernel).eval() self.assertAlmostEqual(cost_old, cost_new, delta=1e-5)
Example #15
Source File: losses_test.py From object_detection_with_tensorflow with MIT License | 5 votes |
def test_mmd_is_zero_when_inputs_are_same(self): with self.test_session(): x = tf.random_uniform((2, 3), seed=1) kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([1.])) self.assertEquals(0, losses.maximum_mean_discrepancy(x, x, kernel).eval())
Example #16
Source File: losses_test.py From object_detection_with_tensorflow with MIT License | 5 votes |
def test_mmd_name(self): with self.test_session(): x = tf.random_uniform((2, 3), seed=1) kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([1.])) loss = losses.maximum_mean_discrepancy(x, x, kernel) self.assertEquals(loss.op.name, 'MaximumMeanDiscrepancy/value')
Example #17
Source File: losses_test.py From multilabel-image-classification-tensorflow with MIT License | 5 votes |
def test_mmd_is_zero_when_distributions_are_same(self): with self.test_session(): x = tf.random_uniform((1000, 10), seed=1) y = tf.random_uniform((1000, 10), seed=3) kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([100.])) loss = losses.maximum_mean_discrepancy(x, y, kernel=kernel).eval() self.assertAlmostEqual(0, loss, delta=1e-4)
Example #18
Source File: losses_test.py From g-tensorflow-models with Apache License 2.0 | 5 votes |
def test_mmd_is_zero_when_inputs_are_same(self): with self.test_session(): x = tf.random_uniform((2, 3), seed=1) kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([1.])) self.assertEquals(0, losses.maximum_mean_discrepancy(x, x, kernel).eval())
Example #19
Source File: losses_test.py From g-tensorflow-models with Apache License 2.0 | 5 votes |
def test_fast_mmd_is_similar_to_slow_mmd(self): with self.test_session(): x = tf.constant(np.random.normal(size=(2, 3)), tf.float32) y = tf.constant(np.random.rand(2, 3), tf.float32) cost_old = MaximumMeanDiscrepancySlow(x, y, [1.]).eval() kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([1.])) cost_new = losses.maximum_mean_discrepancy(x, y, kernel).eval() self.assertAlmostEqual(cost_old, cost_new, delta=1e-5)
Example #20
Source File: losses_test.py From g-tensorflow-models with Apache License 2.0 | 5 votes |
def test_mmd_is_zero_when_distributions_are_same(self): with self.test_session(): x = tf.random_uniform((1000, 10), seed=1) y = tf.random_uniform((1000, 10), seed=3) kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([100.])) loss = losses.maximum_mean_discrepancy(x, y, kernel=kernel).eval() self.assertAlmostEqual(0, loss, delta=1e-4)
Example #21
Source File: losses.py From models with Apache License 2.0 | 5 votes |
def maximum_mean_discrepancy(x, y, kernel=utils.gaussian_kernel_matrix): r"""Computes the Maximum Mean Discrepancy (MMD) of two samples: x and y. Maximum Mean Discrepancy (MMD) is a distance-measure between the samples of the distributions of x and y. Here we use the kernel two sample estimate using the empirical mean of the two distributions. MMD^2(P, Q) = || \E{\phi(x)} - \E{\phi(y)} ||^2 = \E{ K(x, x) } + \E{ K(y, y) } - 2 \E{ K(x, y) }, where K = <\phi(x), \phi(y)>, is the desired kernel function, in this case a radial basis kernel. Args: x: a tensor of shape [num_samples, num_features] y: a tensor of shape [num_samples, num_features] kernel: a function which computes the kernel in MMD. Defaults to the GaussianKernelMatrix. Returns: a scalar denoting the squared maximum mean discrepancy loss. """ with tf.name_scope('MaximumMeanDiscrepancy'): # \E{ K(x, x) } + \E{ K(y, y) } - 2 \E{ K(x, y) } cost = tf.reduce_mean(kernel(x, x)) cost += tf.reduce_mean(kernel(y, y)) cost -= 2 * tf.reduce_mean(kernel(x, y)) # We do not allow the loss to become negative. cost = tf.where(cost > 0, cost, 0, name='value') return cost
Example #22
Source File: losses.py From models with Apache License 2.0 | 5 votes |
def mmd_loss(source_samples, target_samples, weight, scope=None): """Adds a similarity loss term, the MMD between two representations. This Maximum Mean Discrepancy (MMD) loss is calculated with a number of different Gaussian kernels. Args: source_samples: a tensor of shape [num_samples, num_features]. target_samples: a tensor of shape [num_samples, num_features]. weight: the weight of the MMD loss. scope: optional name scope for summary tags. Returns: a scalar tensor representing the MMD loss value. """ sigmas = [ 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1, 5, 10, 15, 20, 25, 30, 35, 100, 1e3, 1e4, 1e5, 1e6 ] gaussian_kernel = partial( utils.gaussian_kernel_matrix, sigmas=tf.constant(sigmas)) loss_value = maximum_mean_discrepancy( source_samples, target_samples, kernel=gaussian_kernel) loss_value = tf.maximum(1e-4, loss_value) * weight assert_op = tf.Assert(tf.is_finite(loss_value), [loss_value]) with tf.control_dependencies([assert_op]): tag = 'MMD Loss' if scope: tag = scope + tag tf.summary.scalar(tag, loss_value) tf.losses.add_loss(loss_value) return loss_value
Example #23
Source File: losses_test.py From models with Apache License 2.0 | 5 votes |
def test_mmd_name(self): with self.test_session(): x = tf.random_uniform((2, 3), seed=1) kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([1.])) loss = losses.maximum_mean_discrepancy(x, x, kernel) self.assertEquals(loss.op.name, 'MaximumMeanDiscrepancy/value')
Example #24
Source File: losses_test.py From models with Apache License 2.0 | 5 votes |
def test_mmd_is_zero_when_inputs_are_same(self): with self.test_session(): x = tf.random_uniform((2, 3), seed=1) kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([1.])) self.assertEquals(0, losses.maximum_mean_discrepancy(x, x, kernel).eval())
Example #25
Source File: losses_test.py From models with Apache License 2.0 | 5 votes |
def test_fast_mmd_is_similar_to_slow_mmd(self): with self.test_session(): x = tf.constant(np.random.normal(size=(2, 3)), tf.float32) y = tf.constant(np.random.rand(2, 3), tf.float32) cost_old = MaximumMeanDiscrepancySlow(x, y, [1.]).eval() kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([1.])) cost_new = losses.maximum_mean_discrepancy(x, y, kernel).eval() self.assertAlmostEqual(cost_old, cost_new, delta=1e-5)
Example #26
Source File: losses_test.py From models with Apache License 2.0 | 5 votes |
def test_mmd_is_zero_when_distributions_are_same(self): with self.test_session(): x = tf.random_uniform((1000, 10), seed=1) y = tf.random_uniform((1000, 10), seed=3) kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([100.])) loss = losses.maximum_mean_discrepancy(x, y, kernel=kernel).eval() self.assertAlmostEqual(0, loss, delta=1e-4)
Example #27
Source File: losses.py From multilabel-image-classification-tensorflow with MIT License | 5 votes |
def maximum_mean_discrepancy(x, y, kernel=utils.gaussian_kernel_matrix): r"""Computes the Maximum Mean Discrepancy (MMD) of two samples: x and y. Maximum Mean Discrepancy (MMD) is a distance-measure between the samples of the distributions of x and y. Here we use the kernel two sample estimate using the empirical mean of the two distributions. MMD^2(P, Q) = || \E{\phi(x)} - \E{\phi(y)} ||^2 = \E{ K(x, x) } + \E{ K(y, y) } - 2 \E{ K(x, y) }, where K = <\phi(x), \phi(y)>, is the desired kernel function, in this case a radial basis kernel. Args: x: a tensor of shape [num_samples, num_features] y: a tensor of shape [num_samples, num_features] kernel: a function which computes the kernel in MMD. Defaults to the GaussianKernelMatrix. Returns: a scalar denoting the squared maximum mean discrepancy loss. """ with tf.name_scope('MaximumMeanDiscrepancy'): # \E{ K(x, x) } + \E{ K(y, y) } - 2 \E{ K(x, y) } cost = tf.reduce_mean(kernel(x, x)) cost += tf.reduce_mean(kernel(y, y)) cost -= 2 * tf.reduce_mean(kernel(x, y)) # We do not allow the loss to become negative. cost = tf.where(cost > 0, cost, 0, name='value') return cost
Example #28
Source File: losses.py From multilabel-image-classification-tensorflow with MIT License | 5 votes |
def mmd_loss(source_samples, target_samples, weight, scope=None): """Adds a similarity loss term, the MMD between two representations. This Maximum Mean Discrepancy (MMD) loss is calculated with a number of different Gaussian kernels. Args: source_samples: a tensor of shape [num_samples, num_features]. target_samples: a tensor of shape [num_samples, num_features]. weight: the weight of the MMD loss. scope: optional name scope for summary tags. Returns: a scalar tensor representing the MMD loss value. """ sigmas = [ 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1, 5, 10, 15, 20, 25, 30, 35, 100, 1e3, 1e4, 1e5, 1e6 ] gaussian_kernel = partial( utils.gaussian_kernel_matrix, sigmas=tf.constant(sigmas)) loss_value = maximum_mean_discrepancy( source_samples, target_samples, kernel=gaussian_kernel) loss_value = tf.maximum(1e-4, loss_value) * weight assert_op = tf.Assert(tf.is_finite(loss_value), [loss_value]) with tf.control_dependencies([assert_op]): tag = 'MMD Loss' if scope: tag = scope + tag tf.summary.scalar(tag, loss_value) tf.losses.add_loss(loss_value) return loss_value
Example #29
Source File: losses_test.py From multilabel-image-classification-tensorflow with MIT License | 5 votes |
def test_mmd_name(self): with self.test_session(): x = tf.random_uniform((2, 3), seed=1) kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([1.])) loss = losses.maximum_mean_discrepancy(x, x, kernel) self.assertEquals(loss.op.name, 'MaximumMeanDiscrepancy/value')
Example #30
Source File: losses_test.py From multilabel-image-classification-tensorflow with MIT License | 5 votes |
def test_mmd_is_zero_when_inputs_are_same(self): with self.test_session(): x = tf.random_uniform((2, 3), seed=1) kernel = partial(utils.gaussian_kernel_matrix, sigmas=tf.constant([1.])) self.assertEquals(0, losses.maximum_mean_discrepancy(x, x, kernel).eval())