本文整理匯總了Python中utils.gaussian_kernel_matrix方法的典型用法代碼示例。如果您正苦於以下問題:Python utils.gaussian_kernel_matrix方法的具體用法?Python utils.gaussian_kernel_matrix怎麽用?Python utils.gaussian_kernel_matrix使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類utils
的用法示例。
在下文中一共展示了utils.gaussian_kernel_matrix方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: maximum_mean_discrepancy
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import gaussian_kernel_matrix [as 別名]
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
示例2: mmd_loss
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import gaussian_kernel_matrix [as 別名]
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
示例3: test_mmd_name
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import gaussian_kernel_matrix [as 別名]
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')
示例4: test_mmd_is_zero_when_inputs_are_same
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import gaussian_kernel_matrix [as 別名]
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())
示例5: test_fast_mmd_is_similar_to_slow_mmd
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import gaussian_kernel_matrix [as 別名]
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)
示例6: test_mmd_is_zero_when_distributions_are_same
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import gaussian_kernel_matrix [as 別名]
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)
示例7: __init__
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import gaussian_kernel_matrix [as 別名]
def __init__(self, encoder, configs):
super(MMD, self).__init__()
self.sigmas = torch.FloatTensor([
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
])
if configs.cuda:
self.sigmas = self.sigmas.cuda()
self.gaussian_kernel = partial(utils.gaussian_kernel_matrix, sigmas=self.sigmas)